poj1207(3n+1)

来源:互联网 发布:amd是什么软件 编辑:程序博客网 时间:2024/05/17 03:34
题意: 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; }







0 0
原创粉丝点击