| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1014 人关注过本帖
标题:一道acm题,当数据小时运行没问题,但数据一大就超时,请强人指点
只看楼主 加入收藏
醉生梦死
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2007-8-21
收藏
 问题点数:0 回复次数:9 
一道acm题,当数据小时运行没问题,但数据一大就超时,请强人指点


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就是超时,请强人简化一下时间复杂度,谢谢

搜索更多相关主题的帖子: acm 强人 数据 超时 
2007-08-30 22:21
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
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]
2007-08-30 22:33
醉生梦死
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2007-8-21
收藏
得分:0 
回复:(雨中飞燕)10s的话时间是充足的啊,题目链接在...
http://acm.jlu.edu.cn/joj/showproblem.php?pid=1021 美女,帮帮忙吧,郁闷

2007-08-30 22:37
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
我在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]
2007-08-30 22:47
醉生梦死
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2007-8-21
收藏
得分:0 
回复:(雨中飞燕)我在zju上AC了,所用时间不到1秒[i...

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


2007-08-30 22:50
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
#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]
2007-08-30 22:52
醉生梦死
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2007-8-21
收藏
得分:0 
回复:(雨中飞燕)#include using n...
大姐啊,你帮帮我吧,我刚开始做acm,加你qq好吗?以后多指教

2007-08-30 23:01
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分: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]
2007-08-30 23:03
醉生梦死
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2007-8-21
收藏
得分:0 
回复:(雨中飞燕)如果再做点手脚,应该可以再快两倍...

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


2007-08-30 23:20
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
http://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]
2007-08-30 23:27
快速回复:一道acm题,当数据小时运行没问题,但数据一大就超时,请强人指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015311 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved