C ++ permutation algorithm

I am trying to translate an algorithm that generates all permutations of k from n in C ++:

public void calculerEquipeTOT(ArrayList<Nageur> L, ArrayList<Nageur> F, int k) {

    if (k == 0) {
        if (calculerPointsTOT(L) > this.pointsMeilleureEquipe){
            this.meilleureEquipe = L;
            this.pointsMeilleureEquipe = calculerPointsTOT(meilleureEquipe);
        }
    } else {
        for (Nageur x : F) {
            ArrayList<Nageur> G = new ArrayList<Nageur>(F);
            G.remove(G.indexOf(x));
            ArrayList<Nageur> L2 = new ArrayList<Nageur>(L);
            L2.add(x);
            calculerEquipeTOT(L2, G, k - 1);
        }
    }
}

My problem is that lists can be a list of objects, and I don’t know how to remove x of an L2 list ... I am not a C ++ specialist, I succeeded in Java, but I have to do it in C ++.

+4
source share
2 answers

I broadcast your function and I got the following

#include <iostream>
#include <list>
#include <iterator>

void arrangements( std::list<char> l, std::list<char> f, size_t k ) 
{
    if ( k == 0 ) 
    {
        for ( char c : l ) std::cout << c << ' ';
        std::cout << std::endl;
    } 
    else 
    {
        for ( auto it = f.begin(); it != f.end(); ++it )
        {
            std::list<char> g( f.begin(), it );
            g.insert( g.end(), std::next( it ), f.end() );

            std::list<char> l2( l );
            l2.push_back( *it );

            arrangements( l2, g , k-1 );
        }
    }
}

int main()
{
    std::list<char> f = { 'A', 'B', 'C', 'D' };

    arrangements( std::list<char>(), f, 2 );
}

Program exit

A B 
A C 
A D 
B A 
B C 
B D 
C A 
C B 
C D 
D A 
D B 
D C 

I do not know what you want to receive.

If, to call a function with k equal to 3, the program output will be

A B C 
A B D 
A C B 
A C D 
A D B 
A D C 
B A C 
B A D 
B C A 
B C D 
B D A 
B D C 
C A B 
C A D 
C B A 
C B D 
C D A 
C D B 
D A B 
D A C 
D B A 
D B C 
D C A 
D C B 
+3
source

, , next_permutation() , next_combination() : http://www.codeguru.com/cpp/cpp/algorithms/combinations/article.php/c5117/Combinations-in-C.htm

:

int main(int argc, const char * argv[]) {

    cout << "Hello, World!\n";
    int nb = 0;

    int tab1[] = {0,1,2,3};
    vector<int> n (tab1, tab1+sizeof tab1 / sizeof tab1[0]);
    int tab2[] = {0,1};
    vector<int> r (tab2, tab2+sizeof tab2 / sizeof tab2[0]);

    sort (n.begin(), n.end());
    do
    {
        sort(r.begin(),r.end());
        //do your processing on the new combination here
        vector<int> r2 = r;
        do
        {
            //do your processing on the new permutation here
            nb++;
            display_vector(r2);
            cout << endl;
        }
        while(next_permutation(r2.begin(),r2.end()));
    }
    while(next_combination(n.begin(),n.end(),r.begin(),r.end() ));

    cout << "Number of possibilities = " << nb << endl;

    return 0;
}

:

Hello, World!
0 1 
1 0 
0 2 
2 0 
0 3 
3 0 
1 2 
2 1 
1 3 
3 1 
2 3 
3 2 
Number of possibilities = 12

1s, 10 12 ... , , , Java.

- , , !:)

+3

All Articles