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组数据,分别给出两数的和以及两数的差,要求输出这两个数字且大数先输出,如果没有满足条件的数字就输出“impossible”。

一开始想到是暴力求解,似乎最近有点思维定势了,不过之后就想到了二分法和公式法求解。最终选用公式法。设定a为两数之和、b为两数之差,这两个数字为x和y。即有如下两个方程形成方程组:x+y=a ;x-y=b。即得:
x = (a + b) / 2;y = (a - b) / 2.得解。

注意点:
1)输出时候,大数先输出。
2)如果使用int型,注意判断是否除2之后会有精度损失。可以试试“3 0”、“3 2”,这两个测试数据。
3)如果a、b中有数字小于0,则输出"impossible".
4)如果用float变量计算,需要判断是否结果为整数,如果不是,输出“impossible”。
5)用float变量计算时候,输出的时候使用int格式输出,如printf("%d %d\n", (int)a, (int)b);

代码(1AC):
#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\n", (int)one, (int)two);
        }
        else{
            printf("impossible\n");
        }
    }
    return 0;
}



你可能感兴趣的:(测试,input,each,float,output,Numbers)