permutation algorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort

int main () {
  int myints[] = {1,2,3};

  std::sort (myints,myints+3);

  std::cout << "The 3! possible permutations with 3 elements:\n";
  do {
    std::cout << myints[0] << ' ' << myints[1] <<' ' << myints[2] << '\n';
  } while ( std::next_permutation(myints,myints+3) );

  std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';

  return 0;
}


this will result


123
132
213
231
312
321


The question is
if i want to get a permutaion combination with range of N
what should i do?
if N = 2
result should be 12,13,21.....such and such
eliminate the last digit is working for 3 combination
but if its going to a bigger number it does not work

any idea?
Ah, so all this combinations stuff is homework.

See my response to your classmate.
http://www.cplusplus.com/forum/general/108307/#msg588575

However, if your professor is asking you about combinations, is he asking for you use the STL or would he prefer you to write your own method to create combinations?
Hey,
that is not the solution i want

let say {1,2,3,4,5,6,7,8,9}
and i want n=3 permutation

I want to see

123,124,125.... such and such

the answer you showed me show all the possible combination 9!

No, the answer I showed you gives you exactly the what you are looking for.

And BTW, n=9 and r=3, or n points taken r at a time is nPr.
oh ok do u have answer without STL?
If i want to use the each numbers
what should i do?

when permutaion is 2,3,4
i want to use 2 and 3 and 4

im preparing this problem for magic square problem

and

what is copy_n( ns.begin(), min( n, ns.size() ), ostream_iterator <int> ( cout, " " ) ); mean?
Last edited on
Topic archived. No new replies allowed.