public static List<Integer> genWeightList(int totalWeight, int typeNum) {
ArrayList<Integer> list = new ArrayList<>();
int remainder = totalWeight % typeNum;
int avgWeight = totalWeight / typeNum;
for (int i = 0; i < typeNum; i++) {
list.add(avgWeight);
}
for (int i = 0; i < typeNum && remainder > 0; i++) {
Integer integer = list.get(i);
list.set(i, integer + 1);
remainder--;
}
return list;
}
先均分再挨个匀
改进一下
public static List<Integer> genWeightList(int totalWeight, int azCount) {
ArrayList<Integer> list = new ArrayList<>();
int remainder = totalWeight % azCount;
int avgWeight = totalWeight / azCount;
for (int i = 0; i < azCount; i++) {
if (remainder>0){
list.add(avgWeight+1);
remainder--;
}else {
list.add(avgWeight);
}
}
return list;
}
|