N个数中选M个数的组合问题
#define N 5 #define M 4 char answer[N]; void Print(char a[], int n) { for (int i=0; i<n; i++) { cout << a[i] << " "; } cout << endl; } void Combination(char a[], int b, int n, int k) { if (k == 0) { Print(answer, M); return; } if (k==n) { while(k>0) answer[--k] = a[b++]; Print(answer,M); return; } answer[k-1] = a[b]; Combination(a, b+1, n-1,k-1); Combination(a, b+1, n-1, k); } int main(int argc, char* argv[]) { char a[N] = {'a','b','c', 'd', 'e'}; Combination(a, 0, N, M); return 0; }