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
结尾无空行
Thanks to Jiwen Lin for amending the test data.
本题,我一开始以为考的是大整数相加,后来发现写不出来,搜了一下,大家都用的判断是否溢出,虽然我不太理解,但是还是明白了大致意思:
如果 x 和 y 都是大于 0 的,那么相加的范围就是 [2, 2^64 - 1) 这样是非常可能发生溢出的,如果溢出,结果就是小于 0 的,所以此时一定大于 c
反之 如果都小于 0 一个道理。
接下来就是不溢出的情况,直接按要求写即可。
#include<iostream>
#include<bits/stdc++.h>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<unordered_set>
#include<unordered_map>
#include<cctype>
#include<algorithm>
#include<stack>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
long long int x, y, c;
scanf("%lld %lld %lld", &x, &y, &c);
long long sum = x + y;
if (x > 0 && y > 0 && sum < 0) {
printf("Case #%d: true\n", i);
} else if (x < 0 && y < 0 && sum >= 0) {
printf("Case #%d: false\n", i);
} else if (x + y > c) {
printf("Case #%d: true\n", i);
} else {
printf("Case #%d: false\n", i);
}
}
return 0;
}