PAT class B-1001-(3n+1) conjecture

Callatz conjecture:

For any natural number n, if it is even, cut it in half; if it is odd, cut (3n+1) in half. In this way, it has been chopped repeatedly, and finally n = 1 must be obtained at a certain step. Karaz announced this conjecture at the World Congress of Mathematicians in 1950. It is said that Yale University teachers and students mobilized together to try to prove this seemingly silly and naive proposition. +1), so that some people say that this is a conspiracy, Karaz is deliberately delaying the progress of teaching and research in the American mathematics community...

Our topic today is not to prove the Karaz conjecture, but for any given positive integer n up to 1000, simply count how many steps (how many chops) are needed to get n=1?

Input format: Each test input contains 1 test case, that is, the value of the natural number n is given.

Output format: Output the number of steps required to calculate from n to 1.

Input sample:
3
Sample output:
5

Very simple, the answer is straight to the point.

#include<iostream>

using namespace std;

intmain()
{
  int a;
  cin >> a;
  if (a == 0)
    return 0;
  int nums = 0;
  while (a != 1)
  {
    if (a % 2 == 0)
      a /= 2;
    else
      a = (3 * a + 1) / 2;
    nums++;
  }
  cout << nums<<endl;

  return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324846303&siteId=291194637