Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Generating the subsets of a set.
#include <iostream>
#include <fstream>
#include <vector>
#define FIN "submultimi.in"
#define FOUT "submultimi.out"
using namespace std;
int main() {
int n;
ifstream fin(FIN);
ofstream fout(FOUT);
fin>>n;
vector<int> stack;
stack.push_back( 1 );
while(!stack.empty()) {
for(vector<int>::size_type i = 0; i < stack.size(); ++i) fout<<stack[i]<<" ";
fout<<endl;
if(stack.back() < n) {
stack.push_back(stack.back() + 1);
} else {
stack.pop_back();
if(!stack.empty()) stack.back()++;
}
}
return(0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment