元素無重復:
如:2,5,8,9.
思路:用遞歸的方法解決,對於2589,先輸出所有以2開頭的排列,然后輸出5開頭的排列.....(此處稱為遞歸操作A)。以2開頭的排列中,第一位是2,后面的是589,然后對589執行相同的遞歸操作A......
代碼如下:
1 #include<iostream> 2 using namespace std; 3 void print_permutation(int n,int *A,int *B,int cur){//n為目標數組長度,cur為對全排列序列A遞歸操作的下標 4 if(cur==n){//遞歸出口 5 for(int i=0;i<n;i++)//打印全排列序列 6 cout<<A[i]<<" "; 7 cout<<endl; 8 } 9 else 10 for(int i=0;i<n;i++){//掃描目標數組 11 int ok=1; 12 for(int j=0;j<cur;j++){ 13 if(A[j]==B[i]){ 14 ok=0;//對A中已有元素(cur下標前的元素)進行掃描,若發現A中已經存在B[i]的值時,說明B[i]元素已經排列過。 15 break; 16 } 17 } 18 if(ok){//若B[i]未排列過,則將B[i]添加到A序列末尾,將cur指針后移一位,進行下一步遞歸操作。 19 A[cur]=B[i]; 20 print_permutation(n,A,B,cur+1); 21 } 22 } 23 } 24 25 int main(){ 26 int a[]={0};//全排列序列,即輸出結果 27 int b[]={2,5,8,9};// 目標數組 28 print_permutation(4,a,b,0); 29 return 0; 30 }
元素有重復:
修改兩個地方:
1.因為元素可以重復,所以不能再用“A中是否已經存在B數組”的條件來決定是否添加B[i]元素。取而代之,分別對A,B數組掃描,只要A中的B[i]不超過B中的B[i]即可。
2.在題目要求上,重復元素屬於相同元素時,如1123的排列中1123和1123是等價的,只能算一種。我們最好先對B數組排個序(排序復雜度是nlogn,相較於兩層循環的代,影響並不是很大),然后在最外層循環添上一個控制條件(i==0||B[i]!=B[i-1]),為的就是防止重復的元素又一次排列。
代碼如下:
1 #include<iostream> 2 using namespace std; 3 void print_permutation(int n,int *A,int *B,int cur) { 4 if(cur==n) { 5 for(int i=0; i<n; i++) 6 cout<<A[i]<<" "; 7 cout<<endl; 8 } else 9 for(int i=0; i<n; i++) { 10 if(!i||B[i]!=B[i-1]) { 11 int c1=0,c2=0; 12 for(int j=0; j<cur; j++)//掃描A中已有B[i]的個數 13 if(A[j]==B[i]) c1++; 14 for(int j=0; j<n; j++)//掃描B中所有B[i]的個數 15 if(B[j]==B[i]) c2++; 16 if(c1<c2) { 17 A[cur]=B[i]; 18 print_permutation(n,A,B,cur+1); 19 } 20 } 21 } 22 } 23 24 int main() { 25 int a[]= {0}; 26 int b[]= {1,1,2,4}; 27 print_permutation(4,a,b,0); 28 return 0; 29 }