Created
February 25, 2013 16:36
-
-
Save caljim/5031089 to your computer and use it in GitHub Desktop.
choose m element from n-set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
void combine(int src[], int s, int e, int k, int take[], int p) | |
{ | |
if(k == e - s + 1) { | |
for(int i = 0; i < p; i++) | |
printf("%d,",take[i]); | |
for(int i = s; i <= e; i++) | |
printf("%d,",src[i]); | |
printf("\n"); | |
return ; | |
} | |
else if(k == 1) { | |
for(int i = s; i <= e; i++) { | |
for(int j = 0; j < p; j++) | |
printf("%d,",take[j]); | |
printf("%d\n",src[i]); | |
} | |
return ; | |
} | |
else { | |
int i = p; | |
take[i++] = src[s]; | |
combine(src, s+1, e, k-1, take, i); | |
combine(src, s+1, e, k, take, p); | |
} | |
} | |
int main() | |
{ | |
int a[9] = {1,2,3,4,5,8,9,10,11}; | |
int b[3] = {0}; | |
combine(a,0,8,3,b,0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment