Python实现Pat 1065. 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.
输入描述:
The first line of the input gives the positive number of test cases, T (<=1000). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.
输出描述:
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).
输入例子:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
输出例子:
Case #1: false
Case #2: true
Case #3: false
解答
用C或c++语言实现起来比较麻烦,要考虑溢出问题:(参考的博客)
1.long long 取值为【-2^63, 2^63)左开右闭。
2.超出范围的时候要判断溢出情况,考虑溢出后的取值
3.A+B的值必须用long long 类型 的res存储后才能与C进行比较,不能直接放到if()中进行比较不然测试点2、3过不去。
4.分情况判断时候要用else if 不能一串if,不然可能会出现一种情况满足几个条件,造成后面的flag覆盖前面的(正负溢出应该优先判断)
而用Python就非常简单了。而且也能通过测试。
n=int(input())
for i in range(n):
abc=[int(x) for x in input().split(' ')]
if abc[0]+abc[1]>abc[2]:
print("Case #%d: true"%(i+1))
else:
print("Case #%d: false" % (i+1))
还没有评论,来说两句吧...