백준 2108: 통계학
2023. 1. 31. 02:11ㆍ개인공부/코딩테스트
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
int main(void)
{
cout << fixed;
cout.precision(0);
int NumberofNumber = 0;
cin >> NumberofNumber;
double Total = 0;
map<int, int > NumberMap;
// 숫자 입력
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 0; i < NumberofNumber; ++i)
{
int GetNumber = 0;
cin >> GetNumber;
Total += GetNumber;
if (NumberMap.find(GetNumber) == NumberMap.end())
{
NumberMap.insert(pair<int, int>(GetNumber, 1));
}
else
{
NumberMap.at(GetNumber) += 1;
}
pq.push(GetNumber);
}
// 산술 평균
int Average = round(Total / NumberofNumber);
if (Average == -0)
Average = 0;
cout << Average << "\n";
// 중앙값
int Max = 0;
int Minimum = 0;
int Middle = 0;
for (int i = 0; i < NumberofNumber; ++i)
{
int top = pq.top();
if (i == 0)
Minimum = top;
if (i == NumberofNumber - 1)
Max = top;
if (i == ((NumberofNumber + 1) / 2) - 1)
Middle = top;
pq.pop();
}
cout << Middle << "\n";
// 최빈값
int MostNumber = 0;
int CheckNumber = 0;
bool IsOverlap = false;
for (auto iter = NumberMap.begin(); iter != NumberMap.end(); ++iter)
{
if (iter->second > CheckNumber)
{
MostNumber = iter->first;
CheckNumber = iter->second;
IsOverlap = false;
}
else if (iter->second == CheckNumber && IsOverlap == false)
{
MostNumber = iter->first;
CheckNumber = iter->second;
IsOverlap = true;
}
}
cout << MostNumber << '\n';
// (범위 최댓값과 최솟값 차이)
cout << Max - Minimum << "\n";
return 0;
}
|
cs |
'개인공부 > 코딩테스트' 카테고리의 다른 글
백준 2231: 분해합 (0) | 2023.01.31 |
---|---|
백준 2798: 블랙잭 (0) | 2023.01.31 |
백준 10814: 나이순 정렬 (0) | 2023.01.30 |
백준 1181: 단어 정렬 (0) | 2023.01.30 |
백준 2738번: 행렬 덧셈 (0) | 2023.01.30 |