HDU1128 Self Numbers 筛选

  由于该题是求一个数是不是“自身数”,而定义一个数是不是“自身数”的根据就是是否有祖先,又因为一个数的祖先一定比这个数要小,所以这就和筛选法很像了。

  代码如下:

 1 #include <cstring>
2 #include <cstdlib>
3 #include <cstdio>
4 using namespace std;
5
6 char hash[1000005];
7
8 void deal( int x )
9 {
10 int rec = x;
11 while( x > 0 )
12 {
13 int c = x % 10;
14 x /= 10;
15 rec += c;
16 }
17 hash[rec] = 1;
18 }
19
20 int main()
21 {
22 for( int i = 1; i <= 1000000; ++i )
23 {
24 if( !hash[i] )
25 printf( "%d\n", i );
26 deal( i );
27 }
28 return 0;
29 }

  

原文地址:https://www.cnblogs.com/Lyush/p/2157197.html