排列——递归

要点:

 将递归用于排列的模拟 ,用C++实现

 数据结构  string

 算法       回溯

细节:

主要分析递归造树的过程  见下图

代码实现:

#include<iostream>
#include<cstring>
using namespace std;
string X;
void Try(int k)
{   
    cout<<X<<endl;
    if(k==X.length()-1)
    { 
        cout<<"X:"<<"	"<<X<<endl;
        return ;
    }
    for(int i=k;i<X.length(); i++)
    {
        char temp;
        temp=X[k];
        X[k]=X[i];
        X[i]=temp;
        Try(k+1);
        X[i]=X[k];
        X[k]=temp;
    }
}
int main()
{
    cin>>X;
    Try(0);
    return 0;
}

运行结果:

原文地址:https://www.cnblogs.com/Howbin/p/8672306.html