对于任意的输入字符串,输出其所有的排列情况,以及排列的总数
#include
<
iostream
>
using namespace std;
int cnt = 0 ;
inline void swap( char & a, char & b)
... {
char t=a;
a=b;
b=t;
}
void permutation( char a[], int m, int n)
... {
int i;
if (m<n-1)
...{
permutation(a, m+1, n);
for (i=m+1;i<n;i++)
...{
swap(a[m],a[i]);
permutation(a, m+1, n);
swap(a[m],a[i]);
}
}
else
...{
cout<<a<<endl;
++cnt;
}
}
int main()
... {
char a[]="1234";
permutation(a, 0,strlen(a));
cout<<cnt<<endl;;
return 0;
}
using namespace std;
int cnt = 0 ;
inline void swap( char & a, char & b)
... {
char t=a;
a=b;
b=t;
}
void permutation( char a[], int m, int n)
... {
int i;
if (m<n-1)
...{
permutation(a, m+1, n);
for (i=m+1;i<n;i++)
...{
swap(a[m],a[i]);
permutation(a, m+1, n);
swap(a[m],a[i]);
}
}
else
...{
cout<<a<<endl;
++cnt;
}
}
int main()
... {
char a[]="1234";
permutation(a, 0,strlen(a));
cout<<cnt<<endl;;
return 0;
}