题解 | #成绩排序#

成绩排序

http://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

while True:
    try:
        n, order = int(input()), int(input())
        data = [] # 不能用字典保存,因为可能有名字是重复的
        for _ in range(n):
            name, score = input().split()
            data.append((name, int(score)))
        if order:
            for i in sorted(data, key=lambda x:x[1]): # 升序
                print(i[0], i[1])
        else:
            for i in sorted(data, key=lambda x:x[1], reverse=True): # 降序
                print(i[0], i[1])
    except:
        break
全部评论
名字重复也能用字典: while True: try: num = int(input()) n = int(input()) dic = {} for i in range(num): person = input().split() if int(person[1]) in dic: dic[int(person[1])].append(person[0]) else: dic[int(person[1])] = [person[0]] if n == 0: for i in sorted(dic,reverse=True): for j in dic[i]: print(j + ' ' + str(i)) else: for i in sorted(dic): for j in dic[i]: print(j + ' ' + str(i)) except: break
1 回复 分享
发布于 2021-12-31 19:26
题目说了不重复
点赞 回复 分享
发布于 04-29 00:03 湖北
order 不需要判断是0或者1吗,不懂
点赞 回复 分享
发布于 2023-06-24 11:55 广东
num = int(input()) sj = input() dict1 = dict() for i in range(num): name, total = input().split() dict1[i] = name + " " + total if sj == "0": key = sorted(dict1, key=lambda x: int(dict1[x].split()[1]), reverse=True) for i in key: print(dict1[i]) if sj == "1": key = sorted(dict1, key=lambda x: int(dict1[x].split()[1])) for i in key: print(dict1[i])
点赞 回复 分享
发布于 2022-05-10 23:04
想请教下,第7行 data.append((name, int(score))) 为啥不能换成data.append([name, int(score)]) 这个呢?
点赞 回复 分享
发布于 2022-03-24 18:02

相关推荐

评论
16
8
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务