Answer the question
In order to leave comments, you need to log in
How to create a code that counts the results of the Olympiad?
Results of the Olympiad
During the Olympiad, each of the participants received their identification number - a natural number. It is necessary to sort the list of participants in the Olympiad by the number of points they scored from high to low points, and in case of equality of points - in ascending order of identification numbers. Do not use built-in sorting algorithms.
Specifications Input
The first line contains a number N(1≤N≤1000) — the number of participants. Each following line gives the identification number and score of the respective participant. All numbers in the input file do not exceed 105.
Output
In the output file, output the original list in descending order of scores. If some participants have the same scores, then they should be displayed among themselves in ascending order of identification numbers.
Answer the question
In order to leave comments, you need to log in
N = int(input()) #кол-во участников
list = [] #пустой список
#Добавляем id и баллы в списки
for i in range(N):
id = int(input())
ball = int(input())
list.append([id,ball]) #Добавление в список значений в еще 1 списке
print(list) #Для наглядности как выглядит список,
# содержащий список каждого участника (id и баллы)
#сортировка методом пузырька, можно и любые другие
flag = True
while flag: #делаем бесконечный цикл, пока все не отсортируется
flag = False #если все сортировано, цикл прекращается
for i in range(len(list)-1):
# Проверяем соседние места и меняем их
if list[i][1] > list[i+1][1]:
list[i], list[i+1] = list[i+1], list[i]
#можно писать по-другому, если более понятнее:
# a = list[i]
# list[i] = list[i+1]
# list[i+1] = a
flag = True
elif list[i][1] == list[i+1][1]: #Если совпадают баллы сравниваем id
if list[i][0] > list[i+1][0]:
list[i], list[i + 1] = list[i + 1], list[i]
print(list)
#если нужны только баллы то делаем след:
balls = []
for ball in list:
balls.append(ball[1])
print(balls)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question