자바생
728x90

풀이 방법

앞의 수 정렬하기 2와 매우 유사한 문제이다. 다른 점이 있다면 중복되는 숫자가 있다. 그래서 count배열을 이용할 때 boolean을 사용할 수 없고, int형을 사용하여 해당 숫자를 index, 숫자 개수를 해당 index 값으로 입력받는다. 그래서 해당 index의 값이 0이 될 때까지 index를 출력하게 된다.

 

사용 개념

카운팅 정렬

 

코드

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
30
31
32
33
34
package Sort;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class BOJ10989 {
    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        StringBuilder sb = new StringBuilder();
 
        int test = Integer.parseInt(st.nextToken());
 
        int count[] = new int[10001];
 
        for(int i = 0; i < test; i++){
            st = new StringTokenizer(br.readLine());
            count[Integer.parseInt(st.nextToken())]++;
        }
 
        for(int i = 1; i < count.length; i++){
            int limit = count[i];
            while(limit > 0){
                sb.append(i + "\n");
                limit--;
            }
        }
 
        System.out.println(sb);
    }
}
 
cs
728x90

'BOJ(Java)' 카테고리의 다른 글

자바(백준) 10845 큐  (0) 2021.04.04
자바(백준) 10828 스택  (0) 2021.04.04
자바(백준) 2751 수 정렬하기 2  (0) 2021.03.31
자바(백준) 1145 적어도 대부분의 배수  (0) 2021.02.01
자바(백준) 4641 Doubles  (0) 2021.02.01
profile

자바생

@자바생

틀린 부분이 있다면 댓글 부탁드립니다~😀

검색 태그