#include <stdio.h>
#define MAXSIZE 1000000
int Cache[MAXSIZE]={0};
int loopLength(int n)
{
if(n==1) return 1;
if(n%2==0) n = n/2;
else n = 3*n+1;
if(n < MAXSIZE)
{
if(Cache[n]==0)
Cache[n] = loopLength(n);
return 1 + Cache[n];
}
return 1 + loopLength(n);
}
int maxLoopLength(int a,int b)
{
int max=0;
int i,n;
int temp;
if(a>b){
a = a+b;
b = a-b;
a = a-b;
}
for(i=a;i<=b;i++)
{
temp = loopLength(i);
if(max<temp) max = temp;
}
return max;
}
int main()
{
int a,b;
while(scanf("%d %d",&a,&b)!=EOF)
{
printf("%d %d %d\n",a,b,maxLoopLength(a,b));
}
return 0;
}
UVa 100 The 3n + 1 problem
最新推荐文章于 2019-05-23 20:29:31 发布