作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
比方说,一个有8个参与者的类(class),我必须以所有可能的方式输出前3个名额。例如:
123124125126127128213等等..
我知道有 next_permutation
算法,但它会返回所有数字(从 1 到 8)的所有可能排列,但我需要所有参与者的前 3 个位置例如:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 8 7
最佳答案
您所追求的不是排列,这就是为什么单独使用 next_permutation
无法解决您的问题。
首先,您需要确定123
是否与321
相同。如果它们相同,则您有普通的 combinations .如果它们不同,则您有 k-permutations (不同于普通排列)。
std::next_permutation
为您提供下一个排列,而不是下一个 k-排列。没有 std::next_combination
。
幸运的是,如果您编写自己的 next_combination
(或在 Internet 上找到一个),您可以将它与 std::next_permutation
一起使用以轻松表达 next_k_permutation
算法。
有了正确的术语,应该很容易找到解决方案。
关于时间:2019-03-08 标签:c++next_permutationalgorithm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9501742/
比方说,一个有8个参与者的类(class),我必须以所有可能的方式输出前3个名额。例如: 123124125126127128213等等.. 我知道有 next_permutation 算法,但它会返
我是一名优秀的程序员,十分优秀!