1065 A+B and C (64bit)

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

题意:

  给出三个数字,计算两个数字之和是否大于第三个数字。

思路:

  因为相加的过程中可能产生溢出,所以要考虑到溢出的情况。所有可能的溢出情况是:正 + 正 = 负; 负 - 负 = 正(0)。如果是第一种溢出情况的话,两数之和一定大于第三个数;如果是第二种溢出的话,两数之和一定小于第三个数。

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int main() {
 6     int n;
 7     long long a, b, c, sum;
 8     cin >> n;
 9     for (int i = 1; i <= n; ++i) {
10         cin >> a >> b >> c;
11         sum = a + b;
12         if (a > 0 && b > 0 && sum < 0)
13             cout << "Case #" << i << ": true" << endl;
14         else if (a < 0 && b < 0 && sum > 0)
15             cout << "Case #" << i << ": false" << endl;
16         else {
17             if (sum > c)
18                 cout << "Case #" << i << ": true" << endl;
19             else
20                 cout << "Case #" << i << ": false" << endl;
21         }
22     }
23     return 0;
24 }

 

思考:

  为什么负数溢出可能为0,而整数不能为0呢?

 

posted @ 2020-05-04 22:42  Veritas_des_Liberty  阅读(233)  评论(0编辑  收藏  举报