Permutation Help?

So i know how to do permutation when you have numbers listed as 1,2,3,4 but how do u write a program where the input file is just "15 , 10"and you have to make 10 permutations for 1-15? I have to do it using a source set
Last edited on
All you have to do is generate an array with the numbers in the range 1-15, then pass that array to the backtracking function which does the permutations. In the backtracking algorithm, simply add a counter and when it reaches 10, use return/break (depending on your implementation recursive/iterative).

Raul
can i plz get like a pseudocode ?
Well, you can put here the pseudocode for the 1,2,3,4 permutation (as you mentioned, you know how to do that), and I'll just tell you where to make changes. Actually, if I had more time right now, I would write it myself, but I'm a little busy, sorry for that.

Raul
There are numerous tutorials on the internet, including one on this site, explaining how to use files in C++. As long as you don't use .open(), .close(), or .eof(), you should be fine with any tutorial.
I am confused because i have use 2 vectors and regenerate random permutation
If you are using vectors can you use the built in std::next_permutation or do you have to use your own? Either way just get the next permutation 10 times and you are good to go.
i have to use my own?? Help
Is that a question or a statement?
statement sorry..
So i have use my own.
Make 15 random permutation (vector)
and 10 is the highest number in each permutation (vector)
Can you not call your permutation function 10 times with the 15 random numbers?
how do u do that ?
Okay do you have to make your permutation function or do you have it done already? Also, if you could post what you have so far it would be helpful.
i am not sure if i am doing this right
void permutaion(const vector<int> &now,
vector<int> &next)
{
int size=now.size();
if(size>0)
{
for(int cnt=0; cnt<size;++cnt)
{
vector<int> vt;

vector<int>::const_iterator it=now.begin();
for(int cnt1=0;cnt1<size;++cnt1)
{
if(cnt1==cnt)
{
++it;
continue;
}
else
.push_back(*it);
++it;
Honestly I am not sure what you are doing(or trying) in your code. It looks a bit odd.

If the upper limit is 15 instead of 10 then so you could have something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 3 4 5 6 7 8 9 10 11 12 13 15 14
1 2 3 4 5 6 7 8 9 10 11 12 14 13 15
1 2 3 4 5 6 7 8 9 10 11 12 14 15 13
1 2 3 4 5 6 7 8 9 10 11 12 15 13 14
1 2 3 4 5 6 7 8 9 10 11 12 15 14 13

...

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

These links may help you:
http://stackoverflow.com/questions/11483060/stdnext-permutation-implementation-explanation
http://www.cplusplus.com/reference/algorithm/next_permutation/
http://en.cppreference.com/w/cpp/algorithm/next_permutation

Here is a basic implemenation on cppreference
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
template<class BidirIt>
bool next_permutation(BidirIt first, BidirIt last)
{
    if (first == last) return false;
    BidirIt i = last;
    if (first == --i) return false;
 
    while (1) {
        BidirIt i1, i2;
 
        i1 = i;
        if (*--i < *i1) {
            i2 = last;
            while (!(*i < *--i2))
                ;
            std::iter_swap(i, i2);
            std::reverse(i1, last);
            return true;
        }
        if (i == first) {
            std::reverse(first, last);
            return false;
        }
    }
}
You can possibly fix your code by learning from this.
Topic archived. No new replies allowed.