阵排列 [英] Permutation of array
本文介绍了阵排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如我有数组:
int a[] = new int[]{3,4,6,2,1};
我需要所有这种排列的列表塔如果一个人是这样的, {3,2,1,4,6}
,其他人一定是不一样的。我知道,如果数组的长度的 N 的则有 N!的可能的组合。这怎么algortihm写?
I need list of all permutations such tha if one is like this, {3,2,1,4,6}
, others must not be the same. I know that if the length of the array is n then there are n! possible combinations. How can this algortihm be written?
更新:谢谢,但我需要像一个伪code算法:
Update: thanks, but I need a pseudo code algorithm like:
for(int i=0;i<a.length;i++){
// code here
}
只是算法。是的,API函数都不错,但它并不能帮助我太多。
Just algorithm. Yes, API functions are good, but it does not help me too much.
推荐答案
如果您使用的是C ++,你可以使用std :: next_permutation从算法头:
If you're using C++, you can use std::next_permutation from algorithm header:
int a[] = {3,4,6,2,1};
int size = sizeof(a)/sizeof(a[0]);
std::sort(a, a+size);
do {
// print a's elements
} while(std::next_permutation(a, a+size));
这篇关于阵排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文