A1065 A+B and C (64bit)

decription

Given three integers A, B and C in [-2^63, 2^63], 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

idea

  • A,B的范围为 [-2^63, 2^63]
    则A+B可能会超出long long 范围
    结合本题只有20分,不至于用到大数运算
  • 考虑分类讨论,记sum = A+B
    当sum正溢出时,sum<0,则为true
    当sum负溢出时,sum>=0,则为false(注意等于的情况,否则第三个测试点过不去
    当sum为发生溢出时,直接比较sum和c的大小即可

solution

#include <stdio.h>
int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		long long a, b, c, sum;
		scanf("%lld%lld%lld", &a, &b, &c);
		sum = a + b;
		if(sum < 0 && a > 0 && b > 0)
			printf("Case #%d: true", i);
		else if (sum >= 0 && a < 0 && b < 0)
			printf("Case #%d: false", i);
		else{
			if(sum > c)
				printf("Case #%d: true", i);
			else
				printf("Case #%d: false", i);
		}
		if(i != n)
			printf("\n");
	}
	return 0;
}
posted @ 2022-03-01 11:20  Moliay  阅读(12)  评论(0编辑  收藏  举报