记录华为OD机试题2021-03-22
问题:只能描述个大概
《给运动员打分》(一星题)
第一行输入两个整数m,n m代表评委的个数,且2<=m<=10,n代表参加的运动员数量3<=n<=100,用英文逗号隔开,如:4,5
随后输入是各个评委的打分,分数最低1分,最高10分,用英文逗号隔开,如,9,9,8,9,6
输出要求:
请输出总分最高的前三名的运动员序号用英文逗号分隔,如果总分相同,则比较运动员获得的最高分较多的排名靠前(输入用例不会出现完全相同的打分分数)
输入用例会有非法输入,此时输出 -1
常规输入例如:
4,5
8,10,8,8,7
10,9,9,6,7
9,9,7,8,9
10,9,8,9,8
表示4名裁判,5名运动员。第一组代表裁判给每个运动员的打分,第一个分数,表示序号为1的运动员分数,依次类推
输出:1,2,3
非法输入:
3,2
8,9
8,9
8,9
输出:-1 (运动员数量为2,小于规定数量)
非法输入:
4,5
8,11,8,8,7
10,9,9,6,7
9,9,7,8,9
10,9,8,9,8
输出:-1(最高分超过10分)
=============================题目分割线===================================
# 4,5 # 9,8,9,8,10 # 8,9,9,8,10 # 9,10,9,6,10 # 7,9,9,8,6 def compare_score(A, B, score_list): # A,B为运动员序号-1 scoreA = [ac[A] for ac in score_list] scoreB = [ac[B] for ac in score_list] scoreA.sort(reverse=True) # 降序 scoreB.sort(reverse=True) # 降序 totalA = sum(scoreA) totalB = sum(scoreB) if totalA > totalB: return A elif totalA < totalB: return B else: # 比较最高分,每个运动员有m个得分,比较排序后每个分数的大小,相同即继续对比,题目提示不可能会全部一样,所以不考虑全部一样的情况 for index in range(int(m)): if scoreA[index] - scoreB[index] > 0: return A elif scoreA[index] - scoreB[index] < 0: return B else: continue def check_variable(score_list): for li in score_list: for l in li: if int(l) > 10 or int(l) < 1: return False return True try: m, n = input().split(',') score_list = [] flag = 1 if 2 <= int(m) <= 10 and 3 <= int(n) <= 100: for i in range(int(m)): score = input().split(',') score_list.append([int(s) for s in score]) flag = check_variable(score_list) # 检查分数合法性 if flag: namelist = [] for loop in range(3): # 循环三次,找三个最大,分别是1,2,3名 # 找第一名 A = 0 for j in range(int(n) - 1): if int(A)+1 in namelist or j + 2 in namelist: # 对比的运动员有一个已经被挑出最大,不参与对比 continue big = compare_score(A, j + 1, score_list) A = big namelist.append(int(A)+1) # 运动员编号比索引值大1 print(','.join(namelist)) else: print(-1) else: print(-1) except: print(-1)