#include <stdio.h> #include <stdlib.h>
#define COMBINE_M 2
void swap(char* a, char* b) { char tmp = *a; *a = *b; *b = tmp; }
void combine(char A[], int n, int m, int b[]) { int i; int j; for(i=n;i>=m;i--) { b[m-1] = i-1; if(m>1) combine( A, i-1, m-1, b); else { for(j=0;j<COMBINE_M;j++) printf("%c\t",A[b[j]]); printf("\n"); } } }
void permute(char A[], int m, int n) { int i; if(m == n) { for(i=0;i<m;i++) printf("%c",A[i]); printf("\n"); } else { for(i=m;i<n;i++) { swap(A+i,A+m); permute(A,m+1,n); swap(A+i,A+m); } } }
int main(int argc, char *argv[]) { char A[] = "abcd"; int* B = (int*)malloc(strlen(A)*sizeof(int) + 1 ); printf("combile is:\n"); combine(A, 4, COMBINE_M, B); printf("permute is:\n"); permute(A,0,3); system("PAUSE"); return 0; }
|