#include #include #define MAX_SIZE 100 #define DEFAULT_N 10 /* Recursive function to generate all cyclic permutations of 0, 1, ..., n-1. Each such permutation begins with 0. A cyclic permutation is printed if and only if A[1] < A[n-1]. */ void allcyclicperms ( int A[], int n, int k, int *nperms, int *ncalls ) { int i, t; ++(*ncalls); if (k == n-1) { /* Only one remaining element */ /* Check whether the cyclic permutation is already printed. This check avoids duplicate printing, but does not reduce the cost of the generation of the duplicate permutations. */ if (A[1] < A[n-1]) { ++(*nperms); printf("Permutation Number %6d: ", *nperms); for (i=0; i A[1]) break; if (j < n) gencyclicperms(A,n,k+1,nperms,ncalls); t = A[k]; A[k] = A[i]; A[i] = t; } } int main ( int argc, char *argv[] ) { int n, A[MAX_SIZE], i, nperms, ncalls; if (argc > 1) n = atoi(argv[1]); else scanf("%d", &n); if ((n < 3) || (n > DEFAULT_N)) n = DEFAULT_N; /* Initially store the permutation 0, 1, 2, ..., n-1 in A[] */ for (i=0; i