[백준 2108번 C/C++] 통계학
[백준 2108번 C/C++] 통계학
https://www.acmicpc.net/problem/2108
해결전략
코드
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
bool Comp(const pair<int, int>& a, const pair<int, int>& b) {
if (a.second == b.second)
return a.first < b.first;
else
return a.second > b.second;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, input, maxNum = -4001, minNum = 4001;
double sum = 0;
cin >> n;
vector<int> v(n);
map<int, int> myMap;
for (int i = 0; i < n; i++) {
cin >> input;
v[i] = input;
sum += input;
myMap[input]++;
if (maxNum < v[i]) maxNum = v[i];
if (minNum > v[i]) minNum = v[i];
}
//산술평균
if (round(sum / n) == 0)
cout << "0" << "\n";
else
cout << round(sum / n) << "\n";
//중앙값
sort(v.begin(), v.end());
cout << v[n / 2] << "\n";
//최빈값
vector<pair<int, int>> vec(myMap.begin(), myMap.end());
sort(vec.begin(), vec.end(), Comp);
if (n > 1 && vec[0].second == vec[1].second)
cout << vec[1].first << "\n";
else
cout << vec[0].first << "\n";
//범위
cout << maxNum - minNum << "\n";
return 0;
}
'⭐ 코딩테스트 > 백준' 카테고리의 다른 글
[백준 14500번 C/C++] 테트로미노 (0) | 2023.07.10 |
---|---|
[백준 20920번 C/C++] 영단어 암기는 괴로워 (0) | 2023.07.09 |
[백준 1916번 C/C++] 최소비용 구하기 (0) | 2023.07.07 |
[백준 2156번 C/C++] 포도주 시식 (0) | 2023.07.06 |
[백준 1780번 C/C++] 종이의 개수 (0) | 2023.07.05 |
댓글
이 글 공유하기
다른 글
-
[백준 14500번 C/C++] 테트로미노
[백준 14500번 C/C++] 테트로미노
2023.07.10 -
[백준 20920번 C/C++] 영단어 암기는 괴로워
[백준 20920번 C/C++] 영단어 암기는 괴로워
2023.07.09 -
[백준 1916번 C/C++] 최소비용 구하기
[백준 1916번 C/C++] 최소비용 구하기
2023.07.07 -
[백준 2156번 C/C++] 포도주 시식
[백준 2156번 C/C++] 포도주 시식
2023.07.06