题目
小明来到某学校当老师,需要将学生按考试总分或单科分数进行排名。
输入描述:
第一行输入学生人数n、科目数量m;
0<n<100; 0<m<10
第二行输入m个科目名称,以空格分开;科目只由英文字母构成,长度不超过10个字符,无重复的科目名称,且与后续输入的学生成绩一一对应;
第三行开始的n行,每行包含一个学生的姓名和该生m个科目的成绩,学生名只由英文字符构成,长度不超过10,且无重复;成绩在【0,100】的整数;
最后一行,输入排序的(单个)科目名称,若无科目名称,则按照总分排序;
输出描述:
按成绩排序的学生名字,空格分隔,成绩相同按名字字典序输出
示例1
输入:
3 2
yuwen shuxue
fangfang 95 90
xiaohua 88 95
minmin 100 82
shuxue
输出:
xiaohua fangfang minmin
说明:
按shuxue成绩排名,依次是xiaohua、fangfang、minmin
解题代码
python
class Project:
def __init__(self, id, scores):
self.name = name
self.scores = scores
self.idx = -1
self.total_score = sum(scores)
def comp(a,b):
if(a.idx != -1):
if(a.scores[idx] != b.scores[idx]):
if(a.scores[idx] > b.scores[idx]):
return -1
else:
return 1
else:
if (a.name > b.name):
return 1
else:
return -1
else:
if(a.total_score != b.total_score):
if(a.total_score > b.total_score):
return -1
else:
return 1
else:
if (a.name > b.name):
return 1
else:
return -1
params1 = [int(x) for x in input().split(" ")]
n = params1[0]
m = params1[0]
all_project = []
all_study = input().split(" ")
for i in range(n):
params = input().split(" ")
name = params[0]
j=1
scores = []
while(True):
if(j>=m):
break
else:
temp_score = int(params[j])
scores.append(temp_score)
j+=1
all_project.append(Project(name, scores))
target_str = input()
idx = -1
for i in range(len(all_study)):
if(target_str == all_study[i]):
idx = i
break
for i in all_project:
i.idx = idx
#按照分数
all_project = sorted(all_project, key=functools.cmp_to_key(comp))
output_str = " "
for i in all_project:
output_str += i.name + " "
print(output_str)