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)