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
#include<iostream>
using namespace std;
typedef long long int ll;
int t;
ll a, b, c;
bool check(ll a, ll b, ll c)
{
ll d = a + b;
if(a >= 0 && b >= 0 && d < 0) return true;
if(a < 0 && b < 0 && d >= 0) return false;
return a + b > c;
}
int main()
{
cin >> t;
for(int i = 1; i <= t; i ++ )
{
printf("Case #%d: ", i);
scanf("%lld%lld%lld", &a, &b, &c);
if(check(a, b, c))
printf("true\n");
else
printf("false\n");
}
}
long long int 可以存下64位的数据,但是因为计算机计算加法的时候用的是补码,所以计算出来的结果可能会溢出,当两个数都位正数时,溢出结果为负,但是因为三个数都是64位以内,所以当两个正数相加溢出(判断结果为相加结果为负)时,a+b>c一定成立,相反,如果两个负数相加溢出,结果不成立。