100 - The 3n+1 problem
Time limit 3.000 seconds
Problems in Computer Science are often classified as belonging to a certain class of problrms(e.g.,NP,Unsolvable,Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not konwn for all possible inputs.
Consider the following algorithm:
1. input n
2.print n
3.if n==1 then STOP
4. if n is odd then n<-----3n+1
5. else n <-------n/2
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 sunch 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 maximum cycle length over all numbers between and including the 1 is printed .For a given n this is called the cyccle-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 and including both i and j.
Input
The input will onsist of a srries of pairs of integers i and j,one pair of integers per line.All integers will be less than 10,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maxmum cycle length over all integers between and including i and j.
You can assume that no operation overflows a 32-bit integer.
Ouput
For each pair of input integers i and j you should output i,j,and the maxmum cycle length for integers between and including i and j.There 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 inout,The integers i ang j must appear in the output int the same order in which they appeared in the input and be followed by the maxmum 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
代码1:此代码时间超限,但是打表策略体现在其中。
#include<stdio.h>
#include<iostream>
using namespace std;
int a[1000001];
int num(int n)
{
int i=1;//数值自身计入1
while(n!=1)
{
if(n&1)//奇数
n=3*n+1;
else
n>>=1;//将n对应的二进制右移一位,对应十进制减半;
i++;
}
return i;
}
void numlen()//存储每个数得计算次数
{
int i;
for(i=1;i<1000001;i++)
a[i]=num(i);
}
int main()
{
numlen();
int x,y,n,maxnum,min,max,i;
while(scanf("%d%d",&x,&y)!=EOF)
{
maxnum=0;
min=x<y?x:y;
max=x>y?x:y;
for(i=min;i<max;i++)
{
n=a[i];
if(n>maxnum)
maxnum=n;
}
printf("%d %d %d\n",x,y,maxnum);
}
return 0;
}
代码2:在UVaOJ上运行通过的代码:
#include<cstdio>
#include<algorithm>
typedef long long LL;
const int maxn = 1000001;
int data[maxn];//打表
using namespace std;
#include<map>
int main()
{
for(int d=1;d<maxn;d++)
{
int cnt=1;
LL i=d;
while(i>1)
{
if(i&1) i=3*i+1;
else i>>=1;
cnt=cnt+1;
}
data[d]=cnt;
}//实现打表
int i,j;
while(scanf("%d%d",&i,&j)!=EOF)
{
int m=i,n=j,min,max;
min = i<j?i:j;
max = i>j?i:j;
int maxn =0;
for(int d=min;d<=max;d++)
if(data[d]>maxn) maxn=data[d];
printf("%d %d %d\n",m,n,maxn);
}
}
什么是 打表:
打表是一种典型的空间换时间的技巧,一般指的是:将所有可能用到的结果事先计算出来,这样后面需要用到时可以直接查表获得。
本文探讨了计算机科学中著名的3n+1问题,详细解释了一个算法如何处理任意整数输入,并分析其最大循环长度。文章提供了两种算法实现,一种为直接计算方法,另一种为优化后的打表策略,后者在UVaOJ上成功运行。文中还阐述了打表策略的概念,即预先计算并存储所有可能结果,以提高后续查询效率。

436

被折叠的 条评论
为什么被折叠?



