Java 如何均衡分配学生

Java 如何均衡分配学生,java,mysql,sql,Java,Mysql,Sql,我有下面的虚拟表格 +----+-------+-------+-----+--------+ | Id |Marks |Grade |GId |Gender | +----+-------+-------+-----+--------+ | 1 | 95 | S | 1 | 1 | | 2 | 95 | S | 1 | 0 | | 3 | 93 | S | 1 | 0 | | 4 | 75

我有下面的虚拟表格

+----+-------+-------+-----+--------+
| Id |Marks  |Grade  |GId  |Gender  |
+----+-------+-------+-----+--------+
| 1  | 95    | S     | 1   | 1      |
| 2  | 95    | S     | 1   | 0      |
| 3  | 93    | S     | 1   | 0      |
| 4  | 75    | B     | 3   | 1      |
| 5  | 74    | B     | 3   | 1      |
| 6  | 83    | A     | 2   | 0      |
| 7  | 83    | A     | 2   | 0      |
| 8  | 83.5  | A     | 2   | 0      |
| 9  | 70    | B     | 3   | 0      |
| 10 | 71    | B     | 3   | 1      |
| 11 | 96    | S     | 1   | 1      |
| 12 | 60    | C     | 4   | 1      |
| 13 | 65    | C     | 4   | 0      |
| 14 | 62    | C     | 4   | 0      |
| 15 | 71    | B     | 3   | 1      |
+----+-------+-------+-----+--------+
我想把它分成一些随机的部分

比如说3节,A=5节,B=5节,C=5节学生,例如。 A=[SABCS],B=[SABCB],C=[SABCB]//等级

比如说4节,A=4节,B=4节,C=4节,D=3节学生,例如。 A=[SABC],B=[SABC],C=[SABC],D=[SBB]//成绩

假设两个部分A=8秒,B=7秒学生,例如。 A=[SABCSABC],B=[SABCSBB]//成绩

我遵循了这个方法,得到了结果,但在均衡分布方面有很多局限性

我如何设置并获得所需的结果,而分区将是动态的

MYSQL将是一个更好的选择。 2.也可以通过java/else编码

注意:为没有得到它的人更新

请看表格和相应的分数。然后将分数实际划分为每个部分的学生。每个部分应该/必须包含每个年级的学生。这就是为什么我对平衡分布进行了标注。

以下方法按gradeId对数据进行排序,并同时填充每个部分


输出随示例而异,但数据分布均匀。[您可以编辑NumberOfSection、students和试用]

如果我理解正确,您可能希望在每组中尽可能均匀地添加所有年级的学生。在这种情况下,我将执行以下操作:

把学生按年级分类 应用中的逻辑来构建组 按照上述问题中的临时列组进行排序。 以上可以说是最简单的实现。您可能希望添加一条规则,其中每个组的分数尽可能接近平均值,大致如下所示


恐怕对于更复杂的算法,您需要使用java/python之类的通用语言。

我不明白您希望如何在每个部分中进行选择。它是随机的吗?还是你想要某种平衡?。如果它是随机的,那么输入表有什么关系呢?您只需要从列grade right中的唯一值集中随机设置4个组?您的问题是您想要的输出,以及您迄今为止尝试过的查询或任何代码。但是,我会给你一个提示,在MySQL中,使用MOD获取提醒,COUNT*获取行数,然后使用你的数学技能,例如如果计数不能被集合数整除,那么减去COUNT-提醒,并在MySQL查询中使用返回的数字,例如限制行数,跳过行,可能是选择每个组的循环,甚至是java循环。谢谢Ivan,我已经参考了您的第一个链接,但发现了一些限制。与你的第二个链接混合听起来不错。谢谢Onkar,接受你的soln。
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public class SectionDivider {
  public static void main(String[] args) {
    List<Student> students =
        Arrays.asList(
            new Student(1, 95f, "S", 1),
            new Student(2, 95f, "S", 1),
            new Student(3, 93f, "S", 1),
            new Student(4, 75f, "B", 3),
            new Student(5, 74f, "B", 3),
            new Student(6, 83f, "A", 2),
            new Student(7, 83f, "A", 2),
            new Student(8, 83.5f, "A", 2),
            new Student(9, 70f, "B", 3),
            new Student(10, 71f, "B", 3),
            new Student(11, 96f, "S", 1),
            new Student(12, 60f, "C", 4),
            new Student(13, 65f, "C", 4),
            new Student(14, 62f, "C", 4),
            new Student(15, 71f, "B", 3));

    int numberOfSections = 4;
    String[] divided = new String[numberOfSections];
    int divIdx = 0;

    Map<Integer, List<Student>> groupedGrades =
        students
            .stream()
            .sorted((d1, d2) -> d1.gradeId.compareTo(d2.gradeId))
            .collect(Collectors.groupingBy(d -> d.gradeId));

    while (true) {
      boolean allEmpty = true;
      for (Entry<Integer, List<Student>> entry : groupedGrades.entrySet()) {
        List<Student> value = entry.getValue();
        int size = value.size();
        for (int i = 0; i < numberOfSections && i < size; i++) {
          allEmpty = false;
          if (divided[divIdx] == null) {
            divided[divIdx] = "";
          }
          divided[divIdx] = divided[divIdx].concat(value.get(0).grade);
          divIdx = (divIdx + 1) % numberOfSections;
          value.remove(0);
          entry.setValue(value);
        }
      }
      if (allEmpty) {
        break;
      }
    }

    Arrays.stream(divided).forEach(System.out::println);
  }
}

class Student {
  Integer id;
  Float marks;
  String grade;
  Integer gradeId;

  public Student(Integer id, Float marks, String grade, Integer gradeId) {
    super();
    this.id = id;
    this.marks = marks;
    this.grade = grade;
    this.gradeId = gradeId;
  }
}