//input: N,R
//output: array x[1..N] where x[i] == 1 means the i-th items are selected.
Choose(N,R) {
X = new array[1..n] of integer
choose(x,N,0,0,R,0,N-R)
}
choose(x,n,step,num_one,max_one,num_zero,max_zero) {
if (step == n) {
print(x); // ...or use x to do something...
} else {
if (num_zero < max_zero)
x[step+1] = 0; choose(x,n,step+1, num_one , max_one, num_zero+1, max_zero);
if (num_one < max_one)
x[step+1] = 1; choose(x,n,step+1, num_one+1, max_one, num_zero , max_zero);
}
}