[英]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 |
+----+-------+-------+-----+--------+
我想把它分成一些随机的号码。 的部分。
A=[SABCS],B=[SABCB],C=[SABCB] //成绩
A=[SABC],B=[SABC],C=[SABC], D=[SBB] //成绩
A=[SABCSABC],B=[SABCSBB] //成绩
我按照链接得到了结果,但平衡分配有很多限制。
我如何设置并获得所需的结果,其中部分划分将是动态的。
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;
}
}
输出因您的示例示例而异,但会均匀分布数据。 [您可以编辑numberOfSections
, students
并试用]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.