[Python] 백준 25325번 : 학생 인기도 측정

2025. 4. 18. 18:20

99클럽 코테 스터디 | 비기너 | 15일차



접근 방법

1. student라고 하는 dictionary 안에 입력 받은 n명의 학생 이름을 key 값으로 세팅한다.

2. n명의 학생들로부터 받은 좋아하는 학생 이름에 대하여 student dict 안에 key가 있으면 value를 하나씩 더한다.

3. student dict를 정렬한다.

4. student의 key, value를 순서대로 프린트한다.

 

import sys

n = sys.stdin.readline()

names = sys.stdin.readline().split()

# set student dict
student = {}
for name in names:
    student.setdefault(name, 0)

# vote popularity of each student
for _ in range(int(n)):
    popular_students = sys.stdin.readline().split()
    for p_s in popular_students:
        if p_s in student.keys():
            student[p_s] += 1

# sort student dict
sorted_student = dict(sorted(student.items(), key=lambda item: item[1], reverse=True))

# print the result
for s, count in sorted_student.items():
    print(s, count)

BELATED ARTICLES

more