原题链接
Problem Description:Given three integers A , B A, B A,B and C C C in $(-2^{63}, 2^{63}), you are supposed to tell whether A + B > C A + B > C A+B>C.
Input Specification:The first line of the input gives the positive number of test cases, T ( ≤ 10 ) T(leq 10) T(≤10). Then T T T test cases follow, each consists of a single line containing three integers A , B A, B A,B and C C C, separated by single spaces.
Output Specification:For each test case, output in one line Case #X: true if A + B > C A + B > C A+B>C, or Case #X: false otherwise, where X X X is the case number (starting from 1).
Sample Input:3 1 2 3 2 3 4 9223372036854775807 -9223372036854775808 0Sample Output:
Case #1: false Case #2: true Case #3: falseProblem Analysis:
本题涉及到 64 64 64 位整数相加,相加过程会爆 long long,因此我们将所有情况列出来,进行判断即可。
- a > 0 , b > 0 , a + b < 0 a > 0, b > 0, a + b < 0 a>0,b>0,a+b<0,正向溢出,此时 a + b a + b a+b 一定大于 c c c。
- a < 0 , b < 0 , a + b ≥ 0 a < 0, b < 0, a + b geq 0 a<0,b<0,a+b≥0,反向溢出,此时 a + b a + b a+b 一定小于 c c c。
- 均无溢出,此时直接判断 a + b a + b a+b 是否大于 c c c 即可。
#include#include #include #include using namespace std; typedef long long LL; bool check(LL a, LL b, LL c) { LL res = a + b; if (a > 0 && b > 0 && res < 0) return true; if (res > c) return true; return false; } int main() { int T; cin >> T; for (int i = 1; i <= T; i ++ ) { LL a, b, c; cin >> a >> b >> c; if (check(a, b, c)) printf("Case #%d: truen", i); else printf("Case #%d: falsen", i); } return 0; }



