1 #include <stdio.h> 2 3 int main() 4 { 5 int n,i , j, a[100], a1[100]; 6 while(scanf("%d", &n) == 1) 7 { 8 for(i = 0; i<n; i++) 9 { 10 scanf("%d", &a1[i]); 11 } 12 13 for (i = n-1; i>0; i--) 14 { 15 for (j = 0; j<i; j++) 16 { 17 if (a1[j] > a1[j+1]) 18 { 19 int t; 20 t = a1[j]; 21 a1[j] = a1[j+1]; 22 a1[j+1] = t; 23 } 24 } 25 } 26 print_permutation(a, a1, n, 0); 27 } 28 } 29 30 void print_permutation(int *a, int *a1, int n, int cur) 31 { 32 int i; 33 if (cur == n) 34 { 35 for (i = 0; i<n; i++) 36 { 37 printf("%d ", a[i]); 38 } 39 40 printf("\n"); 41 } 42 else 43 { 44 for (i = 0; i<n; i++) 45 { 46 int j, ok = 1; 47 for (j = 0; j<cur; j++) 48 { 49 if (a[j] == a1[i]) 50 { 51 ok = 0; 52 break; 53 } 54 } 55 56 if (ok) 57 { 58 a[cur] = a1[i]; 59 print_permutation(a, a1, n, cur+1); 60 } 61 } 62 } 63 }
1 #include <stdio.h> 2 3 int main() 4 { 5 int n,i , j, a[100], a1[100]; 6 while(scanf("%d", &n) == 1) 7 { 8 for(i = 0; i<n; i++) 9 { 10 scanf("%d", &a1[i]); 11 } 12 13 for (i = n-1; i>0; i--) 14 { 15 for (j = 0; j<i; j++) 16 { 17 if (a1[j] > a1[j+1]) 18 { 19 int t; 20 t = a1[j]; 21 a1[j] = a1[j+1]; 22 a1[j+1] = t; 23 } 24 } 25 } 26 print_permutation(a, a1, n, 0); 27 } 28 } 29 30 void print_permutation(int *a, int *a1, int n, int cur) 31 { 32 int i; 33 if (cur == n) 34 { 35 for (i = 0; i<n; i++) 36 { 37 printf("%d ", a[i]); 38 } 39 40 printf("\n"); 41 } 42 else 43 { 44 for (i = 0; i<n; i++) 45 if( !i || a1[i]!=a1[i-1]) 46 { 47 int j, c1 = 0, c2 =0, ok = 1; 48 for (j = 0; j<cur; j++) if (a[j] == a1[i]) c1++; 49 for (j = 0; j<n; j++) if (a1[j] == a1[i]) c2++; 50 if (c1<c2) 51 { 52 a[cur] = a1[i]; 53 print_permutation(a, a1, n, cur+1); 54 } 55 } 56 } 57 }
时间: 2024-10-17 19:03:44