版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014183456/article/details/82953827
#include <iostream>
#include <string>
using namespace std;
void Permutation(string p,const int k,const int m)
{
static int c=0;
if(k==m) //循环到k==m时结束一次循环
{
cout<<p;
cout<<++c;
cout<<endl;
}
for(int i=k;i<=m;i++) //用一个for循环代替下面的
{
swap(p[k],p[i]); //交换两个数
Permutation(p,k+1,m);
swap(p[k],p[i]);
}
/** swap(p[0],p[0]); //假如adc 此时是a和自己本身交换
Permutation(p,1,m); //a不变 此时递归为abc acb
swap(p[0],p[0]);
swap(p[0],p[1]); //假如adc 此时是a和b交换
Permutation(p,1,m); //b不变 此时递归为bca bac
swap(p[0],p[1]);
swap(p[0],p[2]); //假如adc 此时是a和c交换
Permutation(p,1,m); //c不变 此时递归为cab cba
swap(p[0],p[2]);
**/
}
int main()
{
//string p="abc";
string p;
cout<<"请输入一个字符串:"<<endl;
getline(cin,p); //使用string 流输入一个字符串
int c=p.length(); //计算字符串的长度
Permutation(p,0,c-1); //递归调用排列组合
// cout << c << endl;
return 0;
}