您的位置:首页 > 其它

[3782]:xxx定律

2016-03-24 00:53 204 查看
Problem Description

对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。

请计算需要经过几步才能将n变到1,具体可见样例。

Input

测试包含多个用例,每个用例包含一个整数n,当n为0 时表示输入结束。(1<=n<=10000)

Output

对于每组测试用例请输出一个数,表示需要经过的步数,每组输出占一行。

Sample Input

3

1

0

Sample Output

5

0

简单题

/*
author : Yangchengfeng
*/

#include<stdio.h>

int main()
{
int n;
while(scanf("%d", &n)!=EOF){
int time = 0;
if(n == 0){
break;
} else {
while(n != 1){
if(n % 2 == 0){
n /= 2;
} else {
n = (3 * n + 1) / 2;
}
time++;
}
printf("%d\n", time);
}
}

return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: