题意: 3n+1问题,如果i为奇数,则执行i=i*3+1; 否则i/=2; 开始的时候根本就没想到这个题会卡住。错了n遍后,终于鼓起勇气百度了一下,才知道,需要考虑n,m的大小。
<pre name="code" class="html">#include<iostream>
using namespace std;
int main()
{
int m, n, i, cnt, t;
int max;
while(cin>>n>>m)
{
cout<<n<<" "<<m<<" ";
if(n>m){
t = n; n = m; m = t;
}
max = -1;
for(i=n; i<=m; i++)
{
cnt = 1; t = i;
while(t-1)
{
cnt++;
if(t%2) t = 3*t+1;
else t = t/2;
}
if(max<cnt)
max = cnt;
}
cout<<max<<endl; }
return 0;
}

756

被折叠的 条评论
为什么被折叠?



