Algorithm/BOJ

[백준 알고리즘/BOJ/C++] 4673 셀프 넘버

pinevienna 2021. 1. 12. 01:18

 

 

생성자가 없는 숫자를 셀프 넘버라고 한다

10000보다 작거나 같은 생성자를 구하는 문제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
 
bool arr[10001];
 
int main(void) {
    int result;
 
    for (int i = 1; i < 10001; i++) {
        result = i + i % 10 + i % 100 / 10 + i % 1000 / 100 + i % 10000 / 1000 + i / 10000;
        if (result > 10000)
            continue;
 
        arr[result] = true;
    }
 
    for (int j = 1; j < 10001; j++) {
        if (arr[j] == false)
            cout << j << endl;
    }
}
cs

 

bool형 배열을 만들고

배열[n과 n의 각 자리수를 더한 값]에 true를 넣어줌 (생성자가 있는 애들은 true가 되는 것)

false인 주소값을 출력하면 셀프넘버만 출력됨