#include #include void permuteRec ( int m, int n, char *A, int i, int j ) { if ((i == m) && (j == n)) { printf(" %s\n", A); return; } if (i < m) { A[i+j] = 'R'; permuteRec(m,n,A,i+1,j); } if (j < n) { A[i+j] = 'G'; permuteRec(m,n,A,i,j+1); } } void permuteItr ( int m, int n ) { unsigned int i, j, t; int R, G; if (m + n > 31) { printf("!!! Sorry, the iterative method will not work...\n"); return; } for (i=0; i<(1U<<(m+n)); ++i) { t = i; R = G = 0; for (j=0; j>= 1; } if ((R == m) && (G == n)) { printf(" "); t = i; for (j=0; j>= 1; } printf("\n"); } } } int main () { int m, n; char *A; do { printf("m = "); scanf("%d", &m); } while (m <= 0); do { printf("n = "); scanf("%d", &n); } while (n <= 0); printf("\n+++ Recursive method\n"); A = (char *)malloc((m + n + 1) * sizeof(char)); A[m+n] = '\0'; permuteRec(m,n,A,0,0); printf("\n+++ Iterative method\n"); permuteItr(m,n); exit(0); }