Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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
C++ 在二维数组的每行中查找具有1个元素的所有可能组合_C++_Arrays_Combinations - Fatal编程技术网

C++ 在二维数组的每行中查找具有1个元素的所有可能组合

C++ 在二维数组的每行中查找具有1个元素的所有可能组合,c++,arrays,combinations,C++,Arrays,Combinations,最近,我一直试图解决一个问题,要求我只从每行中选择一个元素来查找所有不同的组合。例如,我输入n行,每行2个字符串。但是,我只想找到所有不同的组合,其中我从每行中选择一个字符串 例如: 输入: 3 alex bob straw mat eat drink 示例组合: 亚历克斯稻草饮料 这将导致2^n个组合,在这种情况下,2^3=8个组合。但是,如果我要用n表示循环来找到组合 e、 g #包括 #包括 #包括 使用名称空间std; int n; int main(int argc,字符**argv

最近,我一直试图解决一个问题,要求我只从每行中选择一个元素来查找所有不同的组合。例如,我输入n行,每行2个字符串。但是,我只想找到所有不同的组合,其中我从每行中选择一个字符串

例如:

输入:

3
alex bob
straw mat
eat drink
示例组合: 亚历克斯稻草饮料

这将导致2^n个组合,在这种情况下,2^3=8个组合。但是,如果我要用n表示循环来找到组合 e、 g

#包括
#包括
#包括
使用名称空间std;
int n;
int main(int argc,字符**argv){
cin>>n;//一行行字
字符串字[n][2];//每行有2个字的字
对于(int i=0;i>单词[i][j];//单词输入
}
}
//寻找所有可能的组合
对于(inti=0;i可以进行递归

假设是C++11,可能是这样的(但没有尝试编译):

//查找所有可能的组合
std::向量组合;
const auto processLine=[&](const std::vector¤tCombination,int line){
std::矢量组合0=当前组合;
std::矢量组合1=当前组合;
组合0.向后推(字[line][0]);
组合1.推回(字[行][1]);
如果(行+1
假设是C++11,可能是这样的(但没有尝试编译):

//查找所有可能的组合
std::向量组合;
const auto processLine=[&](const std::vector¤tCombination,int line){
std::矢量组合0=当前组合;
std::矢量组合1=当前组合;
组合0.向后推(字[line][0]);
组合1.推回(字[行][1]);
如果(行+1std::cout对于每行始终有2个元素的设置,一个非常简单的解决方案是使用数据类型integer并将每个位解释为相应行中第一列或第二列的决定;然后简单地从0到2^n-1进行计数,以获得所有组合。
应用于您的示例,如下所示:

int bits meaning
0   000  alex,straw,eat
1   001  alex,straw,drink
2   010  alex,mat,eat
3   011  alex,mat,dring
4   100  bob,straw,eat
5   101  bob,straw,drink
6   110  bob,mat,eat
7   111  bob,mat,drink
对于任何给定整数值0..7,请使用位移位运算符或&-bitmask将每个位映射到列索引:

void getCombinationRepresentedByIntValue(向量和组合,int值){
int-mask=1;
对于(int i=n-1;i>=0;i--){
if(值和掩码)
组合。推回(字[i][1]);
其他的
组合。推回(字[i][0]);

mask=mask对于每行始终有2个元素的设置,一个非常简单的解决方案是使用数据类型integer并将每个位解释为相应行中第一列或第二列的决定;然后简单地从0到2^n-1进行计数,以获得所有组合。 应用于您的示例,如下所示:

int bits meaning
0   000  alex,straw,eat
1   001  alex,straw,drink
2   010  alex,mat,eat
3   011  alex,mat,dring
4   100  bob,straw,eat
5   101  bob,straw,drink
6   110  bob,mat,eat
7   111  bob,mat,drink
对于任何给定整数值0..7,请使用位移位运算符或&-bitmask将每个位映射到列索引:

void getCombinationRepresentedByIntValue(向量和组合,int值){
int-mask=1;
对于(int i=n-1;i>=0;i--){
if(值和掩码)
组合。推回(字[i][1]);
其他的
组合。推回(字[i][0]);

mask=mask似乎可以回答您的问题:

    int ct[n]; // count of the number of pass 
    int current = 0; // index of the current word (n)

    /* while not all combinaison have been exploited */
    while (current >= 0)
    {
        cout << words[current][ct[current]];  /* <<<<< can be used another way*/
        /* down to the next word */
        current ++; // to get the next word
        if (current >=n) { // at the end of the list
            cout << " ";
            current--; // restore last
            ct[current]++; // increment number of time we passed
            /* find the previous not completely exploited */
            while (current >= 0 && ct[current]> 1) /* <<< change 1 to any number of words per line */
            {
                ct[current] = 0;
                current--;
                if (current >= 0) ct[current]++;
            }
            if (current > 0 ) current = 0;
        }
    }
int-ct[n];//通过次数的计数
int current=0;//当前单词的索引(n)
/*但并不是所有的组合都被利用了*/
而(当前>=0)
{

这似乎回答了你的问题:

    int ct[n]; // count of the number of pass 
    int current = 0; // index of the current word (n)

    /* while not all combinaison have been exploited */
    while (current >= 0)
    {
        cout << words[current][ct[current]];  /* <<<<< can be used another way*/
        /* down to the next word */
        current ++; // to get the next word
        if (current >=n) { // at the end of the list
            cout << " ";
            current--; // restore last
            ct[current]++; // increment number of time we passed
            /* find the previous not completely exploited */
            while (current >= 0 && ct[current]> 1) /* <<< change 1 to any number of words per line */
            {
                ct[current] = 0;
                current--;
                if (current >= 0) ct[current]++;
            }
            if (current > 0 ) current = 0;
        }
    }
int-ct[n];//通过次数的计数
int current=0;//当前单词的索引(n)
/*但并不是所有的组合都被利用了*/
而(当前>=0)
{

是不是每行只能有两个元素,或者该算法每行可以处理2个以上的元素?是不是每行只能有两个元素,或者该算法每行可以处理2个以上的元素?谢谢,位移位似乎效果最好,现在就实现它。谢谢,位移位似乎效果最好,implem现在就开始。