组合组合java

发布于 2024-09-24 20:38:06 字数 659 浏览 3 评论 0原文

我需要在JAVA中找到组合的组合。

例如,我班上有 6 个学生。其中,我需要创建 4 人的组合,对于每个组,我可以选择一个亲密的 2 人组。

我必须确保没有双人组(顺序无关紧要)。!并需要打印4人组。

然而,这是困难的部分:

因此用数字定义学生:

如果我打印出 1234 作为组合之一,我不能也打印出1256,因为12同时出现在12341256中。

我怎样才能用Java编写它?

([1,2,3,4,5],3,2) 的EDITED

输出将是:

  1. 不重复的组合 (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. 删除 2 个元素的重复组,将只剩下我: {1,2,3} {1,4,5}(我删除了具有 12,13,23,45,14,15 组合的组,因为它们已经出现在我的前两个组中已经找到了。

I need to find combination of combination in JAVA.

I have for instance 6 students in class. 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.

I have to make sure that there are no doubles (order does not matter).! and need to print the 4 people group.

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 out1256 as well, since 12 appears both in 1234 and in 1256.

How can I write it in Java?

EDITED

output of ([1,2,3,4,5],3,2) will be:

  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}

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-10-01 20:38:06

好的,这是您所描述的过程的简单模拟。但我使用二进制数来表示集合,这使得操作更容易。例如,数字 19 的二进制形式是 10011:这意味着选择了 0、3、4 号学生(这些位置上都是 1)。

先来个小帮手。

// 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;
}

以及主程序。欢迎提出建议。

    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);
        }
    }

但对于大输入,它可能需要改进(例如记忆化)。但目前,由于您没有说出您期望什么样的输入,我认为这已经足够好了。

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).

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.

烟花易冷人易散 2024-10-01 20:38:06

想象一下,您有一个 Student 对象,该对象使用 equals 来比较其主键。在您的示例中,学生 1 将返回 1,学生 2 将返回 2,依此类推。

将它们全部放入集合中,这样可以确保不会出现重复。

将集合迭代 4,然后迭代 2,将返回您想要的结果。

Imagine you have a Student object with an equals comparing their Primarykey. In your example, student 1 will return 1, 2 will return 2 and so on.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文