Skip to content

Instantly share code, notes, and snippets.

@localmin
Created October 31, 2017 13:38
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 localmin/fe576a8401a585445484e238333ee5d5 to your computer and use it in GitHub Desktop.
Save localmin/fe576a8401a585445484e238333ee5d5 to your computer and use it in GitHub Desktop.
Get a subset of [1, 2, ... n], by the Reverse Search Method. (Input is n.)
#include <stdio.h>
#include <stdlib.h>
void ReverseSearch(int x[], int i, int n){
int j;
printf("{ ");
if(i == 0)printf("{} ");//Empty set
for(j = 1; j <= n ; j++){
if(x[j] == 1)printf("%d ", j);
}
printf("}\n");
if(i == n){
return;
}
for(j = i + 1; j <= n; j++){
//Add a element
x[j] = 1;
ReverseSearch(x, j, n);
// Remove a max element of the set.
x[j] = 0;
}
}
int main(int argc, char *argv[]){
int n, j;
n = atoi(argv[1]); // The input number, n.
int x[n];
for(j = 0; j <= n; j++){
x[j] = 0;
}
x[0] = 1;
ReverseSearch(x, 0, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment