当前位置:网站首页>字节跳动笔试题2020 (抖音电商)
字节跳动笔试题2020 (抖音电商)
2022-08-09 06:29:00 【史上最强的弟子】
题目: 写一个以权重来进行随机分配的程序。
拿到这个题的时候我思考了一下,幸好我之前看了Ribbon 的权重算法。核心的思想就是把所有的权重值加起来,做随机值计算最终确定随机值在权重总值中的位置,从而确定是那个数据。
import java.util.ArrayList;
import java.util.List;
public class Test12 {
//权重算法
public Weight getWeight(List<Weight> list){
int totalWeight = 0;
for(Weight weight:list){
totalWeight += weight.getValue();
}
double random = Math.random();
Integer weightValue = new Double(Math.ceil(random*totalWeight)).intValue();
totalWeight = 0;
for(Weight weight:list){
totalWeight += weight.getValue();
if(weightValue - totalWeight<=0){
return weight;
}
}
return null;
}
public static void main(String[] args) {
Weight weight = new Weight(1,"A");
Weight weight2 = new Weight(2,"B");
Weight weight3 = new Weight(3,"C");
List<Weight> list = new ArrayList<>();
list.add(weight);
list.add(weight2);
list.add(weight3);
Test12 test12 = new Test12();
Weight returnWeight = test12.getWeight(list);
System.out.println(returnWeight);
}
}
class Weight{
private Integer value;
private String name;
public Weight(Integer value,String name){
this.value = value;
this.name = name;
}
public Integer getValue() {
return value;
}
}输出结果:

边栏推荐
猜你喜欢

报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disab

运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比

Flask failed to create database without error

Cysteine/Galactose/Perylenediimide Functionalized Fe3O4 Fe3O4 Nanomaterials | Scientific Research Reagents

C# 利用iTextSharp画PDF

Inception V3 闭眼检测

GNNExplainer应用于节点分类任务

运放-运算放大器经典应用电路大全-应用电路大全

Harbor Enterprise Mirror Warehouse Construction

代码目录结构
随机推荐
Text String Length Sorting - Online Tool
PDF不能打印和复制的问题如何解决?
中英文说明书丨CalBioreagents ACTH N端单克隆抗体
什么是excel文件保护
APP product source data interface (taobao, jingdong/spelling/suning/trill platform details a lot data analysis interface) code and docking tutorial
Program Performance Analysis - Complexity Analysis
Use of PlantUML plugin in idea
workbench 数据导出
DDD 领域驱动设计
像天才一样思考:如何培养自己的创造力?
运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
Unity 五子棋游戏设计和简单AI(2)
[R language] interaction test data
[R language] Normalize and organize files into folders of various file types
VB.net程序关闭后后台还在与SQL连接
深度学习-神经网络原理2
C# 利用iTextSharp画PDF
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝
报错:flask: TypeError: ‘function‘ object is not iterable