Skip to content

Instantly share code, notes, and snippets.

@tonytonyjan
Created November 23, 2015 18:08
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 tonytonyjan/eea7a8bc4a731e1aeb3d to your computer and use it in GitHub Desktop.
Save tonytonyjan/eea7a8bc4a731e1aeb3d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define SWAP(a,b) do{ typeof(a) tmp = a; a = b; b = tmp; }while(0)
void permutate(int* v, int n, int i) {
int j;
if (i == n) {
for(j = 0; j < n; j++) printf("%d ", v[j]);
printf("\n");
}else
for(int j = i; j < n; j++) {
SWAP(v[i], v[j]);
permutate(v, n, i+1);
SWAP(v[i], v[j]);
}
}
void combinate(int v[], int start, int n, int k, int maxk) {
int i;
if(k > maxk) {
permutate(v+i, maxk, 0);
return;
}
for(i=start; i<=n; i++) {
v[k] = i;
combinate(v, i+1, n, k+1, maxk);
}
}
int main(){
int v[] = {1,2,3,4,5,6,7,8,9};
combinate(v, 1, 9, 1, 4);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment