![[백준 / BOJ] 2108번 통계학 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FMKxtA%2FbtrJqq1Z9VF%2F1qj2xDlkA5F6IRvf9FcrUK%2Fimg.png)
[백준 / BOJ] 2108번 통계학 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 5. 31. 13:16
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/2108
2108번: 통계학
첫째 줄에 수의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 단, N은 홀수이다. 그 다음 N개의 줄에는 정수들이 주어진다. 입력되는 정수의 절댓값은 4,000을 넘지 않는다.
www.acmicpc.net
문제

문제 풀이
최빈값이 여러 개 있을 경우에 두 번째로 작은 값을 출력해야 해서 생각할 거리가 있었던 문제다.
C++ 상세 풀이
C++ 풀이 펼쳐보기
최빈값이 여러 개 있을 경우에 두 번째로 작은 값을 출력해야 해서 생각할 거리가 있었던 문제다.
숫자 범위가 -4000 ~ 4000이므로 최빈값을 계산할 벡터를 8001사이즈로 선언해주고, cnt[v[i] + 4000]++;로 최빈값을 카운트해준다. Line 27부터는 현재의 최빈값을 cntmax에 저장하고 해당 인덱스를 val에 저장한다. >=가 아닌 >이므로 가장 크기가 작은 최빈값이 저장되어있을 것이고, 두 번째로 작은 최빈값을 출력해야 하니 저장해놨던 인덱스 val + 1부터 다시 탐색한다.
for (int i = 0; i < N; ++i) {
cin >> v[i];
sum += v[i]; // 총합 계산
cnt[v[i] + 4000]++; // 최빈값 저장
}
sort(v.begin(), v.end());
// 최빈값 중 가장 작은 값
for (int i = 0; i <= 8000; ++i) {
if (cnt[i] > cntmax) {
cntmax = cnt[i];
val = i;
}
}
// 두 번째로 작은 최빈값을 구해야 한다.
// 이미 위에서 가장 작은 최빈값을 찾아놨으므로 val + 1부터 탐색한다.
for (int i = val + 1; i <= 8000; ++i) {
if (cnt[i] == cntmax) {
val = i;
break;
}
}
sum = round(double(sum) / N);
Python 상세 풀이
Python 풀이 펼쳐보기
파이썬에는 collections라는 좋은 모듈이 있기 때문에 활용해보았다.
cnt = Counter(arr).most_common(2)
print(round(sum(arr) / N)) # 산술평균
print(arr[(N - 1) / 2]) # 중앙값
if N > 1: # 최빈값
if cnt[0][1] == cnt[1][1]:
print(cnt[1][0])
else:
print(cnt[0][0])
else:
print(cnt[0][0])
print(max(arr) - min(arr)) # 최대 최소 차이
C++ 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <cmath> | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
cout.tie(nullptr); | |
int N, sum = 0, cntmax = 0, val = 0; | |
cin >> N; | |
vector<int> v(N); // 값을 저장할 벡터 | |
vector<int> cnt(8001); // 최빈값을 계산할 벡터 | |
for (int i = 0; i < N; ++i) { | |
cin >> v[i]; | |
sum += v[i]; // 총합 계산 | |
cnt[v[i] + 4000]++; // 최빈값 저장 | |
} | |
sort(v.begin(), v.end()); | |
// 최빈값 중 가장 작은 값 | |
for (int i = 0; i <= 8000; ++i) { | |
if (cnt[i] > cntmax) { | |
cntmax = cnt[i]; | |
val = i; | |
} | |
} | |
// 두 번째로 작은 최빈값을 구해야 한다. | |
// 이미 위에서 가장 작은 최빈값을 찾아놨으므로 val + 1부터 탐색한다. | |
for (int i = val + 1; i <= 8000; ++i) { | |
if (cnt[i] == cntmax) { | |
val = i; | |
break; | |
} | |
} | |
sum = round(double(sum) / N); | |
cout << sum << "\n"; // 산술평균, round(double(sum) / N)을 그대로 쓰면 -0으로 출력됨 | |
cout << v[(N - 1) / 2] << "\n"; // 중앙값 | |
cout << val - 4000 << "\n"; // 최빈값 | |
cout << v[N - 1] - v[0]; // 최대 최소 차이 | |
} |
Python 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
N = int(input()) | |
arr = [] | |
for _ in range(N): | |
num = int(input()) | |
arr.append(num) | |
arr.sort() | |
cnt = Counter(arr).most_common(2) | |
print(round(sum(arr) / N)) # 산술평균 | |
print(arr[(N - 1) / 2]) # 중앙값 | |
if N > 1: # 최빈값 | |
if cnt[0][1] == cnt[1][1]: | |
print(cnt[1][0]) | |
else: | |
print(cnt[0][0]) | |
else: | |
print(cnt[0][0]) | |
print(max(arr) - min(arr)) # 최대 최소 차이 |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 11650번 좌표 정렬하기 (C++, Python) (0) | 2022.05.31 |
---|---|
[백준 / BOJ] 1427번 소트인사이드 (C++, Python) (0) | 2022.05.31 |
[백준 / BOJ] 10989번 수 정렬하기 3 (C++, Python) (0) | 2022.04.14 |
[백준 / BOJ] 2751번 수 정렬하기 2 (C++, Python) (0) | 2022.04.14 |
[백준 / BOJ] 2750번 수 정렬하기 (C++, Python) (0) | 2022.04.13 |
@Reo :: 코드 아카이브
자기계발 블로그