题目描述
Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C.
翻译:给你桑整数A,B,C 范围在[-2^63, 2^63]之间,你需要输出A+B是否大于C。
INPUT FORMAT
The first line of the input gives the positive number of test cases, T (<=10). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.
翻译:输入第一行为测试数据的组数T(<=10)。接着T组测试数据,每组包括一行三个整数A,B,C,之间用空格隔开。
OUTPUT FORMAT
For each test case, output in one line “Case #X: true” if A+B>C, or “Case #X: false” otherwise, where X is the case number (starting from 1).
翻译:对于每组输入数据,输出一行”Case #X: true”如果A+B>C,否则输出”Case #X: false” ,X为测试数据的次数。
Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
解题思路
这道题注意A+B要先保存到一个long long int类型变量中,否则数据仍按照int进行计算。数据需要判断溢出,即A>0&&B>0但是C<0,这种情况一定是true;A<0&&B<0但是A+B>=0,这种情况一定是false,注意负负相加可以为0。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
long long int A,B,C;
int main(){
int T;
scanf("%d",&T);
for(int i=1;i<=T;i++){
scanf("%lld%lld%lld",&A,&B,&C);
long long int sum=A+B;
printf("Case #%d: ",i);
if(A<0&&B<0&&sum>=0)printf("false\n");
else if(A>0&&B>0&&sum<0)printf("true\n");
else if(sum<=C)printf("false\n");
else if(sum>C)printf("true\n");
}
return 0;
}