Recursive Explanation (iv)

Source: Internet
Author: User

1  Public classA {2     Static intK=0;3     Static intTemp_k=0;4     Static intP=0;5     Static intQ=0;6      Public Static voidMain (String args[]) {7         Char[] ss = {' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '};8Permutation (SS, 0);9         if(Temp_k = =k) {Ten System.out.println (k); One System.out.println (p); A System.out.println (q); -         } -     } the      Public Static voidPermutation (Char[] SS,inti) { -          -         if(ss = =NULL|| I < 0 | | i >ss.length) { -             return; +         } -         if(i = =ss.length) { +String A =NewString (ss);  Ak++; atTemp_k =K; -             if(k==3628800) - System.out.println (a); -}Else { -              for(intj = i; J < Ss.length; J + +) { -                 Chartemp = Ss[j];//swaps the prefix so that it produces the next prefix inSS[J] =Ss[i]; -Ss[i] =temp; top++; +Permutation (ss, I + 1); -q++; thetemp = Ss[j];//swap the prefixes back to continue with the previous prefix arrangement. *SS[J] =Ss[i]; $Ss[i] =temp;Panax Notoginseng             } -         } the     } +}

The number of 10 numbers arranged, K storage total arrangement, p, q are stored in the stack and the number of times out of the stack. By the way, the final arrangement is printed, the result:

5123451234362880098641009864100

The total number of permutations is 10!
The calculation law is as follows:

N 1 2 3 4 5 6 10
K 1 2 6 24 120 720 10!
P 1 4 15 64 325 1956 9864100

The visible algorithm is less efficient, the number of recursive stacks is much larger than the number of full permutations, and with the increase of n value, p/n tends to approximate the natural logarithm e.

Recursive Explanation (iv)

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.