读书人

HDU3782:xxx定理

发布时间: 2013-03-06 16:20:31 作者: rapoo

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

310

Sample Output
50


#include <iostream>using namespace std;int main(){    int n;    while(cin >> n && n)    {        int cnt = 0;        while(n!=1)        {            if(n%2)            n = (3*n+1)/2;            else            n = n/2;            cnt++;        }        cout << cnt << endl;    }    return 0;}


读书人网 >编程

热点推荐