[백준] 2108 통계학 C++알고리즘/백준2026. 1. 22. 13:05
Table of Contents

후기 및 풀이
최빈값을 찾는 부분이 까다로운 문제인듯하다.
산술평균은 round함수를 써서 처리하고,
중앙값은 문제를 읽으면 N은 소수라 했으므로 배열을 정렬하고 size() / 2를 통해서 바로 찾을 수 있고
범위의 경우 정렬한 배열 back() - front를 통해 찾을 수 있다.
최빈값을 찾는 방법은 다양한데 본인 풀이와 다른 사람의 풀이 두개다 코드를 올리도록 하겠다.
본인은 map에 최빈값을 담아두고 이를 순회하면서 최빈값들을 vector에 담아두고 vector의 사이즈를 확인해서 최빈값이 여러개인지 하나만 있는지를 확인했는데 플래그를 통해 최빈값을 걸러내는 방법도 있다.
코드
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
#include <stack>
#include <sstream>
#include <set>
#include <unordered_set>
#include <math.h>
using namespace std;
using ll = long long;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n; cin >> n;
vector<ll> v;
map<int, int> cnt;
for (int i = 0; i < n; ++i)
{
int a; cin >> a;
v.push_back(a);
cnt[a]++;
}
sort(v.begin(), v.end());
ll ret1 = 0;
for (int i = 0; i < v.size(); ++i)
{
ret1 += v[i];
}
double retTmp = double(ret1) / v.size();
ret1 = (std::round(retTmp));
cout << ret1 << endl; // 산술평균
cout << v[v.size() / 2] << endl; // 중앙값
// 최빈값
int _max = 0;
for (auto& pair : cnt)
{
if (pair.second > _max)
_max = pair.second;
}
vector<int> ret3;
for (auto& pair : cnt)
{
if (_max == pair.second)
ret3.push_back(pair.first);
}
if (ret3.size() >= 2)
{
cout << ret3[1] << endl;
}
else
{
cout << ret3[0] << endl;
}
// 범위
cout << v[v.size() - 1] - *v.begin() << endl;
return 0;
}
아래는 참고한 다른 풀이이다.
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
#include <stack>
#include <sstream>
#include <set>
#include <unordered_set>
#include <cmath>
using namespace std;
using ll = long long;
vector<int> arr;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int num, tmp, range, middle = 0, most_val, mean = 0;
int most = -9999;
int number[8001] = { 0, };
bool not_first = false;
cin >> num;
for (int i = 0; i < num; ++i)
{
cin >> tmp;
arr.push_back(tmp);
mean += tmp;
number[tmp + 4000]++;
}
sort(arr.begin(), arr.end());
for (int i = 0; i < 8001; ++i)
{
if (number[i] == 0)
continue;
if (number[i] == most)
{
if (not_first)
{
most_val = i - 4000;
not_first = false;
}
}
if (number[i] > most)
{
most = number[i];
most_val = i - 4000;
not_first = true;
}
}
middle = arr[arr.size() / 2];
mean = round(float(mean) / num);
range = arr.back() - arr.front();
cout << mean << "\n" << middle << "\n" << most_val << "\n" << range;
return 0;
}
코드에서 눈여겨 볼 점은 최빈값을 어떻게 찾는지가 핵심이다.
만약 -1 -1 1 1 2 2 로 입력이 들어온 경우를 생각해보면 처음 if (number[i] > most)에 걸려서 최빈값을 갱신하고 플래그(not_first)를 true로 변경한다. 그다음 1이 들어오는 경우 if (number[i] == most)에 걸려 플래그를 false로 뒤집고 두번째로 작은 최빈값을 갱신한다. 이후 2가 들어오더라도 플래그가 false이기 때문에 최빈값을 갱신하지 않게 된다.
(만약 -1 -1 1 1 2 2 3 3 3 인경우 당연히 if (number[i] > most)에 걸려서 다시 최빈값을 갱신하는 로직이다.)
위코드가 더 깔끔한거 같아 코드를 보고 흐름을 이해하는데 집중했다.
참고
'알고리즘 > 백준' 카테고리의 다른 글
| [백준] 1063 킹 C++ (0) | 2026.01.23 |
|---|---|
| [백준] 1244 스위치 켜고 끄기 C++ (0) | 2026.01.22 |
| [백준] 3048 개미 C++ (0) | 2026.01.21 |
| [백준] 1182 부분수열의 합 C++ (0) | 2025.05.05 |
| [백준] 인구이동 16234 C++ (0) | 2025.04.29 |
@CGNY :: 김놀자
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!