내가 작성한 코드
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
|
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
int main(void) {
int N;
int max = 0;
int cnt = 0;
long long tmp;
map <long long, int> arr;
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%lld", &tmp);
arr[tmp]++;
}
for (auto i = arr.begin(); i != arr.end(); i++) {
if (i->second > max) {
max = i->second;
tmp = i->first;
}
}
printf("%lld", tmp);
}
|
cs |
- map을 사용하여 풀 수 있었다.
- 범위가 매우 넓었고 순서도 뒤죽박죽에 음수 값도 넣어야 했기에 기존에 사용하던 방식으로는 해결할 수 없었다.
- map을 사용하면 first값에 key, second값에 value를 넣어 first값에 따라 알아서 정리가 되었다.
- 그 후에 map의 처음부터 마지막 값까지 비교하며 가장 많이 입력된 값을 저장하여 출력했다.
코드 채점 결과
'BAEKJOON' 카테고리의 다른 글
[BaekJoon]11004번 - K번째 수(정렬) (0) | 2019.08.02 |
---|---|
[BaekJoon]2156번 - 포도주 시식(점화식 dp) (0) | 2019.08.02 |
[BaekJoon]10989번 - 수 정렬하기 3 (0) | 2019.08.01 |
[BaekJoon]9465번 - 스티커 (0) | 2019.07.31 |
[BaekJoon]2193번 - 이친수 (0) | 2019.07.30 |