数组的全排列
来源:互联网 发布:iPhone 网页发送到mac 编辑:程序博客网 时间:2024/05/16 09:26
#include <iostream>using namespace std;template<class Type>inline void Swap(Type &a, Type &b){ Type temp=a; a=b; b=temp;}template <class Type>/*算法Perm(list,k,m)递归地产生所有前缀是list[0:k-1],且后缀是list[k:m]的全排列的所有排列。全排列从k位置开始,到m位置结束。*/void Perm(Type list[], int k, int m){ if(k==m) { for(int i=0;i<=m;i++) { cout<<list[i]; } cout<<endl; } else { for(int i=k;i<=m;i++) { Swap(list[k],list[i]); Perm(list,k+1,m); Swap(list[k],list[i]); } }}int main(){ int a[]={1,2,3}; Perm(a,0,2); return 0;}