1065 A+B and C (64bit) (20 point(s))
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
经验总结:
这一题,要注意,A+B是有可能溢出的,而我们恰恰可以利用溢出的特性来进行判断,比如A、B如果都大于0,A+B小于0,那么A+B一定大于C,因为C肯定没有溢出,其他的情况也是这个道理~
AC代码
#include <cstdio>
int main()
{
int n;
long long a,b,c;
scanf("%d",&n);
for(int i=0;i<n;++i)
{
scanf("%lld %lld %lld",&a,&b,&c);
bool flag;
long long temp=a+b;
if(a>0&&b>0&&temp<0)
flag=1;
else if(a<0&&b<0&&temp>=0)
flag=0;
else
flag=temp>c;
printf("Case #%d: %s\n",i+1,flag?"true":"false");
}
return 0;
}