PAT A1065:A+B and C (64bit)(溢出)

没有大整数的C++ STL真让人头疼

题目内容

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 (\leqslant 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

题目要点

本题 20 分,主要难点在使用 C/C++ 时 64位整数相加减可能会存在溢出的问题,需要讨论溢出的条件。Python 支持大整数,不用讨论。

AC 源代码

count = int(input())
numbers = [map(int, input().split()) for _ in range(count)]

for i, (a,b,c) in enumerate(numbers):
    if a + b > c:
        print('Case #{}: true'.format(i+1))
    else:
        print('Case #{}: false'.format(i+1))

参考来源

1065. A+B and C (64bit) (20)-PAT甲级真题_柳婼 の blog-CSDN博客