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
| #include <stdio.h>
void swap(int list[], int i, int j) { int temp = list[i]; list[i] = list[j]; list[j] = temp; }
void permute(int list[], int k, int m) { if (k == m) { for (int i = 0; i <= m; i++) { printf("%d ", list[i]); } printf("\n"); } else { for (int i = k; i <= m; i++) { swap(list, k, i); permute(list, k + 1, m); swap(list, k, i); } } }
int main() { int list[] = {1, 2, 3}; int n = sizeof(list) / sizeof(list[0]); permute(list, 0, n - 1); return 0; }
|