| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 564 人关注过本帖
标题:一个问题
只看楼主 加入收藏
flylee
Rank: 5Rank: 5
等 级:职业侠客
帖 子:309
专家分:374
注 册:2004-8-10
收藏
 问题点数:0 回复次数:6 
一个问题

The Problem

Consider the following algorithm:

 
		1. 		 input n

2. print n

3. if n = 1 then STOP

4. if n is odd then

5. else

6. GOTO 2

Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.

The Input

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

You can assume that no opperation overflows a 32-bit integer.

The Output

For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174
搜索更多相关主题的帖子: middle following sequence 
2005-12-29 17:43
flylee
Rank: 5Rank: 5
等 级:职业侠客
帖 子:309
专家分:374
注 册:2004-8-10
收藏
得分:0 

这是我的代码,不能过。不知道是什么原因,请大家帮忙找找
#include <iostream.h>
#include <fstream.h>

const int maxn=1000000;

unsigned long table[maxn]={0};

void count()
{
table[1]=1;
for(long i=2;i<maxn;i++){
long k=i, s=0;
while(1){
if(k%2)
k=k*3+1;
else
k=k/2;
s++;
if(table[k]){
table[i]=s+table[k];
break;
}
}
}
}

unsigned long compare(long a,long b)
{
long max=0;
for(long i=a; i<=b; i++)
if(table[i]>max)max=table[i];
return max;
}

int main()
{
long i, j;
count();
while(cin){
cin>>i>>j;
cout<<i<<' '<<j<<' '<<compare(i,j)<<endl;
}
return 0;
}

2005-12-29 17:44
冰☆snow
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2005-10-30
收藏
得分:0 

你定义的mixn=1000000,数据溢出.
建议你的输入输出使用提示,这样你会分清楚.
修改了一下,没有问题的:
#include <iostream.h>
#include <fstream.h>

const long int maxn=1000;

unsigned long table[maxn]={0};

void count()
{
table[1]=1;
for(long i=2;i<maxn;i++)
{
long int k=i;
long int s=0;
while(1)
{
if(k%2)
k=k*3+1;
else
k=k/2;
s++;
if(table[k])
{
table[i]=s+table[k];
break;
}
}
}
}

unsigned long compare(long a,long b)
{
long max=0;
for(long i=a; i<=b; i++)
if(table[i]>max)max=table[i];
return max;
}

int main()
{
long i, j;
count();
while(cin){
cout<<"请输入数据"<<endl;
cin>>i>>j;
cout<<"输出为:"<<endl;
cout<<i<<' '<<j<<' '<<compare(i,j)<<endl;
}
return 0;
}
输出的内容有:
请输入数据
1 10
输出为:
1 10 20
请输入数据
100 200
输出为:
100 200 125
请输入数据
201 210
输出为:
201 210 89
请输入数据
900 1000
输出为:
900 1000 174
请输入数据


耐人寻味~~!
2005-12-30 16:20
flylee
Rank: 5Rank: 5
等 级:职业侠客
帖 子:309
专家分:374
注 册:2004-8-10
收藏
得分:0 
首先,输出格式必须遵循题目的要求,因为测试的过程是自动的,评测程序只会根据标准来对提交上去的程序输出进行验证
第二,在不同的编译器下面可以分配的内存大小是不同的
第三,就程序本身的算法而言,我实在找不出问题,但是就是连题目上的数据也不能全部通过

希望大家能够再帮忙看一下
2006-01-05 17:38
冰☆snow
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2005-10-30
收藏
得分:0 

耐人寻味~~!
2006-01-07 14:25
只敢学习
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2005-7-4
收藏
得分:0 
我编译无错...用楼主的程序,可是构建不了EXE文件...

2006-01-08 11:11
flylee
Rank: 5Rank: 5
等 级:职业侠客
帖 子:309
专家分:374
注 册:2004-8-10
收藏
得分:0 

你用的是什么编译器

2006-01-09 17:16
快速回复:一个问题
数据加载中...
 
   



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

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