不用考虑输入系数为0的情况 题目给出k为多项式非零项的个数
注意一、如果加起来系数为0 则该项不输出 k也要减去系数为零的项
注意二、如果k为0 即整个多项式为0 result为空 则只输出0 后面没有空格 要不然测试点6会出现格式错误
注意三、一定要将系数变为1位小数输出 指数变为整型输出 要不让测试点2会出错
通过。用字典存储多项式 key为指数 value为系数。如果不在字典的key中 就创建新的键值对。如果在key中 就将系数加到该key的value中。有可能指数高的key排在字典后面 因此需要使用sorted()函数进行排序
n list(map(float, input().split()))
m list(map(float, input().split()))
result {}
k1 int(n[0])
k2 int(m[0])
for i in range(1, 2 * k1, 2):
result[n[i]] n[i 1]
for j in range(1, 2 * k2, 2):
if m[j] in result:
result[m[j]] m[j 1]
if result[m[j]] 0: % 删除value(系数)为0的键值对
result.pop(m[j])
else:
result[m[j]] m[j 1]
if len(result) 0:
print(len(result), end )
else:
print(len(result), end )
result sorted(result.items(), reverse True)
for k in result:
if k ! result[-1]:
print( %d %.1f % (k[0], k[1]), end )
else:
print( %d %.1f % (k[0], k[1]))
测试点1答案错误。指数需要按整型格式输出
n input().split()
m input().split()
result {}
k1 int(n[0])
k2 int(m[0])
for i in range(1,2*k1,2):
result[n[i]] float(n[i 1])
for j in range(1,2*k2,2):
if m[j] in result:
result[m[j]] float(m[j 1])
if result[m[j]] 0:
result.pop(m[j])
else:
result[m[j]] float(m[j 1])
print(len(result),end )
result sorted(result.items(),reverse True)
for k in result:
print( %s %.1f %(k[0],k[1]),end )
测试点1答案错误 6格式错误。注意当k为0时 只输出0 后面没有空格 输出格式不符题意。思路太复杂
n input().split() m input().split() result [] k1 int(n[0]) k2 int(m[0]) while i 2*k1 or j 2*k2: if i 2*k1: result.extend([m[j],float(m[j 1])]) continue if j 2*k2: result.extend([n[i],float(n[i 1])]) continue if n[i] m[j]: result.extend([n[i], float(n[i 1])]) elif n[i] m[j]: result.extend([m[j], float(m[j 1])]) else: s float(n[i 1]) float(m[j 1]) if s ! 0: % 加和系数非零才输出 result.extend([n[i],s]) print(len(result)//2,end ) print(*result,sep )
测试点1答案错误 6格式错误。
n input().split() m input().split() result [0 for k in range(1002)] k1 int(n[0]) k2 int(m[0]) for i in range(1,2*k1,2): result[int(n[i])] float(n[i 1]) for j in range(1,2*k2,2): result[int(m[j])] float(m[j 1]) for k in range(1001,-1,-1): if result[k] ! 0: s.extend([k,result[k]]) print(len(s)//2,end ) print(*s,sep )
多个错误
n input().split() m input().split() result [] k1 int(n[0]) k2 int(m[0]) while i 2*k1 or j 2*k2: if i 2*k1: result.extend([m[j],float(m[j 1])]) continue if j 2*k2: result.extend([n[i],float(n[i 1])]) continue if n[i] m[j]: result.extend([n[i], float(n[i 1])]) elif n[i] m[j]: result.extend([m[j], float(m[j 1])]) else: result.extend([n[i],float(n[i 1]) float(m[j 1])]) print(len(result)//2,end ) print(*result,sep )



