Description:
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:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
因为A、B的大小为[-2^63, 2^63),显然使用long long;
sum和A、B的正负可以用来判断是否溢出;
如果A > 0, B > 0,sum可能会溢出,sum∈(0, 2^63 – 1+2^63 -1],如果溢出,则sum∈[-2^63(2^63), -2];
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).”
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
因为A、B的大小为[-2^63, 2^63),显然使用long long;
sum和A、B的正负可以用来判断是否溢出;
如果A > 0, B > 0,sum可能会溢出,sum∈(0, 2^63 – 1+2^63 -1],如果溢出,则sum∈[-2^63(2^63), -2];
如果A < 0, B < 0,sum可能会溢出,同理,sum溢出后结果是大于0的。
//NKW 甲级真题1023
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
int main(){
int T;
scanf("%d", &T);
long long a, b, c;
for (int i = 1; i <= T; i++) {
scanf("%lld %lld %lld", &a, &b, &c);
long long sum = a + b; //为了之后判断溢出,long long 的范围是-2^63-2^63-1
if (a > 0 && b > 0 && sum < 0)
printf("Case #%d: true\n", i);
else if (a < 0 && b < 0 && sum >= 0) //负溢出判断时,如果两个负数都是-2^63则和为-2^64,sum=0
printf("Case #%d: false\n", i);
else if (sum > c)
printf("Case #%d: true\n", i);
else
printf("Case #%d: false\n", i);
}
system("pause");
return 0;
}