k-tokitoh

2019-07-28

AtCoder Beginner Contest 135 A

C++の基本的なところ、本読むより実際に書いた方が覚えられそうなので練習する。

A - Harmony

最初に書いたコード

#include<iostream>
#include<string>
using namespace std;

int main() {
  int n, m;
  string message = "IMPOSSIBLE";
  cin >> n >> m;
  if ((n + m) % 2 == 0) {
    cout << (n + m) / 2 << endl;
  } else {
    cout << message << endl;
  }
}

他の回答をみて修正したコード

#include<iostream>
#include<string>
using namespace std;

int main() {
  int n, m;
  string message = "IMPOSSIBLE";
  cin >> n >> m;
  if ((n + m) & 1) cout << message << endl;
  else cout << (n + m) / 2 << endl;
}

学んだこと