Test Instructions: given a 1~n n positive integers, write out all of their order.
idea : According to the knowledge of high school, we know that the result of non-repetition is n! order, in the procedure we first see how to implement it with recursion.
For example, the full arrangement of the array {# * *} is 123,132,213,231.312.321.
Can see a little rule, we exchanged the first number, respectively, from 1 to 3, followed by the rest of the number of the entire arrangement, here can be seen should be used recursive implementation.
algorithm: first the first number and each digit are exchanged sequentially, then recursively called, and finally the swapped array is restored for the next exchange.
my own doubts: at the beginning to see the topic of the settlement law, although can understand the idea, but see the code still can not fully understand, oneself also try to simulate each step in the recursion, but found that it is too complex, and finally read a lot of people's blog to the full array of explanations, To understand the meaning of the code from the basic idea.
Code:
voidPermintA[],intNintm) { if(n = =m) { for(intI=0; i<m;i++) cout<<a[i]<<" "; cout<<"\ n"; } Else { for(intj = n;j<m;j++) {swap (A[n], a[j]); Perm (A,n+1, M); Swap (a[j],a[n]); } }}
Recursive thought and implementation of the full permutation algorithm