Skip to content

Instantly share code, notes, and snippets.

@jose-villegas
Created October 23, 2014 00:23
Show Gist options
  • Save jose-villegas/2168caab29cf6bae2880 to your computer and use it in GitHub Desktop.
Save jose-villegas/2168caab29cf6bae2880 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
int factorial(int x);
void combn(int n, int m, vector<vector<int> > *result);
vector<int> currentCombination;
vector<int> startingFormat;
int main()
{
vector<vector<int> > combinations;
int numDeposits = 6;
int numCollectors = 3;
//int numCombinations = factorial(numDeposits) / factorial(numCollectors) * (factorial(numDeposits - numCollectors));
startingFormat.resize(numDeposits);
fill(startingFormat.end() - numCollectors, startingFormat.end(), 1);
combn(3, 6, &combinations);
for (int i = 0; i < combinations.size(); ++i)
{
for (int j = 0; j < combinations[i].size(); ++j)
{
cout << combinations[i][j];
}
cout << endl;
}
cout << "HEY" << endl;
return 0;
}
int factorial(int x)
{
return (x == 1 || x == 0) ? 1 : factorial(x - 1) * x;
}
void combn(int n, int m, vector<vector<int> > *result)
{
if (n == 0)
{
result->push_back(currentCombination);
return;
}
for (int i = n; i < m; ++i)
{
currentCombination.push_back(startingFormat[i]);
combn(i + 1, m - 1, result);
currentCombination.pop_back();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment