问题:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab,cba.
思路1:利用STL中的next_permutation(...)来直接实现该功能。要包含头文件#include <algorithm>。
void STLnext_permutation(char *s1, int length)
{
do
{
cout<<s1<<endl;
}while(next_permutation(s1, s1+length));
}
void permutation(char *s,char *pBegin)
{
if(*pBegin == '\0')
{
cout<<s<<endl;
}
else
{
for(char* pCh = pBegin; *pCh != '\0'; ++pCh)
{
char temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
permutation(s, pBegin + 1);
temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
}
}
}