0.题目链接https://www.patest.cn/contests/pat-a-practise/1065

1065. A+B and C (64bit) (20)
时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HOU, Qiming

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

1.题意分析

给出三个整数A,B,C,范围在【-2^63,2^63】之间,判断A+B是否大于C.


2.坑点分析

(1)A,B,C三个数据都是【-2^63,2^63】之间,所以这道题需要考虑大数据,即我们必须使用long long 来存储数据。

(2)对于两个数A,B,我们可以知道只有当A和B同时大于0时,才可能有A+B>2^63;当A和B同时小于0的时候,才有A+B<(-2^63);若不在此范围之内,我们肯定可以保证数据不会越界。


3.代码如下

#include <cstdio>

long long inf1 =  ((long long)1<<63)-1;//最大值 
long long inf2 = (-1)*inf1- 1;//最小值 
 
 
void function(long long result ,long long a,long long b,long long c,int count){
	result = a+b;	
	if(result <= c){			
		printf("Case #%d: false\n",count);	
	}
	else
	{
		printf("Case #%d: true\n",count);
	}
	count++;	
} 

int main(){	
	int number;
	scanf("%d",&number);
	int count = 1;
	while(number--){		
		long long a ,b ,c;
		long long result ;
		scanf("%lld %lld %lld",&a,&b,&c);
		
		//如果两者同时小于0,则有可能超过下界
		if(a<0 && b<0){
			if(inf2 - a > b){			
				printf("Case #%d: false\n",count);//a+b肯定小于c 
			}
			else{
				function(result,a,b,c,count);			
			}			
		}
		
		//如果两者同时大于0,则有可能超过上界 
		else if(a >0 && b>0){
			if(inf1 - a < b){			
				printf("Case #%d: true\n",count);//a+b肯定大于c 
			}
			else{
				function(result,a,b,c,count);			
			}
		}
		
		else{
			function(result,a,b,c,count);		
		}		
		count++;		
	}	
} 
/**测试数据
3
0 0 0
2 3 4
-9223372036854775807 -9223372036854775808 0
**/