PAT 甲级 1002 题目:为什么代码跑不过测试点 1?
# -*- coding:utf-8 -*-from collections import deque
def read_polynomial():
tokens = input().split(' ')[1:]
p = deque([])
for i in range(0, len(tokens) - 1, 2):
p.append((int(tokens[i]), float(tokens[i+1])))
return p
def add_polynomial(p1: deque, p2: deque):
ret = deque([])
while len(p1) != 0 and len(p2) != 0:
item1 = p1[0]
item2 = p2[0]
if item1[0] == item2[0]:
coef = item1[1] + item2[1]
if coef != 0.0:
ret.append((item1[0], coef))
p1.popleft()
p2.popleft()
elif item1[0] > item2[1]:
ret.append(p1.popleft())
else:
ret.append(p2.popleft())
if len(p1) != 0:
ret.extend(p1)
if len(p2) != 0:
ret.extend(p2)
return ret
def print_polynomial(p: deque):
ret = [str(len(p))]
while len(p) != 0:
item = p.popleft()
ret.append(str(int(item[0])))
ret.append(f'{item[1]:.1f}')
print(' '.join(ret))
if __name__ == '__main__':
p1 = read_polynomial()
p2 = read_polynomial()
p3 = add_polynomial(p1, p2)
print_polynomial(p3)
以上是 PAT 甲级 1002 题目:为什么代码跑不过测试点 1? 的全部内容, 来源链接: utcz.com/p/938554.html