赞
踩
- int number = 1000;//假设1000个订单数
- Double[] weight = new Double[]{0D,2D,2D};
- //已分配订单数
- Integer[] count = new Integer[weight.length];
- for (int i = 0; i < number; i++) {
- //当前权重
- Double[] current = new Double[weight.length];
- for (int w = 0; w < weight.length; w++) {
- current[w] = weight[w] / (count[w] == null ? 1 : count[w]);
- }
- int index = 0;
- Double currentMax = current[0];
- for (int d = 1; d < current.length; d++) {
- //考虑全等的情况
- Boolean isTrue = true;
- while (isTrue) {
- Set set = new HashSet();
- for (Double c : current) {
- set.add(c);
- }
- if (set.size() == 1) {//代表全等
- for (int e = 0; e < current.length; e++) {
- current[e] = current[e] * Math.random();
- }
- } else {
- isTrue = false;
- }
- }
- //比较所有的数,寻找出下标最大的哪一位
- if (currentMax < current[d]) {
- currentMax = current[d];
- index = d;
- }
- }
- count[index] = count[index] == null ? 1 : count[index] + 1;
- }
- for (Integer i : count) {
- System.err.println(i);
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。