#include<cstdio>
#include<cstdlib>
bool contains(char *pStr, char *pBegin, char *pEnd)
{
for(char *pCh = pBegin; pCh != pEnd; pCh++)
if(*pCh == *pEnd)
return true;
return false;
}
void Permutation(char *pStr, char *pBegin, int &count)
{
if(*pBegin == '\0')
{
printf("%s\n", pStr);
count++;
}
else
{
for(char *pCh = pBegin; *pCh != '\0'; pCh++)
{
//此处处理有重复的情况
if(contains(pStr, pBegin, pCh))
continue;
int temp = *pBegin;
*pBegin = *pCh;
*pCh = temp;
Permutation(pStr, pBegin+1, count);
temp = *pBegin;
*pBegin = *pCh;
*pCh = temp;
}
}
}
void Permutation(char *pStr)
{
if(pStr == NULL)
return;
int count = 0;
Permutation(pStr, pStr, count);
printf("total:%d\n", count);
}
int main()
{
char str[] = "12234";
char *p = str;
Permutation(p);
return 0;
}
转载于:https://my.oschina.net/gaosheng/blog/310617