早教吧 育儿知识 作业答案 考试题库 百科 知识分享

ACM初学者The3n+1problemPOJ1207翻译后大概是这个意思:考虑一个算法来生成以下序列的.从整数n开始.如果n为偶,除以2.如果是奇,乘3加1.对后面的n重复这个步骤,n=1时终止.例如,n=22时,会产生下

题目详情
ACM初学者 The 3n + 1 problem
POJ 1207
翻译后大概是这个意思:考虑一个算法来生成以下序列的.从整数n开始.如果n为偶,除以2.如果是奇,乘3加1.对后面的n重复这个步骤,n = 1时终止.例如,n=22时,会产生下列顺序编号.
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
据猜想(但尚未证实),对任意整数n,该算法将终止在n = 1处.还有,此猜想对于1,000,000内的整数都成立.
对于一个输入n,n的周期长度是指产生包括(1)在内的数字个数.在上面的例子中,22的周期长度为16.给定任何两个数字i和j,需要你确定出对于i和j之间数字的最大周期长度,包括i和j两端点.
输入:
输入将是一系列的整数对i和j,每行一个整数对.所有的整数将小于10000,且>0.
输出:
为每一对输入整数对i和j,输出的i和j应是同样的顺序,最后是的最大周期长度.这三个数字由一个空格隔开,每三个数字一行,对应一种输入和输出.
Sample Input
1 10
100 200
201 210
900 1000
Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174
我是这么写的:
#include
using namespace std;
int main()
{
int i,j,t,n,p,l;
while(cin>>i>>j)
l=1; //周期最小是1
{
if(i>j){t=i;i=j;j=t;}
for(i>0;i
▼优质解答
答案和解析
哈哈!其实有的时候很纠结.我用下面的代码在某校的OJ上过了(因为时间太长忘了),但在我们学校的OJ上去Wrong Answer.
The 3n + 1 problem
Consider the following algorithm to generate a sequence of numbers.Start with an integer n.If n is even,divide by 2.If n is odd,multiply by 3 and add 1.Repeat this process with the new value of n,terminating when n = 1.For example,the following sequence of numbers will be generated for n = 22:
考虑一个算法来生成以下序列的.从整数n开始.如果n为偶,除以2.如果是奇,乘3加1.对后面的n重复这个步骤,n = 1时终止.例如,n=22时,会产生下列顺序编号.
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured (but not yet proven) that this algorithm will terminate at n = 1 for every integer n.Still,the conjecture holds for all integers up to at least.
据猜想(但尚未证实),对任意整数n,该算法将终止在n = 1处.还有,此猜想对于1,000,000内的整数都成立.
For an input n,the cycle-length of n is the number of numbers generated up to and including the 1.In the example above,the cycle length of 22 is 16.Given any two numbers i and j,you are to determine the maximum cycle length over all numbers between i and j,including both endpoints.
对于一个输入n,n的周期长度是指产生包括(1)在内的数字个数.在上面的例子中,22的周期长度为16.给定任何两个数字i和j,需要你确定出对于i和j之间数字的最大周期长度,包括i和j两端点.
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.
输入,
输入将是一系列的整数对i和j,每行一个整数对.所有的整数将少于100万,且>0.
Output
For each pair of input integers i and j,output i,j in the same order in which they appeared in the input and then the maximum cycle length for integers between and including i and j.These three numbers should be separated by one space,with all three numbers on one line and with one line of output for each line of input.
输出
为每一对输入整数对i和j,输出的i和j应是同样的顺序,中间是其间的最大周期长度.这三个数字由一个空格隔开,每三个数字一行,对应一种输入和输出.
Sample Input
输入例:
1 10
100 200
201 210
900 1000
Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
#include
using namespace std;
int main(){
int n1,n2,i=0,j,k=0,max,t=0,m1,m2;
while(scanf("%d%d",&n1,&n2)==2){
max=0;
while(n11000000){
scanf("%d%d",&n1,&n2);
}
m1=n1;m2=n2;
if(m>n){
m1=n2;m2=n1;
}
j=m1;
for(;j