题意
这个题的意思是 :有一个数,按那个规则,最后能够转换到1,算出这个序列的长度
然后输入两个数,在这两个数构成的闭区间中,每个数都有其序列长度,求这个序列中最长的一个。
思路
先写出一个函数,求一个数的序列长度,然后对区间做循环,求长度最大值
难点
似乎也没什么难点,只是有很多细节需要考虑一下,详见代码
我改了三次才AC,就是因为输出那块出了些问题。。。
代码
#include <iostream>
using namespace std;
int number(int num){
int count=1;
while(1){
if(num==1){return count;}
if(num%2!=0){num=3*num+1;count++; }
if(num%2==0) {num=num/2;count++;}
}
}
int main()
{
int low,high,i;
while(cin>>low>>high){
int m =low,n=high;
if(low>high){
int temp = low;
low = high;
high = temp;
}
int max = number(low);
for(i=low;i<=high;i++){
if(number(i)>max) max = number(i);
}
cout<<m<<" "<<n<<" "<<max<<endl;
}
return 0;
}