当前位置:网站首页>ByteDance Written Exam 2020 (Douyin E-commerce)
ByteDance Written Exam 2020 (Douyin E-commerce)
2022-08-09 06:33:00 【The strongest disciple in history】
Title: Write a program that randomly assigns weights.
I thought about it when I got this question. Fortunately, I read Ribbon's weighting algorithm before.The core idea is to add up all the weight values, do random value calculation, and finally determine the position of the random value in the total weight value, so as to determine the data.
import java.util.ArrayList;import java.util.List;public class Test12 {//weight algorithmpublic Weight getWeight(List 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 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;}}
Output result:
边栏推荐
- 工控设备的系统如何进行加固
- sql problem solving statement to create a table
- P7阿里面试题2020.07 之滑动窗算法(阿里云面试)
- AD的library中 库文件后缀有.intlib .schlib .pcblib 的区别
- 电学知识的疑问
- crc calculation
- [R language] interaction test data
- [MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
- 中英文说明书丨TRC D-阿卓糖(D-Altrose)
- 单例模式
猜你喜欢
PDF不能打印和复制的问题如何解决?
MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
6 states of a thread
治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
IQ Products CMV Brite Turbo试剂盒的原理
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
pdf加密、找回密码
什么是excel文件保护
workbench 数据导出
随机推荐
Service
Built-in macros in C language (define log macros)
中英文说明书丨TRC D-阿卓糖(D-Altrose)
The division principle summary within the collection
直接用的zip包 缺少很多依赖,pip没有,感觉用anaconda create一个环境会方便点
PDF不能打印和复制的问题如何解决?
Simple Factory Pattern
数据库中间件-jdbi
事务总结
Fragments
GNNExplainer applied to node classification task
Import the pycharm environment package into another environment
IQ Products CMV Brite Turbo试剂盒的原理
el-table缓存数据
C语言实现顺序栈和链队列
e-learning summary
SIGINT,SIGKILL,SIGTERM信号区别,各类信号总结
db.sqlite3没有“as Data Source“解决方法
workbench 数据导出
[GO], arrays and slices