Recursive output all stack order and Catalain number application

Source: Internet
Author: User

Using the ArrayList simulation stack operation, recursive algorithm is adopted. The code is as follows:

1 Importjava.util.ArrayList;2 ImportJava.util.Scanner;3 4  Public classStackall {5     Static intNum=0;6      Public Static voidMain (string[] args) {7Scanner scanner=NewScanner (system.in);8         intN=scanner.nextint ();//number of input elements9Arraylist<integer> stackin=NewArraylist<integer>();Ten          for(inti=n;i>0;i--)//initialize into stack queue One Stackin.add (i); A          -         Longstart=System.currenttimemillis (); -Stackout (Stackin,NewArraylist<integer> (),NewArraylist<integer> ());//Call Recursion theSYSTEM.OUT.PRINTLN (num);//total output after final completion -System.out.println ((System.currenttimemillis ()-start)/1000f);//time consumption calculation -     } -      +      Public Static voidStackout (arraylist<integer> stackin,arraylist<integer> stack,arraylist<integer>stackout) { -         if(Stackin.size () ==0) {//whether the queued queue is empty +             if(Stack.size () ==0) {//stack is empty A                  for(intX:stackout)//if both the queue and the stack are empty, then the stack is complete and the stack queue is output atSystem.out.print (x+ ""); - System.out.println (); -num++;//to Count -}Else{ -Stackout.add (Stack.get (Stack.size ()-1));//The stack queue is empty, the stack is not empty, then only the stack -Stack.remove (Stack.size ()-1); inStackout (Stackin,stack,NewArraylist<integer>(Stackout)); -             } to}Else{ +             if(Stack.size () ==0) {//The stack queue is not empty, stack empty, then into the stack -Stack.add (Stackin.get (Stackin.size ()-1)); theStackin.remove (Stackin.size ()-1); *Stackout (Stackin,stack,NewArraylist<integer>(Stackout)); $}Else{Panax NotoginsengArraylist<integer> stack_copy=Newarraylist<integer> (stack);//because the callback function is performed in two cases, a copy of the data is copied here -Arraylist<integer> stackin_copy=NewArraylist<integer>(Stackin); the  +Stack_copy.add (Stackin_copy.get (Stackin_copy.size ()-1));//only into the stack AStackin_copy.remove (Stackin_copy.size ()-1); theStackout (Stackin_copy,stack_copy,NewArraylist<integer>(Stackout)); +                  -Stackout.add (Stack.get (Stack.size ()-1));//only out of the stack $Stack.remove (Stack.size ()-1); $Stackout (Stackin,stack,NewArraylist<integer>(Stackout)); -             } -         } the     } -}

The stacking order of stacks with more than 14 elements is calculated, and the time begins to be very long, with each increase of 1, the time increases exponentially.

In fact, all of the stack order may be catalain number, Catalain number almost does not need time, very fast, but the Cattleya number can only output the total number of final results, can not output each stack order of specific data, so according to the situation and use.

The following number of Catalain solves the code.

ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {System.out.println (H (NewScanner (system.in). Nextlong ())); }     Public Static LongHLongN) {        if(n==1)            return1; Else if(n==2)            return2; Else            returnH (n-1) * (4*n-2)/(n+1); }}

Recursive output all stack order and Catalain number application

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.