PAT Class B Java Implementation_1005. Continue (3n+1) Conjecture_With Detailed Problem Solving Notes_05

1005. Continue (3n+1) Conjecture (25)

time limit
400 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
CHEN, Yue

The Callatz conjecture has been described in 1001. In this case, the situation is a little more complicated.

When we verify Karaz's conjecture, in order to avoid double calculation, we can record every number encountered in the recursion process. For example, when verifying n=3, we need to calculate 3, 5, 8, 4, 2, 1, then when we verify n=5, 8, 4, 2, we can directly determine the Karaz conjecture There is no need to repeat the calculation, because these 4 numbers have already been encountered when verifying 3, and we call 5, 8, 4, and 2 the numbers "covered" by 3. We call a number n in a sequence a "key number" if n cannot be covered by other numbers in the sequence.

Now given a series of numbers to be verified, we only need to verify a few key numbers, and we don't have to repeat the verification of the remaining numbers. Your task is to find these key numbers and output them in descending order.

Input format: each test input contains 1 test case, the first line gives a positive integer K (<100), the second line gives K different positive integers to be verified n (1<n<= 100), separated by spaces.

Output format: The output of each test case occupies one line, and the key figures are output in descending order. The numbers are separated by 1 space, but there is no space after the last number in the line.

Input sample:
6
3 5 6 7 8 11
Sample output:
7 6


--------------------------------------------------------------------------------------------------------


//Main idea: If the number A to be verified appears in the process of recursive callatz conjecture of the number B to be verified, then the number A is not a key number.
import java.util.Scanner;
import java.util.HashSet;//Use the set type to record every number encountered in the recursion process. The feature of the set is that the elements are not repeated
// Use a hash set instead of a tree set because an ordered function is not required.
import java.util.Arrays;//Call the static sorting function of Arrays to sort the array to facilitate the final descending output
public class PAT_B_1005//Submission on the PAT platform needs to change the class name to Main
{
	public static void main(String args[])
	{
		Scanner in = new Scanner(System.in);//Receive all input
		{
			int n = in.nextInt();//Read the first digit of the number, representing the number of digits that follow
			int[] num_in = new int[n];
			HashSet callatz = new HashSet();//Use the hash set to record the numbers generated during the recursive callatz conjecture
			for(int i = 0; i < n ; i++)
			{
				num_in[i] = in.nextInt();//Read the number to be verified
				int tmp = num_in[i];//Temporary variable
				while( tmp != 1)//callatz guessing process
				{
					if(tmp % 2 == 0)
						tmp = tmp / 2;
					else if(tmp % 2 == 1)
						tmp = (tmp*3+1)/2;
					callatz.add(tmp);//Record the numbers generated by the process into the hash set
				};
			}
			
			Arrays.sort(num_in);//The title requires output in descending order, so sort the array of numbers to be verified
			int flag = 1;//The flag of the formatted output, indicating that the first digital output has no spaces
			for(int i = n-1; i >= 0; i--)//The default sorting is ascending order, here the effect of descending output is achieved by traversing from back to front
			{
				if(callatz.contains(num_in[i]) == false)
				{//If the number to be verified appears in the hash set, it means that the number is not a keyword.
					if(flag == 1)
					{
						System.out.print(num_in[i]);
						flag = 0;
					}
					else
						System.out.print(" "+num_in[i]);
				}
			}
		}
	}
}



Guess you like

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