3038 3n + 1 problem, 30383n
3038 3n + 1 Problems
Time Limit: 1 s space limit: 32000 KB title level: Silver QuestionDescription
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 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 int tot=0; 6 int hasse(int x) 7 { 8 9 if(x==1)return tot;10 if(x<1)return -1;11 if(x%2==1)12 {13 tot++;14 hasse(3*x+1);15 }16 else 17 {18 tot++;19 hasse(x/2);20 }21 }22 int main()23 {24 int n;25 cin>>n;26 for(int i=1;i<=n;i++)27 {28 tot=0;29 int x;30 cin>>x;31 cout<