Algorithm/BOJ

[백준 알고리즘/BOJ/C++] 10989 수 정렬하기 3

pinevienna 2021. 1. 26. 11:56

 

 

그냥 오름차순으로 정렬하는 문제다

그래서 그냥 내면 메모리초과로 오답이다

 

카운팅 정렬을 사용해서 문제를 풀어야한다

코드를 보면 이해가 빠르다

 

 

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
#include <iostream>
#include <algorithm>
using namespace std;
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    int t, n;
    cin >> t;
 
    int arr[10001= { 0, };
 
    for (int i = 0; i < t; i++) {
        cin >> n;
        arr[n] += 1;
    }
 
    for (int j = 1; j < 10001; j++) {
        if (arr[j] != 0) {
            for (int k = 0; k < arr[j]; k++)
                cout << j << "\n";
        }
    }
}
cs

 

배열을 이용하여 그 수를 몇 번이나 입력받았는지 카운트하여 카운트 된 만큼 차례대로 출력하면 된다