[c++]代码库
#include <iostream>
#include <string>
using namespace std;
bool ok (char a[], int k, int i)
{
for ( int t=k ; t<i; t++)
if (a[t] == a[i] ) return false;
return true ;
}
void perm (char a[], int k, int n)
{
int i; char c;
if (k==n)
{
for (i=0;i<n;i++) cout<<a[i];
cout<<endl; return;
}
for (i=k ; i<n ; i++) if (ok (a, k,i ) )
{
c=a[i];a[i]=a[k];a[k]=c;
perm (a, k+1, n);
c=a[i];a[i]=a[k];a[k]=c;
}
}
void perm (char a[])
{
perm (a,0, strlen(a));
}
int main()
{
char p[]="1122";
int j=strlen(p);
cout<<"数据元素:";
for (int i=0;i<j;i++)
{
if(i!=j-1)
cout<<p[i]<<",";
else
cout<<p[i];
}
cout<<endl<<"全排列为:"<<endl;
perm(p);
return 0;
}