Home > Back-end >  How to put seven set is divided into two groups of intersection set?
How to put seven set is divided into two groups of intersection set?

Time:10-28

Known 7 sets and two intersecting situation between them, how they are divided into two groups with public intersection set? (assuming that will divide the success, and to consider more than one division)

For example, assume that there are seven set, 1,2,3,4,5,6,7.
Known set intersection relation:
2 to 1, 3, 6 (2,3,6,7 respectively with 1 overlap)
2:1, 3,6,4
3:1, 2,6,5
4:5, 7,2,6
Five, seven, 3
6:1, 2 and 4
17, 5, 1

The final program output
(5)
(4, 5, 7)

CodePudding user response:

In combination way is divided into two groups, and then judge whether meet the overlap relationship between the 2 groups

 void combine (vector * arr, int len, int CNT) {//from arr CNT elements grouped 
Int I, j, k, ok;
Vector Gp1 (CNT), gp2 (len - CNT);//are divided into two groups, one group of CNT elements, a set of len - CNT element
for (i=0; iWhile (gp1 [0] Ok=1;
for (i=0; iIf (gp1 [I] & gt; + 1=gp1 [I]) {//give priority to your growing up (i.e., choose [1, 7])
Ok=0;
break;
}
}
If (ok) {//selected after the first set of
For (I=0, k=0; iFor (j=0; jIf (I==gp1 [j]) break;
}
If (j==CNT) gp2 [k++]=I;//the first group's will automatically become the second
}
for (i=0; Ok& & iOk=0;
For (j=0; jIf (arr [gp2 [j]]. The end ()
!=find (arr gp2 [j] [] the begin (), arr gp2 [j] [] the end (), gp1 [I] + 1)) {
Ok=1;
break;
}
}
}
for (i=0; Ok& & iOk=0;
For (j=0; jIf (arr [gp1 [j]]. The end ()
!=find (arr [gp1 [j]]. The begin (), arr [gp1 [j]]. The end (), gp2 [I] + 1)) {
Ok=1;
break;
}
}
}
If (ok) {//if are ok to print the results
Printf (" % d ", gp1 [0] + 1);
for(int i=1; iPrintf (" % d ", gp1 [I] + 1);
}
Printf (", ");
Printf (" % d ", gp2 [0] + 1);
for(int i=1; iPrintf (" % d ", gp2 [I] + 1);
}
Printf ("] \ n ");
}
}
//simulation hexadecimal (full 7 into 1) increasing combinations (like binary full 2 1000001010011100101110111)
Gp1 [CNT - 1] + +;//(if you still don't understand, just as I started from 1234567 to 7654321 cycle, cycle after each round i++)
For (I=CNT - 1; I> 0; I -) {
If (gp1 [I]==len) {
Gp1 [I]=0;
Gp1 [I - 1] + +;
}
}
}
}

Int main ()
{
int i;
Vector Arr [7].//7 collection
,3,6,7 arr [0]={2};//save set relationships
Arr [1]=,3,6,4 {1};
Arr [2]=,2,6,5 {1};
Arr [3]={5,7,2,6};
,7,3 arr [4]={4};
Arr [5]={1, 2, 3, 4};
,5,1 arr [6]={4};
for (i=0; i<7/2. I++) {
Combine (arr, 7, I + 1);
}
Return 0;
}

CodePudding user response:

No man or woman? Help me!
  • Related