셀프넘버를 구현하는 풀이이다.
셀프넘버인 수만 골라 그 수를 인덱스로 벡터에 셀프넘버 여부를 저장한다.
셀프넘버가 아닌 인덱스의 벡터만 출력한다.
#include <iostream>
#include <vector>
using namespace std;
void find_self_number()
{
int i = 1, j, total = 0;
vector <bool> self_number(10001, false);
while (i <= 10000)
{
j = i;
if (self_number[j] == false)
{
while (1)
{
total = 0;
total += j;
total += j / 10000;
j = j % 10000;
total += j / 1000;
j = j % 1000;
total += j / 100;
j = j % 100;
total += j / 10;
j = j % 10;
total += j;
if (total > 10000)
{
break;
}
else
{
self_number[total] = true;
j = total;
}
}
}
i++;
}
for (i = 1; i <= 10000; i++)
{
if (self_number[i] == false)
{
cout << i << "\n";
}
}
return;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
find_self_number();
return 0;
}