self numbers?大家来看看这个题!

stormywaters 2002-04-18 10:06:37
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.


Write a program to output all positive self-numbers less than or equal 1000000 in increasing order, one per line.
...全文
118 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lawrence444 2002-04-19
  • 打赏
  • 举报
回复
int countn(int num)
{
char buf[10];
int i;
int sum=0;
sprintf(buf,"%d",num);
for(i=0;buf[i]!='\0';i++)
sum+=buf[i]-'0';
sum+=num;
return sum;
}
Lawrence444 2002-04-19
  • 打赏
  • 举报
回复
此题简单。
死搜1-1000000的所有数,用时为O(n)。
short flag[1000000];

void main()
{
int i;
for(i=0;i<1000000;i++)
flag[countn(i)-1]=1;
for(i=0;i<1000000;i++)
if(flag[i]==0)
printf("%d\n",i+1);
}

由于是O(n)的算法,30秒绝对搞定
如果有内存限制,就用bitmask做flag数组(用一位代表一个数是否是self number),这样只需要125000B的内存就可以了
LeeMaRS 2002-04-19
  • 打赏
  • 举报
回复
楼上的程序我好像执行了什么结果都没有.
stormywaters 2002-04-19
  • 打赏
  • 举报
回复
#include <alloc.h>
#include <stdio.h>
#define N 1000000L
#define MAXDIGIT 7

int main()
{
int far *c;
long i,s,t;
int d[MAXDIGIT];
int j,k;
c=(int *)farmalloc(N);
if (c==NULL) {
printf("not enough memory!");
return 1;
}
for (i=1;i<N;i++)
c[i]=0;
for (i=1;i<N;i++) {
t=1;
s=0;
for (j=0;j<MAXDIGIT;j++) {
t=10*t;
d[j]=(i-i/t*t)/(t/10);
s=s+d[j];
}
s=s+i;
if (s<N)
c[s]=1;
}
for (i=1;i<N;i++)
if (c[i]==0)
printf("%ld\n",i);
return 0;
}
LeeMaRS 2002-04-18
  • 打赏
  • 举报
回复
编一个小程序看看,好像有规律的.
看后两位,小于10的,下一个数加2.
大于10时,下一个数加11.
stormywaters 2002-04-18
  • 打赏
  • 举报
回复
要求在30秒内解决,能用筛掉非self number的方法解决吗?那样的话是否需要一个大大的数组?

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧