#include <iostream>
#include <assert.h>
using namespace std;
template <typename T>
void CalcAllPermutation_R(T perm[], int first, int num)
{
if (num <= 1) {
cout<<perm<<endl;
return;
}
for (int i = first; i < first + num; ++i) {
if (IsSwap<T>(perm,first,i))//去重的全排列就是从第一个数字起每个数分别与它后面非重复出现的数字交换
{
swap(perm[i], perm[first]); //全排列就是从第一个数字起每个数分别与它后面的数字交换
CalcAllPermutation_R(perm, first + 1, num - 1);
swap(perm[i], perm[first]);
}
}
}
template <typename T>
bool IsSwap(T *pszStr, int nBegin, int nEnd)
{
for (int i = nBegin; i < nEnd; i++)
if (pszStr[i] == pszStr[nEnd])
return false;
return true;
}
int main()
{
char x[] = "1233";
CalcAllPermutation_R<char>(x,0,4);
system("pause");
return 0;
}
去重的全排列
最新推荐文章于 2024-04-06 00:16:51 发布