-> Code vs 3038 3n+1 problem (Recursive)

Source: Internet
Author: User

3038 3n+1 Problemstime limit: 1 sspace limit: 32000 KBtitle level: Silver Solving Title Description Description The 3n+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 the n=1 ends;(3) If n is odd, n becomes 3n+1, otherwise n becomes N/2;(4) Transfer 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

Idea: Simple recursive problem, simulation, according to the topic steps, data polar water, there is a condition useless (if n can not become 1, then output-1.) Water problem!! Bad comment!? (~ ̄ (OO)  ̄) ブ

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;intt,n,s;voidWorkintN) {    if(n==1)return; Else if(n%2!=0) n=3*n+1; Else if(n%2==0) n=n/2; S++; Work (n);}intMain () {CIN>>T;  for(intI=1; i<=t;i++) {cin>>N; S=0;        Work (n); cout<<s<<Endl; }    return 0;}

If it helps you, do not forget to add praise Oh, the clatter!! See you next time! the

-> Code vs 3038 3n+1 problem (Recursive)

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.