gpt4 book ai didi

时间:2019-03-08 标签:c++next_permutationalgorithm

转载 作者:太空狗 更新时间:2023-10-29 20:16:27 25 4
gpt4 key购买 nike

比方说,一个有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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com