Skip to content

Instantly share code, notes, and snippets.

@caljim
Created February 25, 2013 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caljim/5031089 to your computer and use it in GitHub Desktop.
Save caljim/5031089 to your computer and use it in GitHub Desktop.
choose m element from n-set
#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