I have to combine the combination of Java.
I have 6 students in the class in the class. Among them, I need to create a combination of 4 people in the group and for each group, I can select the intimate group of 2.
I have to make sure that there is no couple (no matter)! And the group of 4 people needs to be printed.
However, this is a difficult part:
Define students with numbers:
If I print 1234
1234
As a combination, I can not print 1256
as 12
Both appear in 1234
and 1256
.
How can I write it in Java?
edit
([1,2,3,4,5], 3,2) will be produced:
-
Combination without recurrence (N = 5, r = 3) {1,2,3} {1,2,4} {1,2,5} {1,3,4} {1 , 3,5} {1,4,5} {2,3,4} {2,3,5} {2,4,5} {3,4,5}
-
Removing the repeated groups of two elements, just leave me: {1,2,3} {1,4,5}
(I have destroyed those groups whose 12 , 13,23,45,14,15 are the same as they already appear before the two which I have found.
OK, here's the simple Karan is the process that you described. But I set b resistance to binary numbers, this manipulator makes it easy, for example, the number 191 1011 is in binary form: This means students 0, 3 and 4 are selected (Those posts should be 1).
First a small assistant.
// Returns all the subsets of 'set', in which the size 'alltsis' is set Is & lt; Integer & gt; all subsets (ANT set, int subset size) {set & l T; Integer & gt; Result = NewHashet & lt; Integer & gt; (); If (subsetSize == 0) {result.add (0); Return result; } If (set == 0) {return result; } // Check that the first element is present if (set to% 2 == 1) {// use the first element, to collect a lower element (integer I: all subsets (set / 2, subset size -1 )) {Result.add (I * 2 + 1); }} // 1 element is not used (integer I: all subsets (set / 2, subset size)) {result.add (i * 2); } Return results; }
And main program suggestions are welcome.
int n = 5; Int M = 3; Int Z = 2; & Lt; Integer & gt; Results = New Arreelist & lt; Integer & gt; (); // Find the group of all the elements, which are integers with the 'whole' integer, full = (1 <- lt; N) - 1; (STS: Check all subsets of all substrates (Whole SET, M)) / / set boolean valid = true 'z' elements; (Integer: all substrates (s, z)) // If this Z-element subset has already been used (int past: results) {// check if 't' is a subset of 'past' {(Past | t =) last) {legitimate = wrong; breakage;}} If (! Valid) {break}}} If (valid) {// Z element element was used before the result, then the result .}}
But it may require correction for large input (like) but for now, since you do not say this What kind of input do you expect, I think it's great.
Comments
Post a Comment