POJ 2301 Beat the Spread!(私の水題の道--二数の和、二数の差)


Beat the Spread!
Time Limit: 1000MS
 
Memory Limit: 65536K
Total Submissions: 15515
 
Accepted: 7391
Description
Superbowl Sunday is nearly here. In order to pass the time waiting for the half-time commercials and wardrobe malfunctions, the local hackers have organized a betting pool on the game. Members place their bets on the sum of the two final scores, or on the absolute difference between the two scores. 
Given the winning numbers for each type of bet, can you deduce the final scores? 
Input
The first line of input contains n, the number of test cases. n lines follow, each representing a test case. Each test case gives s and d, non-negative integers representing the sum and (absolute) difference between the two final scores.
Output
For each test case, output a line giving the two final scores, largest first. If there are no such scores, output a line containing "impossible". Recall that football scores are always non-negative integers.
Sample Input
2
40 20
20 40

Sample Output
30 10
impossible

Source
Waterloo local 2005.02.05
N組のデータについては,それぞれ2数の和と2数の差を与え,この2つの数字を出力して大数で先に出力し,条件を満たす数字がなければ「impossible」を出力することが要求される.
最初は暴力的な解だと思っていたが、最近は少し考えが落ち着いたようだが、その後は二分法と公式法の解を思いついた.最終的には数式法を選択します.aを2数の和、bを2数の差とし、この2つの数字はxとyとする.すなわち、x+y=aという2つの方程式が方程式群を形成する.x-y=b.次のようになります.
x = (a + b)/2;y = (a - b)/2.解を得る.
注意点:
1)出力するときは、大数で先に出力します.
2)int型を使用する場合は,2を除いて精度が損なわれるか否かの判断に注意する.「3 0」、「3 2」の2つのテストデータを試してみてください.
3)a,bの中に0未満の数字がある場合は「impossible」を出力.
4)float変数で計算する場合は,結果が整数であるか否かを判断し,そうでない場合は「impossible」を出力する必要がある.
5)float変数で計算する場合、printf("%d%d",(int)a,(int)b)のようなintフォーマットで出力する.
コード(1 AC):
#include <cstdio>
#include <cstdlib>
#include <cmath>

int main(void){
    int ii, casenum;
    int sum, substract;
    float one, two;

    scanf("%d", &casenum);
    for (ii = 0; ii < casenum; ii++){
        scanf("%d%d", &sum, &substract);
        one = (sum + substract) / 2,0;
        two = (sum - substract) / 2.0;
        if (one >= 0 && two >= 0 && one - (int)one == 0 && two - (int)two == 0){
            printf("%d %d
", (int)one, (int)two); } else{ printf("impossible
"); } } return 0; }