3038 3N + 1 Problems

Source: Internet
Author: User
3038 3N + 1 Problems

 

Time Limit: 1 s space limit: 32000 kb title level: Silver silver Title Description DescriptionThe 3N + 1 problem is a simple and interesting but unsolved mathematical problem. This issue was raised by L. Collatz in 1937. Collatz Problem is also called hailstone, 3N + 1, Hasse, Kakutani, Thwaites, or Ulam. The problem is as follows: (1) enter a positive integer N; (2) end if n = 1; (3) if n is an odd number, then N becomes 3N + 1, otherwise, n is changed to n/2. (4) Transfer to step (2. The special problem with claraz is: although it is easy to clarify this issue, however, until today, we still cannot ensure that the algorithm for this problem is valid for all possible inputs-that is, no one has proved that this process is terminated for All integers. Input description Input description

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

The second row is t positive integer n.

Output description Output description

For each positive integer N, each row outputs a number of S, indicating the number of steps through which N is transformed to 1. If n cannot be changed to 1, the output is-1.

Sample Input Sample Input

3

1 2 3

Sample output Sample output

0

1

7

Data range and prompt Data size & hint

1 <= T <= 100

1 <= n <= 10000

 

 1 #include <iostream> 2 using namespace std; 3  4 int main() 5 { 6     int T; 7     cin>>T; 8     for(int i = 0; i < T; i++) 9     {10         int count = 0;11         int n;12         cin>>n;13         while(n > 1)14         {15             if(n %2 == 0)16                 n = n/2;17             else18                 n = 3*n + 1;19             count++;20         }21         cout<<count<<endl;22     }23     return 0;24 }
View code

 

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.