题目
Given three integers A, B and C in [-2^63, 2^63], you are supposed to tell whether A+B > C.
Input Specification:
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.
Output Specification:
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).
Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
思路
之前我在乙题那里也做过类似的题,不过它是32位的,只要用long long就可以了。今天这道题我吸取了教训,然后就直接用double来存储数据,然而发现,结果不对??是的,不管我怎么测试,感觉double的范围就是比long long大啊,但是结果都是错的。无奈之下,只能用long long加溢出判断了。
特殊情况:
- 当A+B>=2^63,显然有A+B>C成立。然而因为相加后溢出,导致相加后的结果为负,需要处理;
- 与1相反,当A+B<=-2^63时,显然不成立,但相加后溢出结果大于等于0,需要处理。
依稀觉得这题很熟悉!!记住了!!不能忘记!!代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using namespace std;
int main(){
long long a, b, c;
int t;
scanf("%d", &t);
for(int i=1; i<=t; i++){
scanf("%lld%lld%lld", &a, &b, &c);
if(a+b<0 && a>0 && b>0){
printf("Case #%d: true\n", i);
}
else if(a<0 && b<0 && a+b>=0){ //>=
printf("Case #%d: false\n", i);
}
else if(a+b > c){
printf("Case #%d: true\n", i);
}
else if(a+b <= c){
printf("Case #%d: false\n", i);
}
}
}
以后还是乖乖按题目要求做题好了。