POJ 1316 Self Numbers(我的水题之路——筛法)

Self Numbers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16630   Accepted: 9324

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

存在一种数字,它可以用另一个数和那个数本身各个位数之和表示,比如51可以用39+3+9表示,则要求输出10000以内,所有不能用这种形式表示的数字。

筛法,用一个标记数组,所有值初始化为0,从数组下标1开始,如果标记数组值为0,则将从这个数字开始,所以以后由这个数字衍生出去的可以表示的数字,标记为1,如:1的衍生数为2、4、8、16...,一直到10000

代码(1AC):
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define N 10000

int array[11000];
int list[11000];

int main(void){
    int i, j;
    int top , end;
    int num, num1, num2;

    memset(array, 0, sizeof(array));
    for (i = 1; i <= N; i++){
        if (array[i] == 0){
            num = num1 = i;
            for (; num1 < N;){
                num1 = num;
                while (num){
                    num1 += num % 10;
                    num /= 10;
                }
                num = num1;
                array[num1] = 1;
            }
        }
    }
    for (i = 1; i <= N; i++){
        if (array[i] == 0){
            printf("%d\n", i);
        }
    }
    return 0;
 }


你可能感兴趣的:(Integer,less,input,generator,output,Numbers)