A1065 A+Band C(64 bit)

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 (≤). 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相加之后可能会溢出,溢出的情况根据溢出后的数值分类讨论

问题:

部分测试点没通过

解决:

•A+B必须放在long long型变量中才可以与C比较,而不是在if的条件中直接比较,即 long long sum=A+B; if(sum>C)

•A B 最小值都是-263,相加得-264,故sum溢出后最小为0

 1 #include <iostream>
 2 using namespace std;
 3 int main() {
 4     long long A, B, C;
 5     int n;
 6     cin >> n;
 7     for (int i = 0; i < n; i++) {
 8         cin >> A >> B >> C;
 9         long long sum = A + B;
10         if (A < 0 && B < 0 && sum>=0)
11             printf("Case #%d: false\n", i+1);
12         else if (A > 0 && B > 0 && sum< 0)
13             printf("Case #%d: true\n", i+1);
14         else if (sum> C)
15             printf("Case #%d: true\n", i + 1);
16         else
17             printf("Case #%d: false\n", i+1);    
18     }
19     return 0;
20 }

猜你喜欢

转载自www.cnblogs.com/PennyXia/p/12284932.html