题目:输入一个字符串,打印出该字符串中字符的所有排列。
例如输入字符串abc,则输出由字符a、b、c 所能排列出来的所有字符串
abc、acb、bac、bca、cab 和cba。
<pre name="code" class="cpp">#include <iostream>
using namespace std;
void permutation(char*pStr, char* pBegin);
void Permutation(char* pStr)
{
permutation(pStr, pStr);
}
void permutation(char* pStr, char* pBegin)
{
if (!pStr || !pBegin)
return;
if (*pBegin == '\0')
printf("%s\n", pStr);
else
{
for (char* pCh = pBegin; *pCh != '\0'; pCh++)
{
swap(*pBegin, *pCh);
permutation(pStr, pBegin + 1);
swap(*pBegin, *pCh);
}
}
}
int main()
{
char str[] = "abcd";
Permutation(str);
return 0;
}