2108번: 통계학
첫째 줄에 수의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 단, N은 홀수이다. 그 다음 N개의 줄에는 정수들이 주어진다. 입력되는 정수의 절댓값은 4,000을 넘지 않는다.
www.acmicpc.net
알고리즘 분류
- 구현
- 정렬
SOLUTION
import sys
from collections import Counter
num = [int(sys.stdin.readline()) for _ in range(int(sys.stdin.readline()))]
tmp = {}
num.sort()
print(round(sum(num)/len(num))) # 산술평균 소수점 이하 첫째 자리에서 반올림한 값 round()함수를 이용
print(num[len(num)//2]) # 중앙값
tmp = Counter(num).most_common() # Counter(data).most_common() 데이터의 개수가 많은 순으로 정렬 해줌
if len(tmp) > 1:
if tmp[0][1] == tmp[1][1]: # 최빈값이 있을 때 최빈값 중 두번째로 작은 값 출력
print(tmp[1][0])
else:
print(tmp[0][0])
else:
print(tmp[0][0])
print(num[-1]-num[0]) # 범위는 가장 큰 값에서 작은 값을 뺀 것
※ class collections.Counter()
collections — Container datatypes — Python 3.10.0 documentation
collections — Container datatypes Source code: Lib/collections/__init__.py This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple. namedtuple() factory f
docs.python.org
※ round(number[, ndigits])
number를 소수점 다음에 ndigits 정밀도로 반올림한 값을 돌려줍니다. ndigits 가 생략되거나 None 이면, 입력에 가장 가까운 정수를 돌려줍니다.
※ Counter().most_common()
데이터의 개수가 많은 순으로 정렬된 배열을 리턴한다
ex) print(Counter('super effective').most_common())
= [('e', 4), ('f', 2), ('s', 1), ('u', 1), ('p', 1), ('r', 1), (' ', 1), ('c', 1), ('t', 1), ('i', 1), ('v', 1)]
'코딩테스트 대비 > BOJ' 카테고리의 다른 글
[Baekjoon/Python] 10773번: 제로 - 효과는 굉장했다! (0) | 2021.10.30 |
---|---|
[Baekjoon/Python] 9012번: 괄호 - 효과는 굉장했다! (0) | 2021.10.29 |
[Baekjoon/Python] 1978번: 소수 찾기 - 효과는 굉장했다! (0) | 2021.10.26 |
[Baekjoon/Python] 1920번: 수 찾기 - 효과는 굉장했다! (0) | 2021.10.26 |
[Baekjoon/Python] 11651번: 좌표 정렬하기2 - 효과는 굉장했다! (0) | 2021.10.26 |