我正在寻找一种有效的方法来实现这一目标:
- 你有一组数字,假设我们的组等于 4 (N = 4);
- 您必须生成 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/