Advertisement
cd62131

Permutations

Mar 21st, 2014
1,496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #define TRUE  1
  3. #define FALSE 0
  4. #define N 4
  5. int p[N];
  6. char ok[N + 1];
  7. void show(void) {
  8.     int i;
  9.     for (i = 0; i < N; i++) printf(" %d", p[i]);
  10.     printf("\n");
  11. }
  12. void put(int pos, int k) {
  13.     int j;
  14.     p[pos] = k;
  15.     if (pos == N - 1) show();
  16.     else {
  17.         ok[k] = FALSE;
  18.         for (j = 1; j <= N; j++)
  19.             if (ok[j]) put(pos + 1, j);
  20.         ok[k] = TRUE;
  21.     }
  22. }
  23. void genperm(void) {
  24.     int k;
  25.     for (k = 1; k <= N; k++) ok[k] = TRUE;
  26.     for (k = 1; k <= N; k++) put(0, k);
  27. }
  28. int main(void) {
  29.     genperm();
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement