题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1316
这道题的算法类似筛法找素数,用一个标志数组标志是否被选中。
#include <iostream>
using namespace std;
#define MAX 10001
int Find(bool yes[10001],int i)
{
for(;i <= 10000 && !yes[i];++i);
if(i > 10000)
return -1;
else
return i;
}
int main()
{
bool yes[MAX];
int i,j,t;
memset(yes,true,sizeof(yes));
i = 1;
i = Find(yes,i);
while(i != -1)
{
j = i;
while(true)
{
t = j;
while(t > 0)
{
j += t%10;
t /= 10;
}
if(j >= MAX)
break;
yes[j] = false;
}
i = Find(yes,i + 1);
}
for(i = 1;i < MAX;++i)
if(yes[i])
cout << i << endl;
return 0;
}