Number Sequence



Given a positive integer number, we want to generate a number sequence with the following rules:

If the current number is 1, the process will be terminated. Otherwise, if the current number is even, the next number will be n/2. If the current number is odd (except 1), the next number will be 3*n+1. Then we turn to deal with the new number, until we get 1.

For example, given the first number 22, we will get {22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1}. The length of this sequence is 16.

Given the first number, your task is to find the length of the sequence generated with these rules. We assume that the length will not be more than 1000.

Input

There is only one number N (1 ≤ N ≤ 1000) for each test case, indicating the first number of the sequence.

The input is terminated by a zero.

Output

Output one number in a line for each test case, indicating the length of the sequence we generate.

Sample Input

22
1000
1
0

Sample Output

16
112
1 

题意概述:按照给定规则产生数字串,直到产生1时终止。水题。。

解题思路:没有什么简单的思路,只能按照给定规则穷举。

源代码:

#include<iostream>
using namespace std;
int main()
{
    int N,L;
    while(cin>>N&&N)
    {
          L=1;
          while(N>1)
          {
               if(N%2==0)N/=2;
               else N=N*3+1;
               ++L;
          }
          cout<<L<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值