转自剑指offer28题:
#include<iostream>
using namespace std;
void Permutation(char* pStr, char* pBegin);
void Permutation(char* pStr)
{
if(pStr == NULL)
return;
Permutation(pStr, pStr);
}
void Permutation(char* pStr, char* pBegin)
{
if(*pBegin == '\0')
{
printf("%s\n", pStr);
}
else
{
for(char* pCh = pBegin; *pCh != '\0'; ++ pCh)
{
char temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
Permutation(pStr, pBegin + 1);
temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
}
}
}
int main()
{
char s1[]="";
char s2[]="a";
char s3[]="abc";
Permutation(s1);
cout<<"\n";
Permutation(s2);
cout<<"\n";
Permutation(s3);
return 0;
}