注册 登录
编程论坛 C++教室

一道acm题,当数据小时运行没问题,但数据一大就超时,请强人指点

醉生梦死 发布于 2007-08-30 22:21, 1018 次点击


1021: Self Numbers

--------------------------------------------------------------------------------
Status In/Out TIME Limit MEMORY Limit Submit Times Solved Users JUDGE TYPE
stdin/stdout 10s 16384K 1810 687 Standard

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.


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
|
|
|
我的代码:
#include <iostream>
using namespace std;

int isnotself[1000000]={0};

int Bitcout(int number,int bitnumber) //用来计算一个数有多少位,bitnumber用来统计一个数有多少位
{
if (number/10<1)
return bitnumber;
else
number=number/10;
return Bitcout(number,bitnumber)+1;
}

void d(int n)
{
if (n<=1000000)
{
int btn=Bitcout(n,1);
int bitshu[8]={0};
for (int i=1,n1=n;i<=btn&&n1!=0;i++)
{
bitshu[i]=n1%10;
n1=n1/10;
}
for (int i=1;i<=btn;i++)
n+=bitshu[i];
isnotself[n]=1;
d(n);
}
else
return;
}

int main()
{
for (int i=1;i<=1000000;i++)
if(isnotself[i]==0)
d(i);

for (int i=1;i<=1000000;i++)
if(isnotself[i]==0)
cout<<i<<endl;

return 0;
}
当把1000000换成100000时答案没什么问题,用1000000就是超时,请强人简化一下时间复杂度,谢谢

9 回复
#2
雨中飞燕2007-08-30 22:33
10s的话时间是充足的啊,题目链接在哪?



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
C/C++算法习题(OnlineJudge):[url]http://yzfy.org/[/url]
#3
醉生梦死2007-08-30 22:37
回复:(雨中飞燕)10s的话时间是充足的啊,题目链接在...
#4
雨中飞燕2007-08-30 22:47
我在zju上AC了,所用时间不到1秒



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
C/C++算法习题(OnlineJudge):[url]http://yzfy.org/[/url]
#5
醉生梦死2007-08-30 22:50
回复:(雨中飞燕)我在zju上AC了,所用时间不到1秒[i...

给我看看你的代码行吗?顺便看看我写的到底怎么改好,谢谢了

#6
雨中飞燕2007-08-30 22:52
#include <iostream>
using namespace std;
const int MAXNUM = 1000000;
int map[MAXNUM+1]={0};
int main()
{
for(int n=1;n<=MAXNUM;++n)
{
int ns = n;
for(int nt=n;nt;nt/=10)
ns += nt%10;
int t=ns;
if(t<=MAXNUM)map[t]=1;
}
for(int n=1;n<=MAXNUM;++n)
{
if(map[n]==0)printf("%d\n", n);
}
return 0;
}



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
C/C++算法习题(OnlineJudge):[url]http://yzfy.org/[/url]
#7
醉生梦死2007-08-30 23:01
回复:(雨中飞燕)#include using n...
大姐啊,你帮帮我吧,我刚开始做acm,加你qq好吗?以后多指教
#8
雨中飞燕2007-08-30 23:03
如果再做点手脚,应该可以再快两倍多



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
C/C++算法习题(OnlineJudge):[url]http://yzfy.org/[/url]
#9
醉生梦死2007-08-30 23:20
回复:(雨中飞燕)如果再做点手脚,应该可以再快两倍...

看了你的代码,我感觉自己写的80%的操作都是废话,郁闷...努力编程

#10
雨中飞燕2007-08-30 23:27

by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571" target="_blank">https://yzfy.org/
来这里看看吧楼上



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571
]请大家不要用TC来学习C语言,点击此处查看原因[/url]
C/C++算法习题(OnlineJudge):[url]http://yzfy.org/[/url]
1