문제 링크입니다: https://www.acmicpc.net/problem/2592
2592번: 대표값
어떤 수들이 있을 때, 그 수들을 대표하는 값으로 가장 흔하게 쓰이는 것은 평균이다. 평균은 주어진 모든 수의 합을 수의 개수로 나눈 것이다. 예를 들어 10, 40, 30, 60, 30, 20, 60, 30, 40, 50의 평균은 이 된다. 평균 이외의 또 다른 대표값으로 최빈값이라는 것이 있다. 최빈값은 주어진 수들 가운데 가장 많이 나타나는 수이다. 예를 들어 10, 40, 30, 60, 30, 20, 60, 30, 40, 50 이 주어질 경우, 3
www.acmicpc.net
저 같은 경우 최빈값을 구할 때 pair<int, int>형 배열을 선언하여 구했지만 더 쉬운 방법이 있을 것이라고 판단됩니다.
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 <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
const int MAX = 1000 + 1; | |
pair<int, int> frequency[MAX]; | |
bool cmp(pair<int, int> a, pair<int, int> b) | |
{ | |
return a.first > b.first; | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
for (int i = 10; i < MAX; i += 10) | |
{ | |
frequency[i].second = i; | |
} | |
int average = 0; | |
int mostFrequent = 0; | |
vector<int> v(10); | |
for (int i = 0; i < 10; i++) | |
{ | |
cin >> v[i]; | |
average += v[i]; | |
frequency[v[i]].first++; | |
} | |
cout << average / 10 << "\n"; | |
sort(frequency, frequency + MAX, cmp); | |
cout << frequency[0].second << "\n"; | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 16500번 문자열 판별 (0) | 2020.05.14 |
---|---|
백준 1059번 수2 (0) | 2020.05.08 |
백준 2581번 소수 (0) | 2020.05.03 |
백준 2476번 주사위 게임 (0) | 2020.05.01 |
백준 17779번 게리맨더링 2 (2) | 2020.04.30 |