题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1?tpId=40&tqId=21333&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan&difficulty=&judgeStatus=&tags=/question-ranking

class Student:
    def __init__(self, id: int, name: str = None, score: int = 0):
        self.name = name
        self.score = score
        self.id = id

try :

    while True:
        count = int(input())
        sortFunc = int(input())
        students = []
        for i in range(count):
            name, score = input().split()
            score = int(score)
            students.append(Student(i, name, score))
        if sortFunc == 0:
            sortedStudents = sorted(students, key=lambda s: (-s.score, s.id))
        else:
            sortedStudents = sorted(students, key=lambda s: (s.score, s.id))
        for student in sortedStudents:
            print("{} {}".format(student.name, student.score))
except EOFError:
    print()

设计一个学生类,除了姓名和成绩属性之外,再加上一个id属性,判断先后顺序,排序时只需要先比较成绩再比较先后顺序即可。

全部评论

相关推荐

牛客771574427号:恭喜你,华杰
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务