栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pat 甲级 1002 python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pat 甲级 1002 python

不用考虑输入系数为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 )

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267130.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号