[백준/Python] 2592 - 대표값

orangesnail·2025년 3월 18일

백준

목록 보기
75/169

https://www.acmicpc.net/problem/2592


statistics 라이브러리를 사용하면 최빈값을 쉽게 구할 수 있다!

import statistics

nums = []

for _ in range(10):
    num = int(input())
    nums.append(num)

avg = sum(nums) // 10
mode = statistics.mode(nums)

print(avg)
print(mode)

라이브러리 없이 최빈값을 직접 구현해서 구하는 버전의 코드는 아래와 같다.

nums = []

for _ in range(10):
    num = int(input())
    nums.append(num)

avg = sum(nums) // 10

freq = {}
for num in nums:
    if num in freq:
        freq[num] += 1
    else:
        freq[num] = 1

max_count = max(freq.values())

for key in freq:
    if freq[key] == max_count:
        mode = key
        break
    
print(avg)
print(mode)

엄청 복잡해진다...

profile
초보입니다. 피드백 환영합니다 😗

0개의 댓글