简体   繁体   English

组合java的组合

[英]combination of combination java

I need to find combination of combination in JAVA. 我需要在JAVA中找到组合的组合。

I have for instance 6 students in class. 我在课堂上有6名学生。 Out of them, I need to create combination of 4 people in group, and for each group I can choose an intimate group of 2. 其中,我需要在小组中创建4个人的组合,并且对于每个小组,我可以选择2个亲密小组。

I have to make sure that there are no doubles (order does not matter).! 我必须确保没有双打(顺序无关紧要)。 and need to print the 4 people group. 并需要打印4人组。

However, this is the hard part: 但是,这是困难的部分:

So defining students with numbers: 所以用数字定义学生:

If I print out 1234 as one of the combinations, I can't print out 1256 as well, since 12 appears both in 1234 and in 1256 . 如果我打印 1234作为其中一个组合, 我也不能打印出1256 ,因为在12341256都出现了12

How can I write it in Java? 我怎么用Java编写它?

EDITED EDITED

output of ([1,2,3,4,5],3,2) will be: ([1,2,3,4,5],3,2)的输出将是:

  1. Combinations without repetition (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} 组合不重复(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}

  2. deleting repeating groups of 2 elements, will leave me only: {1,2,3} {1,4,5} (i deleted groups that have combinations of 12,13,23,45,14,15 since they already appear in the first two that I have found. 删除2个元素的重复组,只留下我: {1,2,3} {1,4,5} (我删除了组合为12,13,23,45,14,15的组,因为它们已经出现在我找到的前两个。

Ok, here's the simple emulation of the process you described. 好的,这是您描述的过程的简单模拟。 But I use binary numbers to present set, it makes manipulations easier. 但我使用二进制数来表示集合,它使操作更容易。 For example, number 19 is 10011 in binary form: it means students 0, 3 and 4 are selected (there're 1's in those positions). 例如,数字19是二进制形式的10011:它意味着学生0,3和4被选中(这些位置有1个)。

A little helper first. 先帮助一个小帮手。

// return all subsets of 'set', having size 'subsetSize'
Set<Integer> allSubsets(int set, int subsetSize) {
    Set<Integer> result = new HashSet<Integer>();
    if (subsetSize == 0) {
        result.add(0);
        return result;
    }
    if (set == 0) {
        return result;
    }

    // check if 1st element is present
    if (set % 2 == 1) {
        // use 1st element, one less element to collect
        for (Integer i : allSubsets(set / 2, subsetSize - 1)) {
            result.add(i * 2 + 1);
        }
    }
    // not use 1st element
    for (Integer i : allSubsets(set / 2, subsetSize)) {
        result.add(i * 2);
    }

    return result;
}

And main program. 和主程序。 Suggestions are welcome. 欢迎提出建议。

    int N = 5;
    int M = 3;
    int Z = 2;

    List<Integer> result = new ArrayList<Integer>();

    // get all groups of M elements from 'wholeSet'
    int wholeSet = (1 << N) - 1;
    for (int s : allSubsets(wholeSet, M)) {
        // Check all subsets of 'Z' elements from set 's'
        boolean valid = true;
        for (int t : allSubsets(s, Z)) {
            // check if this Z-element subset already was used
            for (int past : result) {
                // check if 't' is subset of 'past' set
                if ((past|t) == past) {
                    valid = false;
                    break;
                }
            }
            if (!valid) {
                break;
            }
        }

        if (valid) {
            // none of Z-element subsets of 's' were used before
            result.add(s);
        }
    }

But it may require improvements (like memoization ) for big inputs. 但它可能需要对大输入进行改进(如记忆 )。 But for now, since you don't say what kind of input you expect, I assume this is good enough. 但就目前而言,由于你没有说出你期望的输入,我认为这已经足够了。

Imagine you have a Student object with an equals comparing their Primarykey. 想象一下,你有一个Student对象,它等于比较他们的Primarykey。 In your example, student 1 will return 1, 2 will return 2 and so on. 在您的示例中,学生1将返回1,2,将返回2,依此类推。

Put them all in the set, this will ensure that there will be no double. 将它们全部放入集合中,这将确保不会出现双重问题。

Iterate though the set by 4 then by 2 and will return you your desired result. 通过4然后按2迭代,将返回您想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM