목차

통계학

ps
링크acmicpc.net/…
출처BOJ
문제 번호2108
문제명통계학
레벨실버 4
분류

기초

시간복잡도O(nlogn)
인풋사이즈n<=500,000
사용한 언어Python
제출기록50552KB / 512ms
최고기록228ms
해결날짜2021/08/24

풀이

코드

코드 1 - statistics 모듈 사용

"""Solution code for "BOJ 2108. 통계학".

- Problem link: https://www.acmicpc.net/problem/2108
- Solution link: http://www.teferi.net/ps/problems/boj/2108
"""

import statistics
import sys


def main():
    N = int(sys.stdin.readline())
    nums = [int(sys.stdin.readline()) for _ in range(N)]

    print(round(statistics.mean(nums)))
    print(statistics.median(nums))
    modes = statistics.multimode(nums)
    if len(modes) == 1:
        print(modes[0])
    else:
        print(sorted(modes)[1])
    print(max(nums) - min(nums))


if __name__ == '__main__':
    main()

코드 2 - 그냥 직접 정렬해서 구현

"""Solution code for "BOJ 2108. 통계학".

- Problem link: https://www.acmicpc.net/problem/2108
- Solution link: http://www.teferi.net/ps/problems/boj/2108
"""

import itertools
import sys


def main():
    N = int(sys.stdin.readline())
    nums = [int(sys.stdin.readline()) for _ in range(N)]
    nums.sort()
    
    print(round(sum(nums) / N))
    print(nums[N // 2])    
    mode, count, is_first = 0, 0, False
    for k, g in itertools.groupby(nums):
        l = len(list(g))
        if l > count:
            mode, count, is_first = k, l, True
        elif l == count and is_first:
            mode, count, is_first = k, l, False    
    print(mode)    
    print(nums[-1] - nums[0])


if __name__ == '__main__':
    main()