728x90
문제
N개의 수가 주어졌을 때, 이를 오름차순으로 정렬하는 프로그램을 작성하시오.
입력
첫째 줄에 수의 개수 N(1 ≤ N ≤ 10,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 수가 주어진다. 이 수는 10,000보다 작거나 같은 자연수이다.
출력
첫째 줄부터 N개의 줄에 오름차순으로 정렬한 결과를 한 줄에 하나씩 출력한다.
계수 정렬을 이용하는 문제이다. 처음에 입력 범위가 10,000인 걸 모르고 에러 남..하하
package BOJ0726;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class BOJ_10989_수정렬3 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
int[] count = new int[10001];
for (int i = 0; i < N; i++) {
count[arr[i]]++;
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for (int i = 0; i < 10001; i++) {
for (int j = 0; j < count[i]; j++) {
bw.write(Integer.toString(i) + "\n");
}
}
bw.close();
}
}
728x90
'문제 풀이 > BOJ' 카테고리의 다른 글
[백준] 2869 - 달팽이는 올라가고 싶다 (자바/Java) (0) | 2022.07.26 |
---|---|
[백준] 1316 - 그룹 단어 체커 (자바/Java) (0) | 2022.07.26 |
[백준] 2750 - 수 정렬하기: (자바/Java) 선택 정렬(Selection Sort) (0) | 2022.07.26 |
[백준] 11651 - 좌표 정렬하기 2 (자바/Java) : Merge Sort (0) | 2022.07.26 |
[백준] 1427 - 소트인사이드 (자바/Java) : 계수 정렬(Counting Sort) (0) | 2022.07.26 |