편집 시간: 2022년 2월 7일 오후 8:36

코드

Algorithm/10814.py at main · Junroot/Algorithm

풀이

입력된 순서를 데이터로 함께 저장하고 있으면 정렬하기 쉽다. 이 때, 입력으로 받은 age 가 문자열인지 숫자인지 확인할 필요가 있다.

다른 사람 풀이

딕셔너리를 이용해서 푸는 방법도 있다. 키를 나이로 하는 딕셔너리에 입력순으로 추가하면 순서를 보장한채로 정렬이 가능하다.

import sys
input = sys.stdin.readline
print = sys.stdout.write

N = int(input())
people = {}
for n in range(N):
    age, name = map(str, input().split())
    if int(age) in people:
        people[int(age)].append(name)
    else:
        people[int(age)] = [name]
temp = sorted(people)
for ageNum in temp:
    for person in people[ageNum]:
        print(str(ageNum) + " " + person + "\n")