Java 1005. Continuation (3n+1) conjecture (25)

Source: Internet
Author: User

Thinking out of the question, always feel that a number is not the key number, and in addition to the number of each number of coverage list comparison, so that, enter n number, there will be N list, each number and n-1 a list comparison, but in fact there is no need.

Because each number of overlays will not include itself, if included is the death cycle, can never get 1. So directly calculate the number of coverage, and then take the number of inputs to compare.

The container that receives the number of overlays, with hashset, so that there is no need to judge whether there are duplicates.

Code:

ImportJava.util.ArrayList;ImportJava.util.Collections;ImportJava.util.HashSet;ImportJava.util.List;ImportJava.util.Scanner;PublicClassMain {PublicStaticvoid Main (String[] args) {hashset<Integer> hs=Newhashset<Integer> ();list<Integer> alist=Newarraylist<Integer> ();Scanner s=NewScanner (system.in);int A=s.nextint ();int arr[]=NewInt[a];int arr1[]=NewInt[a];Forint i=0;i<a;i++) {arr[i]=s.nextint (); arr1[i]=arr[i];}Forint i=0;i<a;i++) {while (arr[i]!=1) {if (arr[i]%2==0) {arr[i]/=2; }else{arr[i]= (arr[i]*3 +1)/2;} Hs.add (Arr[i]); }} for (int i=0;i<a;i++) { if (!hs.contains (Arr1[i])) {Alist.add (arr1[i]);}} collections.sort (alist); if (alist.size () ==1) {system.out.print (Alist.get (0));} else{for (int i=alist.size ()- 1;i>=0;i--) {system.out.print ( Alist.get (i)); if (i!=0) {system.out.print (     
Topic:

When we verify Karaz conjecture, in order to avoid repetition, we can record each number that is encountered in the recursive process. For example, when validating the n=3, we need to calculate 3, 5, 8, 4, 2, 1, then when we verify the n=5, 8, 4, 2, we can directly determine the authenticity of Karaz conjecture, and do not need to repeat the calculation, because this 4 number has been in the validation of 3, we call 5, 8, 4, 2 is the number that is 3 "covered". We call a number N in a series A "key number" if n cannot be overridden by other numbers in the sequence.

Now given a series of numbers to be validated, we just need to validate a few of the key numbers, so we don't have to re-validate 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 cases, the 1th line gives a positive integer k (<100), and line 2nd gives the value of k distinct positive integer n (1<n<=100), separated by a space between the numbers.

output format: the output of each test case occupies one row, and the key numbers are output in order from large to small. The number is separated by 1 spaces, but there is no space after the last digit in a row.

Input Sample:
63 5 6 7 8 11
Sample output:
7 6

Java 1005. Continuation (3n+1) conjecture (25)

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.