PAT A1065 A+B and C (64bit) (20 分)

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

题意:

判断a+b是否大于c,是输出Case #X: true,否输出Case #X: false;

输入:

测试样例数
a b c
. . .

输出:

Case #1: false(或true)
. . .

思路:

(1)a,b,c的范围为[-263,263),确定其数据类型为long long型;
(2)要注意a+b的临界情况,若a>0,b>0,a+b<=0,则正溢出(参考计算机组成原理),输出Case #X: true;若a<0,b<0,a+b>=0,则负溢出,输出Case #X: false;
(3)在 if 语句的()中将long long 型的a+b直接与c比较会出错,所以需要用temp来存放a+b.

代码:

#include <cstdio>
int main(){
	long long a,b,c,temp;
	int t;
	scanf("%d",&t);
	for(int i=1;i<=t;i++){
		scanf("%lld %lld %lld",&a,&b,&c);
		temp=a+b;
		if(a>0&&b>0&&temp<=0){
			printf("Case #%d: true\n",i);
		}else if(a<0&&b<0&&temp>=0){
			printf("Case #%d: false\n",i);
		}else if(temp>c){
			printf("Case #%d: true\n",i);
		}else if(temp<=c){
			printf("Case #%d: false\n",i);
		}
	}
	return 0;
}

词汇:

… …

发布了26 篇原创文章 · 获赞 0 · 访问量 493

猜你喜欢

转载自blog.csdn.net/PanYiAn9/article/details/102014326