问题描述
Given three integers A, B and C in [263,263], you are supposed to tell whether A+B>C.
输入格式
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.
输出格式
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).
输入样例
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
(结尾无空行)
输出样例
Case #1: false
Case #2: true
Case #3: false
(结尾无空行)
C语言代码
#include<stdio.h>
#include<stdbool.h>
int main(){
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++){
long long A, B, C;
scanf("%lld%lld%lld", &A, &B, &C);
long long temp = A + B;
bool flag;
if(A > 0 && B > 0 && temp < 0) flag = true; //正溢出判断
else if(A < 0 && B < 0 && temp >= 0) flag = false; //负溢出判断
else if(temp > C) flag = true; //正常计算情况下的判断
else flag = false;
if(flag == true)
printf("Case #%d: true",i);
else
printf("Case #%d: false",i);
if(n!=i) printf("\n");
}
return 0;
}
注意:
- 加法溢出判断依据:“正数+正数 = 负数; 负数 + 负数 = 正数” 时表示出现溢出,n位有符号正整数相加的正溢出溢出值为[-2n-1,-2],n位有符号负整数相加的负溢出值为[0,2n-1-1]
- 正负溢出时相当于表示A+B正负无穷大,此时A+B与C的大小不能正常判断
- 负溢出时,A+B可能会出现等于0的情况,所以判断负溢出条件temp>=0不能忽略 “ = ” 号