您的位置:首页 > 编程语言

字符串全排列代码实现

2014-08-03 17:06 183 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++