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

麝香

我的代码需要其他版本的排列。我可以实现我想要的功能,但是它不够通用。我的算法随着我的要求不断扩大。但这不应该。

对于任何人来说,这都不是一项家庭工作,对于我的关键项目,我需要它,想知道是否可以从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

我需要可变长度的排列,并且仅按升序排列,并且没有重复。

假设我需要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)或这种数学/逻辑有不同的名称。

提前致谢。麝香

赛义德

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

并行使用大量排列:组合iterpc和foreach

来自分类Dev

查找给定编号的所有可能排列和组合。使用Python在列表中的元素

来自分类Dev

比较排列和组合

来自分类Dev

比较排列和组合

来自分类Dev

字符的组合和排列

来自分类Dev

数组C#中数字的组合/排列

来自分类Dev

使用Prolog查找不成对排列的组合

来自分类Dev

使用C#组合foreach和for循环

来自分类Dev

使用Python在2个组合数组中排列序列

来自分类Dev

如何使用条件排列获得所有组合

来自分类Dev

使用LINQ排列集合元素的所有可能组合

来自分类Dev

如何使用C以字母顺序排列字符串和排列单词

来自分类Dev

C ++:关联,聚合和组合

来自分类Dev

使用 && 和 || 组合不工作

来自分类Dev

使用+和-C#的数字的所有组合

来自分类Dev

使用EF和LINQ填充C#/ XAML组合框

来自分类Dev

生成字符串排列和组合的智能方法

来自分类Dev

程序为给定元素打印排列和组合

来自分类Dev

Scala集合中的组合和排列是否稳定?

来自分类Dev

最简单的手动排列和组合

来自分类Dev

如何组合和排列两个单词表

来自分类Dev

程序为给定元素打印排列和组合

来自分类Dev

Int 对象不可调用(排列和组合)

来自分类Dev

如何在CLIPS中进行事实的组合和排列

来自分类Dev

MSSQL 排列/组合 - 使用数据子集查找所有可能的匹配项

来自分类Dev

在容器排列中使用 C++ 适配器和库函数对象

来自分类Dev

C#中的置换和组合

来自分类Dev

在C ++中查找置换和组合

来自分类Dev

分组和组合值 DataTable,c#