You are using a version without Ads of this website. Please, consider donating:

### all combination question

Hello all

for example let say there is vector [1,2,3,4,5,6,7,8,9]
and let say there is int n

if i say n=3
I want to find all possible 3 combination
[1,2,3][1,2,4].....such and such

if i say n=4
[1,2,3,4][1,2,3,5]......

I want to know how you write the code depend on the input n
is it call dynamic programming?
It's not a programming problem. It's a discrete Math problem. Solve that, and the program will write itself.
U don't understand the question dude
Er, not these days, no.

A simple solution using the STL
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include #include #include #include using namespace std; size_t factorial( size_t n ) { size_t result = 1; while (n > 1) result *= n--; return result; } template bool next_take_r( size_t r, Iterator begin, Iterator end ) { bool result = false; size_t n = factorial( distance( begin, end ) - r ); for (size_t x = 0; x < n; x++) result = next_permutation( begin, end ); return result; } int main() { vector ns { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; size_t n = 3; do { copy_n( ns.begin(), min( n, ns.size() ), ostream_iterator ( cout, " " ) ); cout << "\n"; } while (next_take_r( n, ns.begin(), ns.end() )); return 0; }``````

Compile with C++11 support.

Enjoy!
Thanks

is visual studio 2012 support c++11?
Topic archived. No new replies allowed.