PAT Class B Java Implementation _1001. The (3n+1) Conjecture that Killed People Without Paying for Their Lives_With detailed problem solving notes_01

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
------------------------------------------------Dividing line -------------------------------------------------
import java.util.Scanner;

public class pat_b001//The class name needs to be changed to Main when submitting on the PAT platform
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);//Receive input numbers
		int num = in.nextInt();
		int count = 0;//Number of chops
		
		
		while(num != 1 && num != 0)//If the input number is 0 or 1, there is no need to cut
		{//If the input number is not cut to 1, it will continue to cut down
			if(num % 2 == 0)//If it is even, then cut it in half
			{
				num = num / 2;
			}
			else
			{
				num = (3*num + 1)/2;//If it is odd, then cut (3n+1) in half
			}
			count = count + 1;//Accumulate the number of cuts
		}
		
		System.out.println(count);//Number of times of output cutting
	}
}


Guess you like

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