#include<iostream>
#include<algorithm>
using namespace std;
void swap(char& a,char& b) //两个元素进行交换
{
char c = a;
a = b;
b = c;
}
bool isSwap(char b[], int start, int i) //i为该元素下标
{
for(int k = start; k<i;k++) //判断在该元素之前有没有相同元素
if(b[k] == b[i])
return false; //如果有,则返回false
return true;
}
void Perm(char b[],int low,int high,int& n) //核心的算法啦
{
if(low == high)
{
for(int i = 0;i < low;i++)
{
printf("%c",b[i]);
}
n++;
printf("\n");
}
else
{
for(int i = low;i < high;i++)
{
sort(b+low,b+high);
if(isSwap(b,low,i))
{
swap(b[low],b[i]);
Perm(b,low+1,high,n);
swap(b[low],b[i]);
}
}
}
}
int main()
{
int n = 0; //计数用的
string str;
cin>>str; //用字符串接收待排元素
char b[9] ;
int i = 0;
while(str[i]!='\0')
{
b[i] = str[i]; //把待排元素装进字符数组中
i++;
}
Perm(b,0,str.length(),n); //调用Perm进行排序
cout<<n<<endl; //输出总排列个数
}
有重复全排列
猜你喜欢
转载自blog.csdn.net/SuperBvs/article/details/84887796
今日推荐
周排行