DHistory

[Baekjoon] Sort - 나이순 정렬 본문

Computer Science/Algorithm

[Baekjoon] Sort - 나이순 정렬

ddu0422 2023. 9. 8. 14:46

문제

 

10814번: 나이순 정렬

온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을

www.acmicpc.net

 

풀이

import sys

n = int(sys.stdin.readline().rstrip())
people = []
for i in range(n):
    age, name = sys.stdin.readline().rstrip().split()
    people.append([int(age), name, i])


def solution(people):
    return sorted(people, key=lambda x: (x[0], x[2]))


for age, name, _ in solution(people):
    print(age, name, end='\n')

 

채점 결과