#include <iostream>
#include <string>
using namespace std;
bool IsSwap(char* perm, int from, int to)
{
for(int i = from; i < to ; ++i)
{
if(perm[i] == perm[to])
return false;
}
return true;
}
void CalcAllPermutation(char* perm, int from, int to)
{
if (to <= 1)
{
return;
}
if (from == to)
{
for (int i = 0; i <= to; i++)
cout << perm[i];
cout << endl;
}
else
{
for (int j = from; j <= to; j++)
{
if(IsSwap(perm, from, j))
{
swap(perm[j], perm[from]);
CalcAllPermutation(perm, from + 1, to);
swap(perm[j], perm[from]);
}
}
}
}
int main()
{
char a[4] = "baa";
CalcAllPermutation(a, 0, 2);
return 0;
}
字符串全排列代码实现
最新推荐文章于 2019-03-02 21:44:39 发布