본문 바로가기
코딩테스트/백준

Java - 백준 코딩테스트 4673번 [함수 - 셀프 넘버]

by sycareer 2021. 6. 24.

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
35
36
37
38
39
40
41
42
43
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
 
public class SelfNumber {
 
    public static void main(String[] args) {
        // 20210623 - 4673번
        //10000보다 같거나 작은 셀프 넘버 한 줄에 하나씩 출력
        //////////////////////////////////////////////////////////////////////////이해 못함
        
        boolean[] check = new boolean[10001];
        
        for (int i = 1; i < 10001; i++) {
            int n = d(i);
            
            if (n < 10001) {
                check[n] = true;
            }
        }
        
        StringBuilder sb = new StringBuilder();
        
        for (int i = 1; i < 10001; i++) {
            if (!check[i]) {
                sb.append(i + "\n");
            }
        }
        System.out.println(sb);
    }
    
    
    public static int d(int number) {
        int sum = number;
        
        while (number != 0) {
            sum += (number % 10);
            number /= 10;
        }
        
        return sum;
    }
}
cs