Algorithm/Problem Solving 2020. 6. 1. 21:36

(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)

 

문제 링크

- https://www.acmicpc.net/problem/11652

 

사용 알고리즘

- 정렬

 

풀이

- 정렬 후 앞에서부터 인접한 수를 비교하며 개수를 셈

 

소스 코드

- https://github.com/moomini/algorithm/blob/master/boj/11652.cpp

#include <cstdio>
#include <algorithm>
 
using namespace std;
 
long long arr[100003];
 
int main(void) {
    int n; scanf("%d"&n);
    for (int i = 0; i < n; ++i) scanf("%lld"&arr[i]);
    sort(arr, arr + n);
    int ansCount = 1, tempCount = 1;
    long long ans = arr[0];
    for (int i = 1; i < n; ++i) {
        if (arr[i] == arr[i - 1]) {
            ++tempCount;
        }
        else {
            if (ansCount < tempCount) {
                ans = arr[i - 1];
                ansCount = tempCount;
            }
            tempCount = 1;
        }
    }
    if (ansCount < tempCount) ans = arr[n - 1];
    printf("%lld\n", ans);
    return 0;
}
cs

'Algorithm > Problem Solving' 카테고리의 다른 글

[BOJ/1377] 버블 소트  (0) 2020.06.03
[BOJ/11004] K번째 수  (0) 2020.06.02
[BOJ/10989] 수 정렬하기 3  (0) 2020.05.26
[BOJ/10825] 국영수  (0) 2020.05.24
[BOJ/10814] 나이순 정렬  (0) 2020.05.22
posted by DevMoomin
: