Java ArrayDeque method of use

  • 2020-05-09 18:32:03
  • OfStack

Title requirements are:
The Callatz conjecture:

For any natural number n, if it's even, cut it by a half; If it's an odd number, cut it by a half. So I'm going to cut it down again and again, and then I'm going to get n is equal to 1. When we verify the karatz conjecture, to avoid double counting, we can record every number encountered in the recursion. For example to verify this n = 3, we need to calculate 3, 5, 8, 4, 2, 1, when we have n = 5, 8, 4, 2 to verify, can directly judge kara we suspect the authenticity of, without the need for repeated calculation, because the number 4 has encountered when validation 3, we said, 4, 5, 8, 2 is 3 "cover" number. We call a number in a sequence n a "key number" if n cannot be overwritten by other Numbers in the sequence.

Now given a series of 1 Numbers to verify, we only need to verify a few of the key Numbers, so we don't have to repeat the validation of the remaining Numbers. Your task is to find these key Numbers and output them in order from large to small.

Input format: each test input contains 1 test case, and line 1 shows a positive integer K( < 100), line 2 shows K different positive integers n(1) to be verified < n < =100), separated by Spaces.

Output format: the output of each test case is one line, with key Numbers in order from large to small. The Numbers are separated by a space, but there is no space after the last number in the line.

Input sample:
6
3 5 6 7 8 11
Output sample:
7 6

The code is as follows:


<span style="font-size:14px;"> 
import java.util.ArrayDeque; 
import java.util.ArrayList; 
import java.util.Deque; 
import java.util.List; 
import java.util.Queue; 
import java.util.Scanner; 
import java.util.SortedSet; 
import java.util.TreeSet; 
 
public class PAT1005 { 
 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
 
    Scanner scanner = new Scanner(System.in); 
    int numSize=scanner.nextInt(); 
    ArrayDeque<Integer> newArrayDeque=new ArrayDeque<Integer>(); 
    ArrayDeque<Integer> closeArrayDeque=new ArrayDeque<Integer>(); 
    int i; 
    while (scanner.hasNext()) {           // Read the keyboard input value  
      for (i = 0; i < numSize; i++) { 
        newArrayDeque.add(scanner.nextInt()); 
         
      } 
      if (i>=numSize) { 
        break; 
      } 
    } 
    int temp; 
    for (Integer integer : newArrayDeque) {    // Store the non-key number closeArrayDeque In the  
      temp=integer;  
      while (temp!=1) { 
        if (temp%2==0) { 
          temp=temp/2; 
          if (newArrayDeque.contains(temp)) { 
            closeArrayDeque.add(temp); 
          } 
           
        }else { 
          temp=(temp*3+1)/2; 
          if (newArrayDeque.contains(temp)) { 
            closeArrayDeque.add(temp); 
          } 
        } 
      } 
    } 
    SortedSet<Integer> sortedSet=new TreeSet<Integer>(); //sortedSet Used to store key Numbers  
    for (Integer integer : newArrayDeque) { 
      if (!closeArrayDeque.contains(integer)) { 
        sortedSet.add(integer); 
      } 
    } 
    int[] leftInt=new int[sortedSet.size()]; 
    int j=sortedSet.size()-1; 
    for (Integer integer : sortedSet) { 
      leftInt[j]=integer; 
      j--; 
    } 
    for (int j2 = 0; j2 < leftInt.length; j2++) {    // Output key Numbers in order from large to small  
      if (j2==leftInt.length-1) { 
        System.out.println(leftInt[j2]); 
      }else { 
        System.out.print(leftInt[j2]+" "); 
      } 
       
    } 
  } 
 
} 
</span> 

The above is the entire content of this article, I hope to help you with your study.


Related articles: