首页 > 代码库 > poj1316

poj1316

Self Numbers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20864   Accepted: 11709

Description

In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)), d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence

33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ...
The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97.

Input

No input for this problem.

Output

Write a program to output all positive self-numbers less than 10000 in increasing order, one per line.

Sample Input

Sample Output

1
3
5
7
9
20
31
42
53
64
 |
 |       <-- a lot more numbers
 |
9903
9914
9925
9927
9938
9949
9960
9971
9982
9993

Source

Mid-Central USA 1998
这题第一眼看上去就觉得应该要用什么特殊方法,要不然会超时,可是最后没想到根本不会超时,我在调试中提交了n次Runtime error,让我几乎快疯了,后来当我把定义的bool数组移到main函数外就ac了,这让我百思不得其解,难道main函数内部定义一个10000长度的bool数组会超过限制吗
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int MAXn = 10000;
 6 int numOf_Digit(int number)
 7 {
 8     int sum = number;
 9     while(number>=10)
10     {
11         sum += number%10;
12         number /=10;
13     }
14      sum+=number ;
15      return sum;
16 }
17 bool vis[MAXn];
18 int main()
19 {
20     
21     memset(vis,true,sizeof(vis));
22     for(int i=1;i<10000;i++)
23     {
24         int t =numOf_Digit(i);
25         vis[t] = false;
26     }
27     for(int i =1 ;i<10000;i++)
28         if(vis[i]==true)
29             printf("%d\n",i);
30     return 0;
31 }

当然直接把#define 去掉,直接就是vis[10000],并且取消掉那个运算函数,用一句int t = i + i/1000+(i/100)%10+(i/10)%10+i%10; 就可以0MS了,但个人风格,喜欢把功能变成函数