uva 100 - The 3n + 1 problem

 

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 void print(int i, int j)
 6 {
 7     if(i > j) { int temp = i; i = j; j = temp; }
 8 
 9     int max = 0;
10     for(int k = i; k <= j; k++)
11     {
12         int n = k, sum = 1;
13         while(n != 1)
14         {
15             if(n % 2) n = 3 * n + 1, sum ++;
16             else n = n / 2, sum ++;
17         }
18         if(max < sum) max = sum;
19     }
20     printf("%d\n", max);
21 }
22 
23 int main()
24 {
25     int i, j;
26     while(scanf("%d%d", &i, &j) != EOF)
27     {
28         printf("%d %d ", i, j);
29         print(i, j);
30     }
31     return 0;
32 }

 

posted @ 2016-01-20 02:07  小小泽  阅读(129)  评论(0编辑  收藏  举报