A1065. A+B and C (64bit)
Given three integers A, B and C in [-263, 263], 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
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 int main(){ 5 int T; 6 long long a, b, c, test; 7 scanf("%d", &T); 8 for(int i = 0; i < T; i++){ 9 scanf("%lld%lld%lld", &a, &b, &c); 10 test = a + b; 11 if(a >0 && b > 0 && test < 0){ 12 printf("Case #%d: true\n", i + 1); 13 }else if(a < 0 && b < 0 && test >= 0){ 14 printf("Case #%d: false\n", i + 1); 15 }else{ 16 if(test > c) 17 printf("Case #%d: true\n", i + 1); 18 else 19 printf("Case #%d: false\n", i + 1); 20 } 21 } 22 return 0; 23 }
总结:1、首先输入的范围在[-2^63, 2^63], 则只能用long long 8个byte存储。当a与b过大时,会产生溢出,需要考虑正数+正数=负数及负数+负数=正数的情况。
2、A + B的结果不能在条件语句中直接与C比较,必须先存储于long long变量中。