字母排列组合(递归来操作)
2019-06-29 本文已影响0人
朱宏飞
![](https://img.haomeiwen.com/i12909142/da1fae6ba1b2850e.png)
//a开头,跟bc所有排列
swap(p[0],p[0]);
Pormuation(p,1,2)
swap(p[0],p[0]);
//b开头,跟ac所有排列
swap(p[0],p[1]);
Pormuation(p,1,2);
swap(p[0],p[1]);
//a开头,跟bc所有排列
swap(p[0],p[2]);
Pormuation(p,1,2);
swap(p[0],p[2]);
*/
//分析上面的,改写成for循环
---------------------------------------------------------------------------
#include<iostream>
using namespace std;
int cnt=0;
void Pormuation(char *p,const int k,const int m);
int main(){
char a[]="abc";
Pormuation(a,0,2);
return 0;
}
void Pormuation(char *p,const int k,const int m){
cout<<"计数为"<<++cnt<<endl;
if(k==m){
for(int i=0;i<=m;i++)
{
cout<<p[i];
}
cout<<endl;
}
else{
for(int i=k;i<=m;i++) {
swap(p[k],p[i]);
Pormuation(p,k+1,m);
swap(p[k],p[i]);
}
}
}
我还是晕的。这可咋办。。。。。