3038 3n+1 Problems

Source: Internet
Author: User

Topic description Description3n+1 problem is a simple, interesting and unsolved mathematical problem. This problem is made by L. Collatz was introduced in 1937. The Korazim problem (Collatz problem) is also called hailstone problem, 3n+1 problem, Hasse algorithm problem, Kakutani algorithm problem, Thwaites conjecture, or Ulam problem. The problem is as follows: (1) Enter a positive integer n, (2) If N=1 ends, (3) if n is odd, n becomes 3n+1, otherwise n becomes N/2; (4) Moves to step (2). The special point of the Korazim problem is that, although it is easy to clarify the problem, it is still not guaranteed that the algorithm for this problem will work for all possible inputs-that is, no one has proven that the process terminates for all positive integers. Enter a description input Description

The first line is an integer T. Represents the number of groups of input data.

The second line is a T-positive integer n.

Outputs description output Description

For each positive integer n, each line outputs a number of s, indicating how many steps n passes through to 1, and if n cannot become 1, then output-1.

Sample input to sample

3

1 2 3

Sample output sample outputs

0

1

7

Data size & Hint

1 <= T <= 100

1 <= N <= 10000

The reason for this problem-1 is a pit point, I added a large number of conditions but many people reflect even if not judged-1 is OK does not affect AC.

is still a recursive topic, constantly deformation according to conditions until the result is 1.

The AC code is attached:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 using namespacestd;7 8 intT,n;9 Ten intConversionintN) { One     intsum=0; A      while(1){ -         if(n==1) -      returnsum; the         Else if(n%2==1){ -n=n*3+1; -sum++; -     } +          Else{ -n=n/2; +sum++; A       } at       if(sum>1000000) -       return-1; -     } - } -  - intMain () { in      -      while(cin>>t) { to          while(t--){ +Cin>>N; -Cout<<conversion (n) <<Endl; the         }     *     } $     return 0;Panax Notoginseng}

3038 3n+1 Problems

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.