1065. A+B and C (64bit) (20)

时间:2022-09-01 09:55:02

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

这题有两个点:
第一:需要使用 long long int.附上一张各类数的取值范围
类型名称 字节数 取值范围
signed char 1 -128~+127
short int 2 -32768~+32767
int 4 -2147438648~+2147438647
long int 4 -2147438648~+2141438647
long long long int 8 -9223372036854775808~+9223372036854775807
第二:需要判断下a+b发生溢出的情况,当溢出时:
a>0,b>0,a+b<0,此时c一定小于a+b;
a<0,b<0,a+b>0,此时c一定大于a+b。

#include <iostream>
using namespace std;
int main(){  
    //freopen("in.txt","r",stdin); 
    int n,i;   
    long long int a,b,c;  
    cin>>n; 
    for(i=1;i<=n;++i)
    {     
        bool flag = true;  
        cin>>a>>b>>c; 
        long long res = a+b;   
        if(a>0 && b>0 && res<=0)
        {
            printf("Case #%d: true\n",i);  
            continue;
        }
        else if(a<0 && b<0 && res>=0)
        {
            printf("Case #%d: false\n",i);  
            continue;
        }
        if(res>c)
        {
            printf("Case #%d: true\n",i);  
            continue;
        }
        else
        {
            printf("Case #%d: false\n",i);  
            continue;
        }  

    }  
    return 0;  


}