题意:已经一个数能够经过这样的变化:
1 : if n = 1 then STOP
if n is odd then n <-- 3n+1
else n <-- n/2
GOTO 1
最后的结果都会变为1,要求是输入一数字区间,输出经过这样的变化最后结果为1的操作得最多的一次的次数。
#include <iostream>
using namespace std;
int main()
{
long max=0,cishu=0;
long minj,maxj,i,number,tep;
while(cin>>minj>>maxj)
{
tep=0;
max=0;
if(minj>maxj)//注意题目的阴险好像就在这里,不要被simple迷惑了
{
long temp;
temp=minj;
minj=maxj;
maxj=temp;
tep=1;
}
for(i=minj;i<=maxj;i++)
{
number=i;
cishu=1;
while(number!=1)
{
if(number%2)
number=3*number+1;
else
number=number/2;
cishu++;
}
if(cishu>max)
max=cishu;
}
if(tep==1)
cout<<maxj<<" "<<minj<<" ";
else
cout<<minj<<" "<<maxj<<" ";
cout<<max<<endl;
}
return 0;
}