递归求全排列

来源:互联网 发布:羊皮夹克价格.知乎 编辑:程序博客网 时间:2024/05/16 04:16

#include<iostream.h>
#include<string.h>
template<class T>
void perm(T list[],int k,int m)
{
 int i;

if (k==m)
 {
  for(i=0;i<=m;i++)
   cout<<list[i];
  cout<<endl;
 }
 else
 for(i=k;i<=m;i++)
 {
  swap(list[k],list[i]);
     perm(list,k+1,m);
  swap(list[k],list[i]);
 }

}
template<class T>
swap(T &a,T &b)
{
T temp;
temp=a;a=b;b=temp;
}

void main()
{
 int num[]={1,2,3,4,5};
 perm(num,0,4);

}

求数组中k到m的元素的全排列。vc6调试通过。