1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include<stdio.h> #include<malloc.h>
void permutations(int *a, int m, int n); void exchange(int *a, int m, int n); void swap(int *dataone, int *datatwo);
int main() { int n = 4; int i, *p; p = (int*)malloc(n*(sizeof(int))); for (i = 0; i < n; ++i) { p[i] = i + 1; } permutations(p, 0, n); }
void permutations(int *a, int m, int n) { int i,j; if(m == n) { for(i = 0;i < n; i++) printf("%d", a[i]); printf("\n"); } else { for(j = m;j < n; j++) { exchange(a, m ,j); permutations(a, m + 1, n); swap(&a[j], &a[m]); } } }
void exchange(int *a, int m, int n) { int tmp = a[m]; a[m] = a[n]; a[n] = tmp; }
void swap(int *dataone, int *datatwo) { int tmp = *dataone; *dataone = *datatwo; *datatwo = tmp; }
|