The 3n + 1 problem
http://poj.org/problem?id=1207
此题注意比较n,m的大小,还有最后的原样输出。
1 #include<stdio.h> 2 int main() 3 { 4 int n,m,i,j,max,t; 5 while(~scanf("%d%d",&n,&m)) 6 { 7 int n1 = n; 8 int m1 = m; 9 max = 0; 10 if(n > m) 11 { 12 t = n; 13 n = m; 14 m = t; 15 } 16 for (i = n; i <= m; i ++) 17 { 18 int cnt = 1; 19 j = i; 20 while(j!=1) 21 { 22 if (j%2==0) 23 { 24 j /= 2; 25 } 26 else 27 { 28 j = 3*j+1; 29 } 30 cnt++; 31 } 32 if (cnt > max) 33 max = cnt; 34 } 35 printf("%d %d %d\n",n1,m1,max); 36 } 37 return 0; 38 }