Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm n个不同数的组合_Algorithm_Math_Combinations_Permutation_Distinct Values - Fatal编程技术网

Algorithm n个不同数的组合

Algorithm n个不同数的组合,algorithm,math,combinations,permutation,distinct-values,Algorithm,Math,Combinations,Permutation,Distinct Values,我有一组n的大小写,格式是a,b,我要做的是,我要形成一个数,一个数,一个数,一个数的不同组合。例如 假设n=4,a,b如下 1 2 3 1 2 4 3 2 现在总共有4个不同的数字,它们是(1,2,3,4) 并且可以形成所有不同数字的两个组合,它们是(1,3,4,2)和(2,1,4,3),如下所示:- 1 2 | 3 1 \ 2 4 | 3 2 及 我的问题是我无法思考如何编码,因为n要形成一个不同数字的列表,只需使用一个“唯一集”,并不断将所有数字插入其中。在C++

我有一组n的大小写,格式是a,b,我要做的是,我要形成一个数,一个数,一个数,一个数的不同组合。例如

假设n=4,a,b如下

1 2
3 1
2 4
3 2
现在总共有4个不同的数字,它们是(1,2,3,4)

并且可以形成所有不同数字的两个组合,它们是(1,3,4,2)和(2,1,4,3),如下所示:-

 1 2
 | 
 3 1
  \
 2 4
   |
 3 2


我的问题是我无法思考如何编码,因为n要形成一个不同数字的列表,只需使用一个“唯一集”,并不断将所有数字插入其中。在C++中,STD::Debug定义只存储唯一的数字。

要找到不同序列组合的数量,您必须保留一个“候选列表”列表,并在其中插入数字(如果它们已经没有这些数字),否则删除该特定候选列表

C++中的完整代码:

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

int main() {

    int n = 4;
    set<int> uniqueNumbers; // ordered set of unique numbers
    vector< set<int> > possibleLists( 1 );
    set<int>::iterator it;

    for ( int i = 0; i < n; i++ ) {

        int num1;
        int num2;
        cin >> num1 >> num2;

        // numbers will be inserted if not already present in set (by definition)
        uniqueNumbers.insert( num1 );
        uniqueNumbers.insert( num2 );

        // make a copy for a possible new branch
        vector< set<int> > possibleListsCopy( possibleLists );

        //int size1 = possibleLists.size();

        for ( int j = 0; j < possibleLists.size(); j++ ) {

            it = possibleLists[j].find( num1 );
            if ( it == possibleLists[j].end() ) {
                possibleLists[j].insert( num1 ); // insert if not found
                //cout << "inserted1 "<<endl;
            }
            else {
                // erase this possible combination
                possibleLists[j].clear();
                possibleLists.erase( possibleLists.begin() + j );
                j--;
            }

        }

        //int size2 = possibleListsCopy.size();

        for ( int j = 0; j < possibleListsCopy.size(); j++ ) {
;

            it = possibleListsCopy[j].find( num2 );
            if ( it == possibleListsCopy[j].end() ) {
                possibleListsCopy[j].insert( num2 ); // insert if not found
            }
            else {
                // erase this possible combination
                possibleListsCopy[j].clear();
                possibleListsCopy.erase( possibleListsCopy.begin() + j );
                j--;
            }

        }

        // concatenate both set of lists.
        possibleLists.insert( possibleLists.end(),
                                possibleListsCopy.begin(),
                                possibleListsCopy.end() );


    }


    cout << " The unique list: ";
    //output the unique list.
    for ( it = uniqueNumbers.begin(); it != uniqueNumbers.end(); it++ )
        cout << *it << " ";

    /*cout << endl << endl;

    cout << "Possible Lists:" << endl;

    for ( int i = 0; i < possibleLists.size(); i++ ) {

        for ( it = possibleLists[i].begin(); it != possibleLists[i].end(); it++ )
            cout << *it << " ";
        cout << endl;

    }*/

    cout << endl << "Total number of combinations: "
        << possibleLists.size() << endl;

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
int n=4;
set uniqueNumbers;//唯一数的有序集
向量可能列表(1);
set::迭代器;
对于(int i=0;i>num1>>num2;
//如果集合中还没有数字(根据定义),则将插入数字
插入唯一编号(num1);
插入的唯一编号(num2);
//为可能的新分支制作一份副本
向量possiblelistscope(possibleLists);
//int size1=possibleLists.size();
对于(int j=0;j//Couth

递归可能是解决像这样的组合问题的最简单的方法。这个想法是考虑当前项目的所有可能性,然后通过对剩余项目的重复来传递剩余的工作。在这种情况下,您需要传递一些关于不使用哪些项目的额外信息。

它的工作原理如下:

def DistinctChooseFromEach(listOfChoicePairs, alreadyUsed = {}):
    if listOfChoicePairs is empty: return []
    for value in listOfChoicePairs[0]:
        if value in alreadyUsed: continue;
        newUsed = union(alreadyUsed, value)
        remainingChoices = listOfChoicePairs[1:];
        tails = DistinctChooseFromEach(remainingChoices, newUsed)
        for tail in tails:
           yield concat(value, tail)

谢谢你的回复,但我没有得到什么是候选人名单,我也无法在谷歌上找到(抱歉我在学习C++和矢量的时候迟到了)谢谢你的回复,我做了你说的,检查<代码>http://ideone.com/8DZkvY
但我没有得到正确的输出,因为只有两个不同的1234,但得到了50,这是因为向量的大小是50..如何获得正确的输出(我看不到你的代码。但是,我已经编辑了我的答案,并包含了完整的代码。你可以检查一下。如果
n>max(a)| | n>max(b)
,你就找不到像你所说明的那样从上到下的路径。在你到达之前,你已经没有了不同的数字了。只是想指出它,正如帖子所说:
as n
def DistinctChooseFromEach(listOfChoicePairs, alreadyUsed = {}):
    if listOfChoicePairs is empty: return []
    for value in listOfChoicePairs[0]:
        if value in alreadyUsed: continue;
        newUsed = union(alreadyUsed, value)
        remainingChoices = listOfChoicePairs[1:];
        tails = DistinctChooseFromEach(remainingChoices, newUsed)
        for tail in tails:
           yield concat(value, tail)