题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
# 名字和分数分别记录,对分数进行冒泡排序,同时更改姓名的排序,这样不会对相同分数的人进行操作 n = int(input()) flag = int(input()) name = [] score = [] for i in range(n): line = input().split() name.append(line[0]) score.append(int(line[1])) # 按照sore冒泡排序,这样不会改变录入顺序 if flag == 0: # 从高到底 for i in range(n - 1, 0, -1): for j in range(n - 1, n - i - 1, -1): if score[j] > score[j - 1]: score[j], score[j - 1] = score[j - 1], score[j] name[j], name[j - 1] = name[j - 1], name[j] elif flag == 1: for i in range(n - 1): for j in range(0, n - 1 -i): if score[j] > score[j + 1]: score[j], score[j + 1] = score[j + 1], score[j] name[j], name[j + 1] = name[j + 1], name[j] for i in range(n): print(name[i] + ' ' + str(score[i]))