问题描述
1949年,印度数学家D.R. Kaprekar发现了一组名为“自我数字”的数字。对于任何正整数n,定义d(n)为n,加上n的数字的和(d代表数字,这是Kaprekar创造的术语)。
例如,d(75)= 75 + 7 + 5 = 87。给出任何正整数n作为起点,您可以构建无限增序列的整数n,d(n)、d(d(n))、d(d(d(n))),….
例如,如果你从33开始,下一个数字是33 + 3 + 3 = 39,下一个是39 + 3 + 9 = 51,下一个是51 + 5 + 1 = 57,所以你生成了序列
33、39、51、57、69、84、96、111、114、120、123、141、……
n被称为d(n)的生成器。在上面的序列中,33是一个39的生成器,39是一个51的生成器,51是一个57的生成器,等等。
一些数字有多个生成器:例如,101有两个生成器,91和100。一个没有生成器的数字是一个自数。
有13个自我数字小于100:1、3、5、7、9、20、31、42、53、64、75、86和97。
Write a program to output all positive self-numbers less than 10000 in increasing order, one per line
#include"stdio.h"
#define N 10001
int self[N];
int D();
int main()
{
int i ;
D();
for(i = 1; i<=N; i++)
{
if(!self[i])
printf("%d\n",i);
}
return 0;
}
int D()
{
int sum;
int i,j,flag;
for(i = 1; i<=N; i++)
{
sum = i;
flag = i;
while(flag!=0)
{
j= flag%10;//j表示余数
sum = sum+j;
flag = flag/10;
}
self[sum] = 1;
}
return 1;
}