https://www.acmicpc.net/problem/2108
2108번: 통계학
첫째 줄에 수의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 단, N은 홀수이다. 그 다음 N개의 줄에는 정수들이 주어진다. 입력되는 정수의 절댓값은 4,000을 넘지 않는다.
www.acmicpc.net
최빈값을 어떻게 구할 것인가?
정수의 절대값이 4000을 넘지 않는다.
그럼 count[8001]을 만들고
음수가 나오면 +4000을 하면 되겠다!
최빈값이 여러 개 일 수도 있으므로
check를 사용
반올림을 해주는 함수 -> round 함수
#include <cmath>
나누기를 할 떄는 double
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<int> arr;
arr.resize(n);
// 정수의 절대값이 4000이므로
int count[8001] = { 0 };
double sum = 0;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
sum += arr[i];
count[arr[i] + 4000]++;
}
sort(arr.begin(), arr.end());
int countMax = -987654321;
int countAns = 0;
// 최빈값이 여러개 일 때
int check = 0;
for (int i = 0; i < 8001; i++)
{
if (countMax < count[i])
{
countAns = i - 4000;
countMax = count[i];
check = 1;
}
else if (count[i] == countMax && check == 1)
{
countAns = i - 4000;
check = 0;
}
}
cout << (int)round(sum / n) << '\n' << arr[n / 2] << '\n' << countAns
<< '\n' << arr[n - 1] - arr[0];
return 0;
}
|
cs |