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
题解:
基础题。题目不难,只要能够理解题意的话就能够顺利的做出来了.
代码:
1 /* 2 Name: Self Number 3 Copyright: 4 Author: 5 Date: 11/08/17 04:23 6 Description: 7 */ 8 #include<stdio.h> 9 #include<iostream> 10 #include<cstring> 11 #include<algorithm> 12 using namespace std; 13 const int NUM = 10000; 14 int record[NUM]; 15 int judge(int i) 16 { 17 int temp = i; 18 while(i) 19 { 20 temp += i%10; 21 i /= 10; 22 } 23 return temp; /*返回非self number*/ 24 } 25 26 int main() 27 { 28 memset(record,0,sizeof(record)); /*数组初始化,是一个好习惯*/ 29 for(int i = 1; i < NUM;i++) 30 { 31 int temp = judge(i); 32 if(temp < NUM ) 33 record[temp] = 1; /*将不是self number的整数标记为1*/ 34 } 35 bool flag = false; 36 for(int i = 1; i < NUM;i++) 37 { 38 39 if(record[i] == 0 )/*注意输出格式 最后一个不空行*/ 40 { 41 if(flag) 42 cout<<endl; 43 cout<<i; 44 flag = true; 45 } 46 47 } 48 return 0; 49 }