<접근 방법>

1. 문제에서 정의한 d(n) 함수를 C++ 함수로 구현

2. 위의 함수에 n을 1부터 9999까지 초기 시드로 넣고 2-1을 진행한다.

2-1. d(x)가 10000보다 작은 동안 n, d(n), d(d(n)), ... 을 반복 진행한다.

3. 초기값을 제외하고 과정 2에서 나오는 숫자들은 셀프 넘버가 아니므로 셀프 넘버에서 제외시킨다.

4. 위의 과정이 모두 끝난 후, 셀프 넘버인 것들을 차례대로 출력한다.


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
#include <iostream>
 
using namespace std;
 
int d(int n) {
    return n + (n/1000+ (n%1000)/100 + (n%100)/10 + (n%10);
}
 
int main(void) {
    bool isSelf[10000];
    int temp;
    
    for(int i=0; i<10000; i++)
        isSelf[i] = true;
    
    
    for(int i=1; i<10000; i++) {
        temp = i;
        while(temp < 10000) {
            temp = d(temp);
            if(temp <= 10000)
                isSelf[temp-1= false;
        }
    }
    
    for(int i=0; i<10000; i++)
        if(isSelf[i])
            cout << i+1 << "\n";
    
    return 0;
}
cs


'C & C++ > Baekjoon' 카테고리의 다른 글

백준 5622 - 다이얼  (0) 2017.12.27
백준 8393 - 합  (0) 2017.12.27
백준 4344 - 평균은 넘겠지  (0) 2017.12.27
백준 2941 - 크로아티아 알파벳  (0) 2017.12.27
백준 2920 - 음계  (0) 2017.12.27

+ Recent posts