当前位置:网站首页>字节跳动笔试题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;
}
}
输出结果:
边栏推荐
- MongDb的查询方式
- Getting started with kubernetes apparmor
- Adds, deletes, searches, and changes the leading doubly circular linked list (implemented in C language)
- [MySQL]二、进程的关系、MySQL密码破解、建表和建库相关命令
- Ferric oxide/bismuth sulfide nanocomposites ([email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop
- XxlJobConfig分布式定时器任务管理XxlJob配置类,替代
- 中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
- MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
- 22 high mid term paper topics forecast
- A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
猜你喜欢
Harbor Enterprise Mirror Warehouse Construction
[MySQL]二、进程的关系、MySQL密码破解、建表和建库相关命令
Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
redis 运行lua 脚本 出现Invalid argument(s)
GNNExplainer applied to node classification task
How to find package information and pin definitions for NXP S32K1xx series microcontrollers
05 多线程与高并发 - ThreadPoolExecutor 源码解析
Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
IQ Products CMV Brite Turbo试剂盒的原理
[GO], arrays and slices
随机推荐
2022.8.8DAY628
思维方法 解决问题的能力
Online tool for sorting multi-line strings
crc calculation
Adds, deletes, searches, and changes the leading doubly circular linked list (implemented in C language)
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
untiy countdown
vs番茄助手的方便功能和便捷快捷键介绍
【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
jdepend
中英文说明书丨TRC D-阿卓糖(D-Altrose)
CalBioreagents超全Id 蛋白兔单克隆抗体,助力科研
Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
像天才一样思考:如何培养自己的创造力?
golang xml 处理动态属性
电学知识的疑问
Error jinja2.exceptions.UndefinedError: 'form' is undefined
tidb crash test
ZIP压缩包文件删除密码的方法
Simple Factory Pattern