Hduoj ------- (1128) Self numbers

Source: Internet
Author: User
Self numbers

Time Limit: 20000/10000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 6227 accepted submission (s): 2728


Problem descriptionin 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 (n )), 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.


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

 

Sample output135792031425364 | <-- a lot more numbers | 9903991499259927993899499960997199829993 |

 

Source mid-Central USA 1998 Nima, It's too simple, and the water goes through ...... code:
1 # include <cstdio> 2 # include <cstring> 3 # define maxn 1000001 4/* calculate the sum of single digits */5 Int work (int n) 6 {7 int sum = 0; 8 while (n> 0) {9 sum + = n % 10; 10 N/= 10; 11} 12 Return sum; 13} 14 bool ans [maxn]; 15 int main () {16 int Pos; 17 // freopen ("test. out "," W ", stdout); 18 memset (ANS, 0, sizeof (ANS); 19 for (INT I = 1; I <maxn; I ++) {20 Pos = I + work (I); 21 if (Pos <= 1000000 &&! Ans [POS]) ans [POS] = 1; 22} 23 for (INT I = 1; I <maxn; I ++) {24 if (! Ans [I]) printf ("% d \ n", I); 25} 26 return 0; 27}
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.