xxx定律

题目描述:
    对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。
    请计算需要经过几步才能将n变到1,具体可见样例。
输入:
    测试包含多个用例,每个用例包含一个整数n,当n为0 时表示输入结束。(1<=n<=10000)
输出:
    对于每组测试用例请输出一个数,表示需要经过的步数,每组输出占一行。
样例输入:
3
1
0
样例输出:
5
0

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <cctype>
 6 #include <cstring>
 7 #include <vector>
 8 #include <list>
 9 #include <deque>
10 #include <stack>
11 #include <queue>
12 #include <algorithm>
13 #include <string>
14 #include <map>
15 #include <set>
16 
17 
18 using namespace std;
19 
20 
21 int main()
22 {
23 
24 
25 
26 
27 
28 
29     int n;
30     int step;
31 
32     while(scanf("%d",&n)!=EOF)
33     {
34         if(n==0)
35             break;
36 
37         step=0;
38 
39         while(n!=1)
40         {
41             if(n%2==0)
42             {n/=2;step++;}
43             else
44             {
45                 n=n*3+1;
46                 n/=2;
47                 step++;
48             }
49         }
50 
51         printf("%d\n",step);
52     }
53 
54 
55 
56 
57 
58 
59     
60 
61 
62 
63         
64 
65 
66 
67 
68 
69 
70     return 0;
71 }

 

posted @ 2012-05-30 20:37  cseriscser  阅读(371)  评论(0编辑  收藏  举报