java 将100个学生按照分数和数量均等分配到10个班级


1. 设计思路

第一步 将100个学生的成绩排序(从小到大)。

第二步进行分配:分配采用s型进行分配,

1,2,3,4,5,6,7,8,9,10,

第三步进行倒序分配:

20,19,18,17,16,15,14,13,12,11

依次分配下去,直到所有的人数分配完成。

算法如下:

public static void main(String[] args) {
List score = new ArrayList<>(80);
for (int i = 0; i < 80; i++) {
score.add((int) (Math.random() * 100 + 1));
}
Collections.sort(score);
System.out.println("生成的排序数组:");
System.out.println(score);
//设置班级数
Integer count = 9;
int[][] array = new int[count][(int) (Math.ceil((double) score.size() / count))];
//开始分配
for (int i = 0; i < (int) (Math.ceil((double) score.size() / count)); i++) {
List temp = null;
if (i % 2 == 0) {
try {
temp = score.subList(i * count, (i + 1) * count);
} catch (Exception ex) {
temp = score.subList(i * count, score.size());
}
for (int j = 0; j < temp.size(); j++) {
array[i][j] = temp.get(j);
}
} else {
try {
temp = score.subList(i * count, (i + 1) * count);
} catch (Exception ex) {
temp = score.subList(i * count, score.size());
}
Collections.sort(temp, new Comparator() {
@Override
public int compare(Integer a, Integer b) {
return b.compareTo(a);
}
});
for (int j = 0; j < temp.size(); j++) {
array[i][j] = temp.get(j);
}
}

}
//分配后的数据
System.out.println("分配后的数据");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + ",");
}
System.out.println();
}
}
4. 运行的效果如下: