#include <iostream>using namespacestd;voidPermintList[],intKintm);//StatementvoidPermintList[],intKintM//called{ if(k==m)//equal to the output, always output the last layer. { for(intj=0; j<=m;j++) {cout<<List[j]; } cout<<Endl; } Else { for(inti=k;i<=m;i++) {swap (list[k],list[i]);//On The first floor, change with yourself first. 1 and 1 for, 2 and 2 for, 3 is equal then output. Output back, 2 and 3 to replace. Perm (list,k+1, M); Swap (list[k],list[i]);//Change The order of the previous changes back to prevent the order from changing. } }}intMain () {//Char a[7]= "123456"; inta[3]={0,1,2}; Perm (A,0,2); System ("Pause"); return 0;}
Logical Order:
The first layer Loops 3 times:
K=0,i=k=0,list[0] and list[0] Exchange. And then call k+1=1,2 recursively.
Go to the second layer and loop 2 times:
K=1,I=K=1,LIST[1] and List[1] exchange, and then recursively call k+1=2,2
At this time equal, output result, 0,1,2 (equivalent to No loop or loop 1 times)
And then back to the second layer, the 2nd time of the second layer.
K=1,I=K+1=2,LIST[1] and list[2] exchange, and then recursively call k+1=2,2
Equal at this time, output 0,2,1
Then go back to the first layer, cycle the first layer for the 2nd time, followed by the same process.
C + + recursive method for full array