python记录(10.23)

【PAT A1065】A+B and C(64bit)

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 (≤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). Each line should ends with '\n'.

Sample Input:

3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0

Sample Output:

Case #1: false
Case #2: true
Case #3: false

Thanks to Jiwen Lin for amending the test data.

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

python解法:

T = eval(input())
for i in range(T):
    a,b,c = map(int,input().split())
    if a+b > c:
        print(f"Case #{i+1}: true")
    else:
        print(f"Case #{i+1}: false")
        

如果用C++还要判断溢出,如果两个正数之和等于负数,或者两个负数之和等于正数,那么就是溢出。

https://zhuanlan.zhihu.com/p/399590503

 【PAT A1002】A+B for Polynomials

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N1​ aN1​​ N2​ aN2​​ ... NK​ aNK​​

where K is the number of nonzero terms in the polynomial, Ni​ and aNi​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK​<⋯<N2​<N1​≤1000.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

第一次解:

A = [eval(x) for x in list(input().split())]
B = [eval(x) for x in list(input().split())]
Adict = {}
Bdict = {}
for i in range(1,len(A),2):
    Adict[A[i]] = A[i+1]
for i in range(1,len(B),2):
    Bdict[B[i]] = B[i+1]

for key,value in Bdict.items():
    if key in Adict:
        Adict[key] += value
    else:
        Adict[key] = value
C = dict(sorted(Adict.items(),key = lambda x:x[0],reverse=True))
print(len(C),end="")
for key,value in C.items():
    print(f" {key} {value}",end="")

思考1:因为输入有小数有整数,不能用map(int,..)来解,想到用eval(代码行:1)

在思考过程中看到一个帖子,虽然与本题无关,但可以记录一下:

Python 如何将字符串转为字典 - 菜鸟学院

思考2:两个字典怎么相加键相同的值?

参考https://www.jb51.net/article/157352.htm

 

 

 结果:

第二次解:

增加了多项式加减法后,0项的删除。参考输入,只有非零项。 

删除字典:pop和del的区别在于有无返回值。形式:A.pop(key)    del A[key]

python基础之字典的删除_jiankang66的博客-CSDN博客_python 字典删除

A = [eval(x) for x in list(input().split())]
B = [eval(x) for x in list(input().split())]
Adict = {}
Bdict = {}
for i in range(1,len(A),2):
    Adict[A[i]] = A[i+1]
for i in range(1,len(B),2):
    Bdict[B[i]] = B[i+1]

for key,value in Bdict.items():
    if key in Adict:
        Adict[key] += value
        if Adict[key] == 0:
            del Adict[key]
    else:
        Adict[key] = value
C = dict(sorted(Adict.items(),key = lambda x:x[0],reverse=True))
print(len(C),end="")
for key,value in C.items():
    print(f" {key} {value}",end="")

 第三次解:

还有测试点没通过。可能是格式问题。仔细阅读:

Please be accurate to 1 decimal place.

翻译:请精确到小数点后一位

A = [eval(x) for x in list(input().split())]
B = [eval(x) for x in list(input().split())]
Adict = {}
Bdict = {}
for i in range(1,len(A),2):
    Adict[A[i]] = A[i+1]
for i in range(1,len(B),2):
    Bdict[B[i]] = B[i+1]

for key,value in Bdict.items():
    if key in Adict:
        Adict[key] += value
        if Adict[key] == 0:
            del Adict[key]
    else:
        Adict[key] = value
C = dict(sorted(Adict.items(),key = lambda x:x[0],reverse=True))
print(len(C),end="")
for key,value in C.items():
    print(f" {key} {value:.1f}",end="")

 【PAT A1009】Product of Polynomials

This time, you are supposed to find A×B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N1​ aN1​​ N2​ aN2​​ ... NK​ aNK​​

where K is the number of nonzero terms in the polynomial, Ni​ and aNi​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤NK​<⋯<N2​<N1​≤1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

看不懂怎么运算的,之后再看

可以参考

 https://www.bilibili.com/read/cv9778023/

 Python itertools 模块中的 product 函数_CodeLogs的博客-CSDN博客_python product

10.27补充

系数相乘,指数相加

 

A = [eval(x) for x in list(input().split())]
B = [eval(x) for x in list(input().split())]
Adict = {}
Bdict = {}
C = {}
for i in range(1,len(A),2):
    Adict[A[i]] = A[i+1]
for i in range(1,len(B),2):
    Bdict[B[i]] = B[i+1]

for key,value in Bdict.items():
    for key1,value1 in Adict.items():
        C[key + key1] = C.get(key + key1,0) + value*value1
        if C[key + key1] == 0:
            del C[key + key1]
C = dict(sorted(C.items(),key = lambda x:x[0],reverse=True))
print(len(C),end="")
for key,value in C.items():
    print(f" {key} {value:.1f}",end="")

 

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值