#include<iostream>
using namespace std;
int n=0;
template <class T>
void Swap(T *a,T *b)
{
T temp=*a;
*a=*b;
*b=temp;
}
template <class T>
void Perm(T A[],int k,int m)
{
if(k>m)
{
for(int i=0;i<=m;i++)
{
cout<<A[i]<<" ";
}
n++;
cout<<endl;
}
else
{
for(int i=k;i<=m;i++)
{
Swap(&A[k],&A[i]);
Perm(A,k+1,m);
Swap(&A[k],&A[i]);
}
}
}
int main()
{
char A[4]={'a','b','c','d'};
Perm(A,0,3);
cout<<"Total number is: "<<n<<endl;
return 0;
}
有位大神写的更好,附上链接:http://blog.youkuaiyun.com/Hackbuteer1/article/details/7462447