poj 1207 The 3n + 1 problem

#include <iostream>
using namespace std;
int cycle(int n)
{
int t=1;
while(n!=1)
{
if(n%2)
n
=3*n+1;
else
n
/=2;
t
++;
}
return t;
}
int main()
{
int i,j,k,max,m,tag;
while(cin>>i>>j)
{
tag
=0;
if(i>j)
{
tag
=1;
swap(i,j);
}
max
=1;
for(k=i;k<=j;k++)
{
if(cycle(k)>max)
m
=k,max=cycle(k);
}
if(tag==1)
swap(i,j);
cout
<<i<<" "<<j<<" "<<max<<endl;
}
return 0;
}

  

posted on 2011-07-17 23:54  sysu_mjc  阅读(120)  评论(0编辑  收藏  举报

导航