Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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++ 如何打印4个不同字母组合成2个字母对的所有方式,以填充四个点,每个字母正好出现两次?_C++ - Fatal编程技术网

C++ 如何打印4个不同字母组合成2个字母对的所有方式,以填充四个点,每个字母正好出现两次?

C++ 如何打印4个不同字母组合成2个字母对的所有方式,以填充四个点,每个字母正好出现两次?,c++,C++,到目前为止,我得到的是: #include <iostream> #include <algorithm> using namespace std; int main(){ string genePool[16] = {"aa", "ab", "ac", "ad", "ba", "bb", "bc", "bd", "ca", "cb", "cc", "cd", "da", "db", "dc", "dd"};

到目前为止,我得到的是:

#include <iostream>
#include <algorithm>

using namespace std;

int main(){

    string genePool[16] = {"aa", "ab", "ac", "ad", "ba", "bb", "bc", "bd", "ca", "cb", 
                       "cc", "cd", "da", "db", "dc", "dd"};

    string coco, code, deco, dede;

    int total = 0;

    for (int i = 0; i < 16; i++){
        coco = genePool[i];
        for (int j = 0; j < 15; j++){
            code = genePool[j];
                 for (int k = 0; k < 14; k++){
                 code = genePool[k];
                      for (int l = 0; l < 13; l++){
                      code = genePool[l];

                      size_t a = count(coco.begin(), coco.end(), 'a') +
                                 count(code.begin(), code.end(), 'a') +
                                 count(deco.begin(), deco.end(), 'a') +
                                 count(dede.begin(), dede.end(), 'a');
                      size_t b = count(coco.begin(), coco.end(), 'b') +
                                 count(code.begin(), code.end(), 'b') +
                                 count(deco.begin(), deco.end(), 'b') +
                                 count(dede.begin(), dede.end(), 'b');
                      size_t c = count(coco.begin(), coco.end(), 'c') +
                                 count(code.begin(), code.end(), 'c') +
                                 count(deco.begin(), deco.end(), 'c') +
                                 count(dede.begin(), dede.end(), 'c');
                      size_t d = count(coco.begin(), coco.end(), 'd') +
                                 count(code.begin(), code.end(), 'd') +
                                 count(deco.begin(), deco.end(), 'd') +
                                 count(dede.begin(), dede.end(), 'd');

                        if (a = 2 || b = 2 || and c = 2 || d = 2){
                           total++;
                           cout << total << ") "coco << "," << code << "," << deco 
                                << "," << dede << endl;
                        }
                      }    
                }
        }
    }
    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串genePool[16]={“aa”、“ab”、“ac”、“ad”、“ba”、“bb”、“bc”、“bd”、“ca”、“cb”,
“cc”、“cd”、“da”、“db”、“dc”、“dd”};
字符串coco,code,deco,dede;
int-total=0;
对于(int i=0;i<16;i++){
coco=genePool[i];
对于(int j=0;j<15;j++){
代码=基因库[j];
对于(int k=0;k<14;k++){
代码=基因库[k];
对于(int l=0;l<13;l++){
代码=基因库[l];
size\u t a=计数(coco.begin()、coco.end()、'a')+
计数(code.begin()、code.end()、'a')+
计数(deco.begin()、deco.end()、a)+
计数(dede.begin(),dede.end(),'a');
size\u t b=计数(coco.begin()、coco.end()、'b')+
计数(code.begin()、code.end()、'b')+
计数(deco.begin(),deco.end(),'b')+
计数(dede.begin(),dede.end(),'b');
size\u t c=count(coco.begin(),coco.end(),'c')+
计数(code.begin(),code.end(),'c')+
计数(deco.begin(),deco.end(),'c')+
计数(dede.begin(),dede.end(),'c');
size\u t d=计数(coco.begin(),coco.end(),'d')+
计数(code.begin(),code.end(),'d')+
计数(deco.begin(),deco.end(),'d')+
计数(dede.begin(),dede.end(),'d');
如果(a=2 | | b=2 | |和c=2 | | d=2){
总计++;

难道你想创建长度为4的序列,从4个字母中选择2个,每个字母包含两次

我(相当粗鲁)的做法如下: 它将一个字符一个字符地添加到序列中,并根据您在每个步骤中的规则缩小可能的解决方案

这将创建如下内容:

aabb, abba, baba, abab an so on
您可以取消注释某些行以只允许原子对,这是在前两个字母之后完成的,以相应地添加第二对。(请参见代码中的注释)

下面是一些代码:

#include <vector>
using namespace std,

// .....

string letters = "abcd", tmpStr;
string wrkString; // string to hold sequence during creation
<vector>string sequences; // vector to hold all valid sequences

for(int first=0;i<letters.length();first++){ // every char as first char
    wrkString = letters.at(first);
    for(int  second=0;i<letters.length();second++){ // every char as second char
        wrkString += letters.at(second);
        // got first 2 now differentiate
        if(first == second) // having aa
            letters.erase(first,1); // if same twice, erase from possible elements
        else{ // having ab
            tmpStr = letters;  // if different only this two remain possible solutions
            letters = tmpStr.at(first) + tmpStr.at(second);   

            // uncomment here to add this pair again in same order (ab ab)
            // wrkString += wrkString;
            // sequences.push_back(wrkString);  
            // letters = '';        
        }
        for(int third=0;i<letters.length();third++){ // having aa or ab or ba or bb or ac ...
            wrkString += letters.at(third);
            if(wrkString.find(letters.at(third))==1) // having aac
               letters = letters.at(third);  // if included it has to be added once more
            else // having acc 
               letters -= letters.at(third); // if included twice, remove from possible elements

            for(int fourth=0;i<letters.length();fourth++){ // here allways 1 letter left
                wrkString += letters.at(fourth);
                sequences.push_back(wrkString);    // adding copy of sequence to vector
            }
        }
    }
    letters = "abcd"; // reset possible elements for next iteration
}
#包括
使用名称空间std,
// .....
字符串字母=“abcd”,tmpStr;
string wrkString;//在创建过程中保留序列的字符串
字符串序列;//用于保存所有有效序列的向量

对于(int first=0;i那么,您想要一个8个字符的序列(4个点x每个点2个字符),每个点包含4个字符中的2个实例

听起来您实际上想要所有可能的
“aabbccdd”
排列,因为您选择了全部8个字符

因此,这将打印每个排列:

#include <string>
#include <algorithm>
#include <iostream>

void print_permutations(const char *source)
{
    // current permutation
    std::string s(source);
    // this must start off sorted
    std::sort(s.begin(), s.end());
    do {
        std::cout << s << '\n';
    } while (std::next_permutation(s.begin(), s.end()));
}

// original gene pool
const char *pool = "aabbccdd";

int main()
{
    print_permutations(pool);
}
#包括
#包括


如果这不是您想要的,也许您可以显示一些示例输出?

我将采用图形方法。构建一个图形,其中geenpool的每个元素由一个节点表示,每个可能的配对由一条边表示

稍微缩小一点,它可能看起来像这样

\#include "iostream"

\#include "string"

\#include "algorithm"

\#include "map"

using namespace std;

using namespace std;

int main(){

string genePool[16] = {"aa", "ab", "ac", "ad", "ba", "bb", "bc", "bd", "ca", "cb",
                   "cc", "cd", "da", "db", "dc", "dd"};


map< string,map< string,bool > * > Graph;

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

Graph[genePool[i]]= new map< string,bool>;

    for(int j=0;j < 16;++j){

    string combined = genePool[i]+genePool[j];

    unsigned found;

    int count_a =count(combined.begin(),combined.end(),'a');

    int count_b =count(combined.begin(),combined.end(),'b');

    int count_c =count(combined.begin(),combined.end(),'c');

    int count_d =count(combined.begin(),combined.end(),'d');

    if( (count_a == 2 ||count_a ==0)   && (count_b == 2 ||count_b ==0)  && (count_c == 2             ||count_c ==0) && (count_d == 2 ||count_d ==0))

    (*Graph[genePool[i]])[genePool[j]] = true;

    }

}

map<string,map<string,bool>* >::iterator it1 =Graph.begin();

while(it1 != Graph.end() ){

    map<string,bool>::iterator it2 =it1->second->begin();

    while(it2 != it1->second->end()){

        cout <<it1->first<<it2->first<<endl;

        ++it2;

    }

   ++it1;

}


}
\\包括“iostream”
\#包括“字符串”
\#包括“算法”
\#包括“地图”
使用名称空间std;
使用名称空间std;
int main(){
字符串genePool[16]={“aa”、“ab”、“ac”、“ad”、“ba”、“bb”、“bc”、“bd”、“ca”、“cb”,
“cc”、“cd”、“da”、“db”、“dc”、“dd”};
map*>图形;
对于(int i=0;i<16;++i){
图[genePool[i]]=新映射;
对于(int j=0;j<16;++j){
字符串组合=基因池[i]+基因池[j];
未签名的发现;
int count_a=count(combined.begin(),combined.end(),'a');
int count_b=count(combined.begin(),combined.end(),'b');
int count_c=count(combined.begin(),combined.end(),'c');
int count_d=count(combined.begin(),combined.end(),'d');
如果((计数a==2 | |计数a==0)和&(计数b==2 | |计数b==0)和&(计数c==2 | |计数c==0)和&(计数d==2 | |计数d==0))
(*图[genePool[i]])[genePool[j]]=true;
}
}
迭代器it1=Graph.begin();
while(it1!=Graph.end()){
迭代器it2=it1->second->begin();
而(it2!=it1->second->end()){
库特
\#include "iostream"

\#include "string"

\#include "algorithm"

\#include "map"

using namespace std;

using namespace std;

int main(){

string genePool[16] = {"aa", "ab", "ac", "ad", "ba", "bb", "bc", "bd", "ca", "cb",
                   "cc", "cd", "da", "db", "dc", "dd"};


map< string,map< string,bool > * > Graph;

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

Graph[genePool[i]]= new map< string,bool>;

    for(int j=0;j < 16;++j){

    string combined = genePool[i]+genePool[j];

    unsigned found;

    int count_a =count(combined.begin(),combined.end(),'a');

    int count_b =count(combined.begin(),combined.end(),'b');

    int count_c =count(combined.begin(),combined.end(),'c');

    int count_d =count(combined.begin(),combined.end(),'d');

    if( (count_a == 2 ||count_a ==0)   && (count_b == 2 ||count_b ==0)  && (count_c == 2             ||count_c ==0) && (count_d == 2 ||count_d ==0))

    (*Graph[genePool[i]])[genePool[j]] = true;

    }

}

map<string,map<string,bool>* >::iterator it1 =Graph.begin();

while(it1 != Graph.end() ){

    map<string,bool>::iterator it2 =it1->second->begin();

    while(it2 != it1->second->end()){

        cout <<it1->first<<it2->first<<endl;

        ++it2;

    }

   ++it1;

}


}