HDOJ 1194 Beat the Spread!

【题意】:给出两个数 m n,第一个数是另外两个数a b的和,第二个数是a b的差的绝对值(absolute difference)。输出这两个数a b,大的在前。

【思路】:大的数等于 (m+n)/2,小的等于m-大的。

【注意】:impossible的判断。分两种,一种是m<n,,一种是m+n为奇数的情况。如果m+n为奇数,则a b不存在。

【AC代码】:

#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>#include <iomanip>using namespace std;int main(){int T = 0;cin >> T;getchar();while (T–){int m = 0, n = 0;cin >> m >> n;if (m < n || 0 != (m+n)%2){cout << "impossible" << endl;continue;}else{int a = 0, b = 0;a = (m+n)/2;b = m – a;if (a > b)cout << a << " " << b << endl;elsecout << b << " " << a << endl;}}return 0;}

乐观者在灾祸中看到机会;悲观者在机会中看到灾祸

HDOJ 1194 Beat the Spread!

相关文章:

你感兴趣的文章:

标签云: