내가 작성한 코드

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 longint> 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의 처음부터 마지막 값까지 비교하며 가장 많이 입력된 값을 저장하여 출력했다.

코드 채점 결과

+ Recent posts