Nine degrees oj-topic 1031:xxx Law

Source: Internet
Author: User

Title Description:
For a number n, if it is even, cut off n half, if it is odd, turn N into 3*n+ 1 and cut it down by half until the number becomes 1.
Please calculate how many steps it takes to change N to 1, which is a concrete example.
Input:
The test consists of multiple use cases, each containing an integer n, which indicates the end of the input when n is 0. (1<=n<=10000)
Output:
for each set of test cases, output a number that represents the number of steps that need to elapse, one row for each set of outputs.
Sample input:
310
Sample output:
50
Source:
2009 Zhejiang University computer and software engineering research on the real problem of life test
FAQ:

Problem solving problems? to discuss the topic of the topic please visit: http ://t.jobdu.com/thread-7755-1-1.html

 #include <iostream>using namespace Std;    int count = 0;void lala (int n) {if (n==1) cout<<0<<endl;        else if (n%2==0) {N=N/2;        count++;        if (n==1) cout<<count<<endl;    else Lala (n);        } else {n= (3*n+1)/2;        count++;        if (n==1) cout<<count<<endl;    else Lala (n);    }} int main () {int k;        while (cin>>k&&k!=0) {lala (k);    Count = 0; } return 0;} /************************************************************** problem:1031 user:vhreal language:c++ Resul
 t:accepted time:10 Ms memory:1520 kb****************************************************************/


Nine degree oj-title 1031:xxx Law

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.