🧩PS/🥈Nomal
[JAVA] 백준 10989번 - 수 정렬하기 3 (Counting Sort)
Cocoon_
2021. 10. 7. 03:11
반응형
<코드>
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
int N = Integer.parseInt(br.readLine());
int count[] = new int[10001];
for (int i = 0; i < N; i++) {
count[Integer.parseInt(br.readLine())]++;
}
for (int i = 1; i <= 10000; i++) {
for (int j = 0; j < count[i]; j++) {
sb.append(i+"\n");
}
}
System.out.println(sb);
}
}
풀이 방법
입력되는 수의 범위가 1~10000이므로 Counting Sort (카운팅 정렬)로 풀이를 하였습니다.
https://www.acmicpc.net/problem/10989
10989번: 수 정렬하기 3
첫째 줄에 수의 개수 N(1 ≤ N ≤ 10,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 수가 주어진다. 이 수는 10,000보다 작거나 같은 자연수이다.
www.acmicpc.net
반응형