PAT 1065 A+B and C (64bit) (20)
1065. A+B and C (64bit) (20)
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 0Sample Output:
Case #1: false Case #2: true Case #3: false
8
1 2 3
2 3 4
10 -10 0
101 -100 0
9223372036854775807 -9223372036854775808 0
9223372036854775808 9223372036854775808 1
9223372036854775808 -9223372036854775808 1
-9223372036854775808 -9223372036854775808 -1
ans:
Case #1: false
Case #2: true
Case #3: false
Case #4: true
Case #5: false
Case #6: true
Case #7: false
Case #8: false
代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const LL INF = 0x7FFFFFFFFFFFFFFF; 5 const int MAXN = 30; 6 const char str[MAXN] = "9223372036854775808"; 7 const char st[MAXN] = "-9223372036854775808"; 8 9 char a[MAXN], b[MAXN], c[MAXN]; 10 LL na, nb, nc, nd; 11 12 int check( LL x ) { return x > 0 ? 1 : x < 0 ? -1 : 0; } 13 14 int main() { 15 int t, cnt = 0; 16 int ta, tb, tc, td; 17 bool fa, fb, fc; 18 scanf( "%d", &t ); 19 while( t-- ) { 20 printf( "Case #%d: ", ++cnt ); 21 scanf( "%s%s%s", a, b, c ); 22 if( !strcmp( a, st ) && !strcmp( b, st ) ) { 23 puts( "false" ); continue; 24 } 25 fa = !strcmp( a, str ); 26 fb = !strcmp( b, str ); 27 fc = !strcmp( c, str ); 28 if( !fa && !fb ) { 29 sscanf( a, "%I64d", &na ); ta = check( na ); 30 sscanf( b, "%I64d", &nb ); tb = check( nb ); 31 if( !fc ) { sscanf( c, "%I64d", &nc ); tc = check( nc ); } 32 nd = na + nb; td = check( nd ); 33 if( ( ta * tb ) > 0 && ( ta * td ) < 0 ) { 34 if( ta > 0 ) puts( "true" ); 35 else puts( "false" ); 36 } else { 37 if( fc ) puts( "false" ); 38 else puts( nd > nc ? "true" : "false" ); 39 } 40 } else { 41 ta = a[0] == '-' ? -1 : a[0] == '0' ? 0 : 1; 42 tb = b[0] == '-' ? -1 : b[0] == '0' ? 0 : 1; 43 if( fc ) { 44 if( !fa ) { swap( a, b ); swap( ta, tb ); swap( fa, fb ); } 45 if( tb <= 0 ) puts( "false" ); 46 else puts( "true" ); 47 } else { 48 sscanf( c, "%I64d", &nc ); tc = check( nc ); 49 if( ta ^ tb ) { 50 if( !fa ) { swap( a, b ); swap( ta, tb ); swap( fa, fb ); } 51 sscanf( b, "%I64d", &nb ); 52 nd = INF + nb + 1; 53 puts( nd > nc ? "true" : "false" ); 54 } else puts( "true" ); 55 } 56 } 57 } 58 return 0; 59 }