Python实现Pat 1065. A+B and C (64bit) (20)

爱被打了一巴掌 2022-06-04 01:39 236阅读 0赞

题目描述

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就非常简单了。而且也能通过测试。

  1. n=int(input())
  2. for i in range(n):
  3. abc=[int(x) for x in input().split(' ')]
  4. if abc[0]+abc[1]>abc[2]:
  5. print("Case #%d: true"%(i+1))
  6. else:
  7. print("Case #%d: false" % (i+1))

这里写图片描述

发表评论

表情:
评论列表 (有 0 条评论,236人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PAT~乙级~1065 单身狗 ~python

    目描述: “单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。 输入格式: 输入第一行给出一个正整数 N(≤ ...