HDU 1032 The 3n + 1 problem

题解:简单模拟

#include <cstdio>  
#include <algorithm>
using namespace std;
int main(){  
    int n,m,num,cnt,max,i;  
    while(~scanf("%d %d",&m,&n)){  
        printf("%d %d ",m,n);
        if(m>n)swap(m,n);   
        for(max=0,i=m;i<=n;i++){  
            num=i; cnt=1;  
            while(num!=1){  
                if(num%2==0)num=num/2;  
                else num=3*num+1;cnt++; 
            }if(cnt>max)max=cnt;  
        }printf("%d\n",max);  
    }return 0;  
}  
posted @ 2014-08-29 17:29  forever97  阅读(127)  评论(0编辑  收藏  举报