c# - 来自一组 n 算法的 k 个对象的排列

标签 c# algorithm permutation combinations combinatorics

我正在寻找一种有效的方法来实现这一目标:

  1. 你有一组数字,假设我们的组等于 4 (N = 4);
  2. 您必须生成 3 个元素的所有排列 (K = 3);

N = 4 和 K = 3 的输出:

1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2

任何人都拥有出色、快速的算法或 Web 引用?

谢谢!

最佳答案

像这样的伪代码:

permute(set, output, len)  //output will hold all the permutations

for each number in the set do
    choose number and store it at output[0]
    if(!empty(set))
        call permute(set{without the number}, output + (len - 1)!, len-1) //adjust the position

通过permute(set, output, k)调用

关于c# - 来自一组 n 算法的 k 个对象的排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13180970/

相关文章:

javascript - 获取由两个数字之间的 ASCII 字符生成的字符串的所有可能组合

c# - ASP.NET MVC 为什么不从 View 中调用此发布操作?

c# - 获取解决方案中使用的所有 NuGet 包的列表

c++ - 如何优化匹配范围(作业)

c++ - Leetcode 1366:堆缓冲区溢出

algorithm - 一种使用堆栈枚举数字 {1,2,...,n} 的所有排列的算法

c# - 逐个元素比较二维数组元素的最快方法

c# - Sitecore Item Bucket 查询全部返回 null

algorithm - 责任链能否有多个节点修改请求?

Python Secret Santa 程序——如何获得更高的成功率