繁体   English   中英

如何均衡分配学生

[英]How to distribute students in a balanced way

我有以下虚拟表。

+----+-------+-------+-----+--------+
| 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      |
+----+-------+-------+-----+--------+

我想把它分成一些随机的号码。 的部分。

  1. 说 3 节 SEC A=5,SEC B=5,SEC C=5 学生,例如

A=[SABCS],B=[SABCB],C=[SABCB] //成绩

  1. 说 4 节 SEC A=4, SEC B=4,SEC C=4, SEC D=3 学生 例如

A=[SABC],B=[SABC],C=[SABC], D=[SBB] //成绩

  1. 说 2 节 SEC A=8,SEC B=7 学生,例如

A=[SABCSABC],B=[SABCSBB] //成绩

我按照链接得到了结果,但平衡分配有很多限制。

我如何设置并获得所需的结果,其中部分划分将是动态的。

  1. MYSQL 将是首选。

2.通过java/else编码也可以。

注意:更新谁没有得到它。

请看表格和相应的成绩。然后划分每个部分的成绩(实际上是学生)。 每个部分应该/必须包含每个年级的学生。 这就是为什么我提到平衡分配。

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

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

输出因您的示例示例而异,但会均匀分布数据。 [您可以编辑numberOfSectionsstudents并试用]

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

  1. 按年级对学生进行排序
  2. 应用所引用问题中的逻辑来构建组
  3. 按上述问题中的临时列(组)排序。

以上可以说是最简单的实现。 您可能想要添加一个规则,使每个组的成绩尽可能接近平均值,遵循此方法

恐怕对于更复杂的算法,您需要转向像 java/python 这样的通用语言。

暂无
暂无

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

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