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


对于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;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值