怎样输出几个数的全排列呢?(C++编程实现)

第一种方法,比较简单,借助于STL库中的next_permutation函数。next_permutation的作用就是计算全排列。

示例:输出整数数组array的全排列

#include #include using namespace std; int array[] = {1, 2, 3, 4}; const int iArraySize = sizeof(array)/sizeof(int); int main(){ int iCnt = 0; cout << iArraySize << endl; sort(array, array + iArraySize); do { for(int i = 0; i < iArraySize; i++) { cout << array[i]; } cout << " "; iCnt++; } while (next_permutation(array, array + iArraySize)); cout << endl; cout << "Total number: " << iCnt << endl; return 0; }

 

第二种方法,不用STL库,改用递归的思想来实现。

示例:输出字符串str中各个字符的全排列(数组与此类似,这里给出针对字符串的代码)

#include #include #include using namespace std; int array[] = {1, 2, 3, 4}; const int iArraySize = sizeof(array)/sizeof(int); //删除str的第n个字符 void DeleteCharAt(string& str, int n) { if (n < 0 || n >= str.length()) { return; } string tmpStr(str.substr(n + 1)); str = str.substr(0, n) + tmpStr; } //base 以该字符串作为基础字符串,进行选择性组合。 //buff 所求字符串的临时结果 //result 存放所求结果 void ListAll(string& strBase, string strBuff, vector& result) { if (strBase.length() <= 0) { result.push_back(strBuff); } for(int i = 0; i < strBase.length(); i++) { string tmp(strBase); DeleteCharAt(tmp, i); ListAll(tmp, strBuff + strBase[i], result); } } int main(){ int iCnt = 0; string str = "1324"; vector vResult; ListAll(str, "", vResult); //Output the result for (int i = 0; i < vResult.size(); ++i) { cout << vResult[i] << " "; } cout << "/n"; cout << "Total number: " << vResult.size() << endl; return 0; }

 

---------------

今天又看到此文,动手又实践了一下,

输出几个数的全排列:

(调用示例:vector rst; testAllPermutations("", "abc", rst);

//列出全排列
void testAllPermutations(const string& preStr, const string& nextArr, vector& retVec)
{
    if(nextArr.length() == 1)
    {
        retVec.push_back(nextArr);
        return;
    }

    for (unsigned int i = 0; i < nextArr.length(); ++i)
    {
        string now(1, nextArr[i]);

        string next(nextArr);
        next = next.erase(i, 1);

        vector retVal;
        testAllPermutations(now, next, retVal);

        for(unsigned int j = 0; j < retVal.size(); ++j)
        {
            retVec.push_back(now+retVal[j]);
        }
    }
}

在此基础上,输出几个数的所有的可能组合:

//列出所有可能的组合
void testAllCombination(const string& preStr, const string& nextArr, vector& retVec)
{
    if(preStr.length() == 0)
    {
        //初始时将单字符组合形式记录下来
        for (unsigned int t = 0; t < nextArr.length(); ++t)
        {
            retVec.push_back(string(1, nextArr[t]));
        }
    }
    if(nextArr.length() == 1)
    {
        retVec.push_back(nextArr);
        retVec.push_back("");
        return;
    }

    for (unsigned int i = 0; i < nextArr.length(); ++i)
    {
        string now(1, nextArr[i]);

        string next(nextArr);
        next = next.erase(i, 1);

        vector retVal;
        testAllCombination("", next, retVal); //首参数传空

        for(unsigned int j = 0; j < retVal.size(); ++j)
        {
            retVec.push_back(now+retVal[j]);
        }
    }
}
不过,这样最后计算的结果,数据会有一定重复,过滤一下就OK。



你可能感兴趣的:(C/C++,数据结构与算法)