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
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
38
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;

size_t factorial( size_t n )
  {
  size_t result = 1;
  while (n > 1)
    result *= n--;
  return result;
  }

template <typename Iterator>
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 <int> 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 <int> ( 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.