您的位置:首页 > 编程语言 > C语言/C++

【HDU】1194 Beat the Spread!

2018-03-19 21:03 465 查看
[align=left]Problem Description[/align]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?[align=left]Input[/align]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.[align=left]Output[/align]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.
[align=left]Sample Input[/align]
2
40 20
20 40 [align=left]Sample Output[/align]
30 10
impossible

    这道题其实是一道很简单的题,但是要读懂题意,注意细节。题目大概就是已知两个整数的和和差的绝对值,输出这两个整数,大的在前,小的在后。
    比如 Each test case gives s and d, non-negative integers representing the sum and (absolute) difference between the two final scores. 第一遍没有AC就是因为没考虑到输出要是整数。
代码:#include <stdio.h>

int main() {
int N,a,b,x1,x2;
scanf("%d",&N);
while (N--) {
scanf("%d %d",&a,&b);
if(a<b)
printf("impossible\n");
else if ((a+b)%2==0&&(a-b)%2==0) //判断要求的x1,x2是不是整数
{
x1=(a+b)/2;
x2=(a-b)/2;
printf("%d %d\n",x1,x2);
}
else
printf("impossible\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C语言