1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
void combinations_r_recursive(const vector<char> &elems, unsigned long req_len,
vector<unsigned long> &pos, unsigned long depth,
unsigned long margin)
{
// Have we selected the number of required elements?
if (depth >= req_len)
{
for (unsigned long ii = 0; ii < pos.size(); ++ii)
myfile << elems[pos[ii]];
myfile << "," << endl;
cout << ". ";
return;
}
// Try to select new elements to the right of the last selected one.
for (unsigned long ii = 0; ii < elems.size(); ++ii)
{
pos[depth] = ii;
combinations_r_recursive(elems, req_len, pos, depth + 1, ii);
}
return;
}
void combinations_r(const vector<char> &elems, unsigned long req_len)
{
assert(req_len > 0 && req_len <= elems.size());
vector<unsigned long> positions(req_len, 0);
combinations_r_recursive(elems, req_len, positions, 0, 0);
}
const unsigned long num_elements = 25;
const unsigned long comb_len = 4;
|