全排列的实现

 

编程思路如下(截取自《Erlang程序设计》3.8):

调试的代码:

View Code
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

void insert(string &str,char ch, vector<string>& aa)
{
    int n=str.length();
    for (int i=0; i<=n; i++)
    {
        string t_str = str;
        t_str.insert(i,1,ch);
        aa.push_back(t_str);
    }
}
void fun(string array, vector<string>& a)
{
    int n = array.length();
    if (n==2)
    {
        string str1 = array;
        string str2 = array.substr(1,1)+array.substr(0,1);
        a.push_back(str1);
        a.push_back(str2);
        return;
    }
    string array2 = array.substr(1,n-1);
//    cout << "array2=" << array2<<endl;
    vector<string> aa;
    char ch = array[0];
    fun(array2,a);
//    cout << "a.size()=" << a.size() << endl;
    n = a.size();
    for (int i=0; i<n; i++)
    {
//        cout << "a[" <<i<<"]="<< a[i] << endl;
        insert(a[i],ch,aa);
/*        for (int i=0; i<aa.size();i++)
        {
            cout <<"aa["<<i<<"]="<< aa[i]<< endl;
        }
*/
    }
    a.resize(aa.size());
    copy(aa.begin(),aa.end(),a.begin());
}

int main(int argc, char *argv[])
{
    string str;
    cout << "----全排列-----" << endl;
    cout <<"退出请输入:exit"<<endl; 
    cout << "请输入待排列的序列(如:abc):";
    cin>>str;
    while (str!="exit")
    {
        vector<string> v_str;
        fun(str,v_str);
        int size = v_str.size();
        for (int i=0; i<size; i++)
        {
            cout <<"v_str["<<i<<"]="<< v_str[i]<< endl;
        }
        cout <<"退出请输入:exit"<<endl; 
        cout << "请输入待排列的序列(如:abc):";
        cin>>str;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

正式代码:

#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

void insert(string &str,char ch, vector<string>& aa)
{
    int n=str.length();
    for (int i=0; i<=n; i++)
    {
        string t_str = str;
        t_str.insert(i,1,ch);
        aa.push_back(t_str);
    }
}
void fun(string array, vector<string>& a)
{
    int n = array.length();
    if (n==2)
    {
        string str1 = array;
        string str2 = array.substr(1,1)+array.substr(0,1);
        a.push_back(str1);
        a.push_back(str2);
        return;
    }
    string array2 = array.substr(1,n-1);
    vector<string> aa;
    char ch = array[0];
    fun(array2,a);
    n = a.size();
    for (int i=0; i<n; i++)
    {
        insert(a[i],ch,aa);
    }
    a.resize(aa.size());
    copy(aa.begin(),aa.end(),a.begin());
}

int main(int argc, char *argv[])
{
    string str;
    cout << "----全排列-----" << endl;
    cout <<"退出请输入:exit"<<endl; 
    cout << "请输入待排列的序列(如:abc):";
    cin>>str;
    while (str!="exit")
    {
        vector<string> v_str;
        fun(str,v_str);
        int size = v_str.size();
        for (int i=0; i<size; i++)
        {
            cout <<"v_str["<<i<<"]="<< v_str[i]<< endl;
        }
        cout <<"退出请输入:exit"<<endl; 
        cout << "请输入待排列的序列(如:abc):";
        cin>>str;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}
posted @ 2012-10-30 17:45  涵曦  阅读(591)  评论(0编辑  收藏  举报