题目大意:
按照
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
规律计算i和j之间迭代次数最多的数,最后要计算到1
解题思路:
简单模拟
代码如下:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int cycle(int n)
{
int count=1,m=n;
while(m!=1)
{
if(m%2)
{
m=3*m+1;
count++;
}
else
{
m=m/2;
count++;
}
}
return count;
}
int main()
{
int i,j;
int count,max;
int k;
while(scanf("%d%d",&i,&j)!=EOF)
{
max=0;
if(i<j)
{
max=cycle(i);
for(k=i+1;k<=j;k++)
{
count=cycle(k);
if(count>max)
{
max=count;
}
}
}
else
{
max=cycle(j);
for(k=j+1;k<=i;k++)
{
count=cycle(k);
if(count>max)
{
max=count;
}
}
}
printf("%d %d %d\n",i,j,max);
}
return 0;
}