注意一、尽量少使用循环迭代来查找元素,python效率很低,容易超时。查找嵌套列表中子列表中的元素只能使用循环迭代来查找,所以也要减少嵌套列表的使用。多使用单层列表或字典,因为可以用 if x in list(dir)来快速查找元素
注意二、字典是无序的,不能使用sort()方法或sorted()函数来排序,要转化为有序的列表来排序,需使用字典特有的items()方法。
items() 方法以列表形式(并非直接的列表,若要返回列表值还需调用list函数)返回可遍历的(键, 值) 元组数组。
items的返回值需要接收,而且返回的并非直接的列表,而是dict_items类型(不知道是什么类型),只能使用sorted()函数来排序,不能使用sort()方法。如果需要返回列表需要使用list()函数,可以使用sort()方法。
sort()方法不需要接收,因为没有返回值,sort()方法是在原有的列表上更改数据,内存地址不变。sorted()函数需要接收返回值,sorted()函数是在内存中创建一个新列表,内存地址改变了
使用多个单层字典来存储数据,方便快速查找。还可以优化,总分需要四舍五入,可以直接加0.5再取整,不使用条件语句进行判断。谨慎使用round()函数,round()函数五不一定会入位。
可以简化使用联合排序:
score = list(score.items()) #items返回的是并非列表,需要使用list函数 score.sort(key=lambda x:(-x[1],x[0])) #先按成绩降序排列,成绩相同时再按名字字母升序排列。默认升序排列,"-"代表降序排列,等同于reverse=Ture,"-"不能用于字符串降序排列
p,m,n = map(int,input().split())
gp_dirc = {}
gm_dirc = {}
gf_dirc = {}
score = {}
for i in range(p):
name, gp = input().split()
if int(gp) >= 200:
gp_dirc[name] = int(gp)
for i in range(m):
name, gm = input().split()
if name in gp_dirc:
gm_dirc[name] = int(gm)
for i in range(n):
name, gf = input().split()
if name in gp_dirc:
gf_dirc[name] = int(gf)
for i in gp_dirc:
if i in gf_dirc:
if i in gm_dirc:
if gf_dirc[i] >= gm_dirc[i]:
s = gf_dirc[i]
else:
s = gm_dirc[i]*0.4+gf_dirc[i]*0.6
if s - int(s) >= 0.5: # 可以直接加0.5再取整,不使用条件语句
s = int(s) + 1
else:
s = int(s)
if s >= 60: # 总分不小于60才加入列表输出
score[i] = s
else:
if gf_dirc[i] >= 60:
score[i] = gf_dirc[i]
gm_dirc[i] = -1
score = sorted(score.items()) # 先按名字字母升序排列
score.sort(key=lambda x:x[1],reverse=True) # 再按总分降序排列
for i in score:
print(i[0],gp_dirc[i[0]],gm_dirc[i[0]],gf_dirc[i[0]],i[1])
测试点3超时。使用了多个循环来查找元素
p,m,n = map(int,input().split())
score = []
result = []
for i in range(p):
name,gp = input().split()
if int(gp) >= 200:
score.append([name,int(gp)])
for i in range(m):
name,gm = input().split()
for j in score:
if j[0] == name:
j.append(int(gm))
for i in score: # 未参加其中考试,将期中成绩设置为-1
if len(i) == 2:
i.append(-1)
for i in range(n):
name,gf = input().split()
for k in score:
if k[0] == name:
k.append(int(gf))
for i in score:
if i[2] != -1 and len(i) == 4:
if i[3] >= i [2] and i[3] >= 60:
result.append([i[0],i[1],i[2],i[3],i[3]])
else:
g = i[2]*0.4+i[3]*0.6
if g - int(g) >= 0.5:
g = int(g)+1
else:
g = int(g)
if g >= 60:
result.append([i[0],i[1],i[2],i[3],g])
elif i[2] == -1 and len(i) == 4:
if i[3] >= 60:
result.append([i[0],i[1],i[2],i[3],i[3]])
result.sort()
result.sort(key=lambda x:x[4],reverse=True)
for i in result:
print(*i,sep=' ')
测试点1答案错误,测试点3超时。未考虑参加了期中考试但没参加期末考试的情况
p,m,n = map(int,input().split())
score = []
result = []
for i in range(p):
name,gp = input().split()
if int(gp) >= 200:
score.append([name,int(gp)])
for i in range(m):
name,gm = input().split()
for j in score:
if j[0] == name:
j.append(int(gm))
for i in range(n):
name,gf = input().split()
for k in score:
if k[0] == name:
k.append(int(gf))
for i in score:
if len(i) == 4:
if i[3] >= i [2] and i[3] >= 60:
result.append([i[0],i[1],i[2],i[3],i[3]])
else:
g = i[2]*0.4+i[3]*0.6
if g - int(g) >= 0.5:
g = int(g)+1
else:
g = int(g)
if g >= 60:
result.append([i[0],i[1],i[2],i[3],g])
elif len(i) == 3:
if i[2] >= 60:
result.append([i[0],i[1],-1,i[2],i[2]])
result.sort()
result.sort(key=lambda x:x[4],reverse=True)
for i in result:
print(*i,sep=' ')