A - Add Odd or Subtract Even
思路:其实认真观察就能发现,这个与输入的书有关系,且答案为0,1,2。先看相同,不用加减,为0,再看前小后大,因为加奇数减偶数,如果,相差奇数,为1,相差偶数,为2,同理可得前大后小的答案。
代码:
#include<iostream> #include<cmath> using namespace std; int main(){ std::ios::sync_with_stdio(false); int a, b, sum, n; cin >> n; while (n--){ cin >> a >> b; if (a == b) cout << "0" << endl; else if (a < b){ sum = b - a; if (sum % 2 == 0) cout << "2" << endl; else cout << "1" << endl; } else{ sum = a - b; if (sum % 2 == 0) cout << "1" << endl; else cout << "2" << endl; } } return 0; }