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; }
愿你出走半生,归来仍是少年