我正在寻找一种有效的方法来实现这一目标:
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
任何人都有他们的袖子或网络参考的伟大,漂亮的'快速算法?
谢谢!
答案 0 :(得分:1)
像这样的伪代码:
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)