给定一个权重数组,实现一个带权重的路由策略
Posted 冬马党
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了给定一个权重数组,实现一个带权重的路由策略相关的知识,希望对你有一定的参考价值。
说明:服务的索引就是权重数组的下标
import java.util.*; public class Client { public static void main(String[] args) { int[] arr = {3,4,5,6,7}; List<Weight> weights = new ArrayList<>(); int index = 0; int sum = 0; for (int i = 0; i < arr.length; i++) { Weight weight = new Weight(); weight.setStart(index); weight.setEnd(index + arr[i]); weight.setServer(i); index += arr[i]; weights.add(weight); sum += arr[i]; } Random random = new Random(); for (int i = 0; i < 10; i++) { int round = random.nextInt(sum); for (Weight weight : weights) { if(round >= weight.getStart() && round < weight.getEnd()){ System.out.println("round:"+round+",server:"+weight.getServer()+", start:"+weight.getStart()+",end:"+weight.getEnd()); } } } } }
public class Weight { private int start; private int end; private int server;
以上是关于给定一个权重数组,实现一个带权重的路由策略的主要内容,如果未能解决你的问题,请参考以下文章
给定有权无向图的邻接矩阵如下,求其最小生成树的总权重,代码。