使用c ++的排列和/组合

时间:2015-12-12 03:24:17

标签: c++ algorithm math combinations permutation

我的代码需要不同版本的排列。我能够实现我想要的但是它不够通用。我的算法随着我的要求不断变大。但那不应该。

对于任何一个人来说,这不是一个家庭作业,我需要一个我的关键项目,想知道是否可以使用boost或任何其他预定义算法。

以下是使用c ++的next_permutation的标准版本。

// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation

int main () 
{
  int myints[] = {1,2,3};
  do 
  {
    std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
  } while ( std::next_permutation(myints,myints+3) );


  return 0;
}

这给出了以下输出:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

但我的要求是: - 我们说我有1到9个数字: 1,2,3,4,5,6,7,8,9

我需要一个可变长度的排列,只需要ASCENDING顺序,而且不需要重复排列。

我们说我需要3位长的排列,然后我需要输出如下。

123
124
125
.
.
.
128
129
134   // After 129 next one should be exactly 134
135      // ascending order mandatory
136
.
.
.
148
149
156   // exactly 156 after 149, ascending order mandatory
.
.
.
489   // exactly 567 after 489, because after 3rd digit 9, 2nd digit
567   // will be increased to 49? , so there is no possibility for
.     // 3rd digit, so first digit gets incremented to 5 then 6 then
.     // 7, in ascending order.
.     
.
.
789   // and this should be the last set I need.

我的列表可能包含最多数百个数字,可变长度可以是1到最大列表的大小。

我自己的算法适用于特定的可变长度和特定的大小,当它们都发生变化时,我需要编写巨大的代码。所以,寻找一个通用的。

我甚至不确定这是否被称为Permutations,或者这种数学/逻辑有不同的名称。

提前致谢。 麝香&#39; S

2 个答案:

答案 0 :(得分:1)

使用正确的术语,您试图生成所有combinations个m元素,而不会从一组n个元素中重复。

以下代码显示从集合[0;n-1]中获取的m个元素的所有组合:

#include <algorithm>
#include <iostream>
#include <vector>

bool first_combination (std::vector<int> &v, int m, int n)
{
    if ((m < 0) || (n < 0) || (m > n)) {
        return false;
    }

    v.clear ();
    for (int i = 0; i < m; i++) {
        v.push_back (i);
    }

    return true;
}

bool next_combination (std::vector<int> &v, int m, int n)
{
    for (int i = m - 1; i >= 0; i--) {
        if ((v[i] < n) && (n - 1 - v[i] >= m - i)) {
            v[i]++;
            for (int j = i + 1; j < m; j++) {
                v[j] = v[j - 1] + 1;
            }
            return true;
        }
    }

    return false;
}

int main ()
{
    const int M = 3;
    const int N = 5;

    std::vector<int> v;

    if (first_combination (v, M, N)) {
        do {
            for (size_t i = 0; i < v.size(); i++) {
                if (i > 0) {
                    std::cout << ", ";
                }
                std::cout << v[i];
            }
            std::cout << '\n';
        } while (next_combination (v, M, N));
    }
}

您可以使用此代码和链接文章作为灵感。

答案 1 :(得分:1)

此任务可以使用简单的迭代算法完成。只需递增可递增的第一个元素,然后重新缩放元素,直到没有要递增的元素为止。

int a[] = {0,1,2,3,4,5,6,7,8,9}; // elements: must be ascending in this case
int n = sizeof(a)/sizeof(int);
int digits = 7; // number of elements you want to choose
vector<int> indexes; // creating the first combination
for ( int i=digits-1;i>=0;--i ){
    indexes.push_back(i);
}

while (1){
    /// printing the current combination
    for ( int i=indexes.size()-1;i>=0;--i ){
        cout << a[indexes[i]] ;
    } cout << endl;
    ///
    int i = 0;
    while ( i < indexes.size() && indexes[i] == n-1-i ) // finding the first element
        ++i;                                            // that can be incremented
    if ( i==indexes.size() ) // if no element can be incremented, we are done
        break;
    indexes[i]++; // increment the first element
    for ( int j=0;j<i;++j ){ // rescale elements before it to first combination
        indexes[j] = indexes[i]+(i-j);
    }
}

输出:

0123456
0123457
0123458
0123459
0123467
0123468
0123469
0123478
0123479
0123489
0123567
0123568
0123569
0123578
0123579
0123589
0123678
0123679
0123689
0123789
0124567
0124568
0124569
0124578
0124579
0124589
0124678
0124679
0124689
0124789
0125678
0125679
0125689
0125789
0126789
0134567
0134568
0134569
0134578
0134579
0134589
0134678
0134679
0134689
0134789
0135678
0135679
0135689
0135789
0136789
0145678
0145679
0145689
0145789
0146789
0156789
0234567
0234568
0234569
0234578
0234579
0234589
0234678
0234679
0234689
0234789
0235678
0235679
0235689
0235789
0236789
0245678
0245679
0245689
0245789
0246789
0256789
0345678
0345679
0345689
0345789
0346789
0356789
0456789
1234567
1234568
1234569
1234578
1234579
1234589
1234678
1234679
1234689
1234789
1235678
1235679
1235689
1235789
1236789
1245678
1245679
1245689
1245789
1246789
1256789
1345678
1345679
1345689
1345789
1346789
1356789
1456789
2345678
2345679
2345689
2345789
2346789
2356789
2456789
3456789