打印一对集之间的所有关系

时间:2015-04-18 03:32:43

标签: c++ set

寻找帮我解决问题的方法。 我需要打印一对具​​有域和用户输入的codomain的集合之间的所有关系。

例如,如果输入是“ab”和“abc”,则相应的关系列表将是:

0: {}
1: {(a, a), }
2: {(a, b), }
3: {(a, a), (a, b), }
4: {(a, c), }
5: {(a, a), (a, c), }
6: {(a, b), (a, c), }
7: {(a, a), (a, b), (a, c), }
8: {(b, a), }
9: {(a, a), (b, a), }
10: {(a, b), (b, a), }
11: {(a, a), (a, b), (b, a), }
12: {(a, c), (b, a), }
13: {(a, a), (a, c), (b, a), }
14: {(a, b), (a, c), (b, a), }
15: {(a, a), (a, b), (a, c), (b, a), }
16: {(b, b), }
17: {(a, a), (b, b), }
18: {(a, b), (b, b), }
19: {(a, a), (a, b), (b, b), }
20: {(a, c), (b, b), }
21: {(a, a), (a, c), (b, b), }
22: {(a, b), (a, c), (b, b), }
23: {(a, a), (a, b), (a, c), (b, b), }
24: {(b, a), (b, b), }
25: {(a, a), (b, a), (b, b), }
26: {(a, b), (b, a), (b, b), }
27: {(a, a), (a, b), (b, a), (b, b), }
28: {(a, c), (b, a), (b, b), }
29: {(a, a), (a, c), (b, a), (b, b), }
30: {(a, b), (a, c), (b, a), (b, b), }
31: {(a, a), (a, b), (a, c), (b, a), (b, b), }
32: {(b, c), }
33: {(a, a), (b, c), }
34: {(a, b), (b, c), }
35: {(a, a), (a, b), (b, c), }
36: {(a, c), (b, c), }
37: {(a, a), (a, c), (b, c), }
38: {(a, b), (a, c), (b, c), }
39: {(a, a), (a, b), (a, c), (b, c), }
40: {(b, a), (b, c), }
41: {(a, a), (b, a), (b, c), }
42: {(a, b), (b, a), (b, c), }
43: {(a, a), (a, b), (b, a), (b, c), }
44: {(a, c), (b, a), (b, c), }
45: {(a, a), (a, c), (b, a), (b, c), }
46: {(a, b), (a, c), (b, a), (b, c), }
47: {(a, a), (a, b), (a, c), (b, a), (b, c), }
48: {(b, b), (b, c), }
49: {(a, a), (b, b), (b, c), }
50: {(a, b), (b, b), (b, c), }
51: {(a, a), (a, b), (b, b), (b, c), }
52: {(a, c), (b, b), (b, c), }
53: {(a, a), (a, c), (b, b), (b, c), }
54: {(a, b), (a, c), (b, b), (b, c), }
55: {(a, a), (a, b), (a, c), (b, b), (b, c), }
56: {(b, a), (b, b), (b, c), }
57: {(a, a), (b, a), (b, b), (b, c), }
58: {(a, b), (b, a), (b, b), (b, c), }
59: {(a, a), (a, b), (b, a), (b, b), (b, c), }
60: {(a, c), (b, a), (b, b), (b, c), }
61: {(a, a), (a, c), (b, a), (b, b), (b, c), }
62: {(a, b), (a, c), (b, a), (b, b), (b, c), }
63: {(a, a), (a, b), (a, c), (b, a), (b, b), (b, c), }

元素的数量用公式计算:

N_e =最终列表中的元素数量;

k和j =给定集合中的元素数量;

N_e = 2 ^ {K *Ĵ}

2 个答案:

答案 0 :(得分:0)

不发布代码,但这是一个可能的解决方案:

1)首先创建一个包含所有主要关系的数组.i.e用于您的上述输入,即{(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)}。将其存储在2D数组中。

2)现在你所要做的就是打印出这些集合的所有nCr组合。

(1)可以使用两个for循环来实现,(2)你可以谷歌(或bing)一些算法。

答案 1 :(得分:0)

我明白了。

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>
#include <cmath>
#include <iterator>
#include <bitset>
#include <limits>
#include <algorithm>
#include <gmpxx.h>

using namespace std;
void printVector(vector <string> inComing);
long long calcElem(long long n1);

void getList(vector <string> &answersList, vector <string> setList, long long nPossibleElements);
void combineSets(vector <string> &setList, string set1, string set2);


int main(){
    //initializing variables
    int flag = 1; //flag for user input = 0; for build in string = 1
    long long nPossibleElements = 0;
    long long nSet2 = 0;
    long long nTest1 = 0;
    long long nTest2 = 0;
    //string input, nInput;
    string s_setA = { "abc" }, s_setB = {"ab"};
    vector <string> v_Answers, v_setA, v_setB, v_SetList;

    if (flag == 0)
    {
        cout << "Enter set A (no spaces): ";
        getline(cin, s_setA);

        cout << "Enter set B (no spaces): ";
        getline(cin, s_setB);
    }

    combineSets(v_SetList, s_setA, s_setB);
    nPossibleElements = calcElem(s_setA.size()*s_setB.size()); 
    getList(v_Answers, v_SetList, nPossibleElements);
    printVector(v_Answers);

    cout << "Part2. Enter number of elements in sets(separated by space): ";
    cin >> nTest1 >> nTest2;

    cout << "The answer for |Domain| = " << nTest1 << " and |Codomain| = " << nTest2 << " is " << calcElem(nTest1* nTest2) << endl;

}

    //Prints out the vector elements
void printVector(vector <string> inComing){

        for (size_t i = 0; i < inComing.size(); i++)
        {
            cout << i << ": " << inComing[i] << endl;
        }
    }

        //function to calculate # of possible answers
long long calcElem(long long n1)
    {
        long long temp = 2;

        for (size_t index = 1; index < n1; index++)
        {
            temp *= 2;
        }

        return temp;
    }

        //Building answers combination
void combineSets(vector <string> &tempSet, string set1, string set2)
    {

        for (size_t a = 0; a < set1.size(); a++)
        {
            for (size_t b = 0; b < set2.size(); b++)
            {
                stringstream temp;
                temp << "(" << set1[a] << ", " << set2[b] << "), ";
                tempSet.push_back(temp.str());
            }
        }
    }

void getList(vector <string> &answersList, vector <string> setList, long long nPossibleElements)
    {
        for (size_t index = 0; index < nPossibleElements; index++)
        {
            string s = bitset<numeric_limits<unsigned long long>::digits>(index).to_string();
    string::size_type n = s.find('1');
    string temp;

    if (n != string::npos)
    {
        temp = s.substr(n);
        reverse(temp.begin(), temp.end());
    }

    string tempString = "{";

    for (size_t elem = 0; elem < temp.size(); elem++)
    {
        if (temp[elem] == '1')
        {
            tempString.append(setList[elem]);
        }
    }
    tempString.append("}");
    answersList.push_back(tempString);
    }
}