Re: All Permutations of 10 Things Taken 7 at a Time?
On Jan 10, 11:55 pm, sherman wrote:
What it amounts to is: pick all combinations of 7 numbers out of 10,
and then all permutations of them.
Consider an algorithm like this for the first part:
template <class T>
void nChooseK(std::vector<T> const & items, int const k, std::vector<
std::vector<T> > & results)
{
assert( k >= 0 );
if(k == 0) // if we are choosing zero items
{ // there is only one way to choose zero items, and that is an
empty set
results.push_back( std::vector<T>() );
}
else if(k <= items.size())
{
for(std::vector<T>::const_iterator iter = items.begin();
iter != items.end(); ++iter)
{
std::vector<T> stripped_items(iter+1, items.end());
std::vector< std::vector<T> > stripped_results;
nChooseK(stripped_items, k-1, stripped_results);
for(std::vector< std::vector<T> >::iterator inner_iter =
stripped_results.begin(); inner_iter != stripped_results.end(); +
+inner_iter)
{
inner_iter->push_back(*iter);
results.push_back(*inner_iter);
}
}
}
// else we are being asked to select more than the number of items
we
// have, and there is no way to do that so don't add anything to
the
// result
}
And std::next_permutation for the next part.