Self Numbers
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 22335 | Accepted: 12567 |
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.
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
/***********************************************************************
Copyright (c) 2015,wangzp
All rights no reserved.
Name: 《Self Numbers》In PEKING UNIVERSITY ACM
ID: PROBLEM 1316
问题简述: 找出10000以内的自数。什么叫做自数呢?
比如:d(75) = 75 + 7 + 5 = 87,87就不是自数;
d(1) = 1 + 1 = 2,2也不是自数;
100以内的自数为:1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97.
Date: SEP 09, 2015
***********************************************************************/
#include
#include
int a[10001];
void self_num_fun(void)
{
int i,t1,t2;
for (i = 1;i <= 10000;i++)
{
t1 = t2 = i;
while(t1)
{
t2 += t1 % 10;
t1 /= 10;
}
if (t2 <= 10000)//注意这个条件
{
a[t2] = 1;//不是self-number的元素做标记
}
}
//打印self-number
for (i = 1;i <= 10000;i++)
{
if (!a[i])
{
printf("%d\n",i);
}
}
}
int main(void)
{
memset(a,0,sizeof(a));//把所有的a元素都初始化为0(代表是self-number)
self_num_fun();
return 0;
}