hd 3782 xxx定律

来源:互联网 发布:linux怎么修改配置文件 编辑:程序博客网 时间:2024/05/16 01:37

 

xxx定律

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2188    Accepted Submission(s): 1694


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
 


 

Source
浙大计算机研究生复试上机考试-2009年
 


 

Recommend
notonlysuccess   |   We have carefully selected several similar problems for you:  3785 3786 3784 3787 2028 
 


 

 

#include<stdio.h>
int main()
{
    int n;
    int p=0;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
      p=0;
      if(n==0)
      printf("%d\n",p);
      while(n!=1)
      {
      if(n%2==0)
      n/=2;
      else
      n=(n*3+1)/2;
      p++;
      }
      printf("%d\n",p);
                                    }
return 0;
    }

0 0
原创粉丝点击