9 degrees OJ 1031 xxx law, 9 degrees oj1031xxx

Source: Internet
Author: User

9 degrees OJ 1031 xxx law, 9 degrees oj1031xxx

Question 1031: xxx Law

Time Limit: 1 second

Memory limit: 32 MB

Special question: No

Submit: 4995

Solution: 3187

Description:
If n is an even number, n is halved. If n is an odd number, n is converted to 3 * n + 1 and then halved until the number is changed to 1.
Perform several steps to change n to 1. For details, see the example.
Input:
The test contains multiple use cases. Each use case contains an integer of n. If n is 0, the input ends. (1 <= n <= 10000)
Output:
For each group of test cases, please output a number, indicating the number of steps to pass, each group of output occupies one line.
Sample input:
310
Sample output:
50

#include<stdio.h>int main(int argc, char *argv[]){    int n;    int i=0;    while(scanf("%d",&n)!=EOF)    {        i=0;        if(n==0)break;        while(n!=1){            if(n%2==0)            {                n=n/2;                i++;            }            else if(n%2==1)            {                n=3*n+1;                n=n/2;                i++;            }        }        if(n==1)            printf("%d\n",i);    }    return 0;} /**************************************************************    Problem: 1031    User: kirchhoff    Language: C    Result: Accepted    Time:0 ms    Memory:912 kb****************************************************************/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.