1065 A+B and C (64bit) (20)(20 分)提问
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
思路:
题目给出的范围[-2^63,2^63] 应该用long long.
隐含的条件就是发生了正溢出和负溢出,正溢结果为负数,负溢结果为正,需要考虑这一点.
#include<cstdio>
int main()
{
int i,K;
scanf("%d",&K);
long long a,b,c; //长整数类型
bool jud;
for(i=1;i<=K;i++)
{
scanf("%lld%lld%lld",&a,&b,&c);
long long sum=a+b; //二者加和值也必为长整数类型
if(a>0&&b>0&&sum<0) jud=true; //a,b均为正数,且加值发生溢出,若溢出值为负数,则a+b>c;
else if(a<0&&b<0&&sum>=0) jud=false; //a,b均为负数,且加值发生溢出,若溢出值为正数,则a+b<c;
else if(sum>c) jud=true; //如果加值大于C,则说明没有溢出,且a+b>c;
else jud=false; //其他情况,则为a+b<c;
if(jud==true) printf("Case #%d: true\n", i); //如果判断值为正,输出相应反应;
if(jud==false) printf("Case #%d: false\n", i); //如果判断值为负,输出相应反应;
}
return 0;
}