当前位置:网站首页>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:
边栏推荐
猜你喜欢
输入框最前面添加放大镜&&background-image和background-color冲突问题
Built-in macros in C language (define log macros)
运放-运算放大器经典应用电路大全-应用电路大全
Likou Brush Question 180
Excel受保护的工作表怎么操作?
变压器的工作原理(图解,原理图讲解,一看就懂)
INSTALL_RPATH and BUILD_RPATH problem in CMake
CMake中INSTALL_RPATH与BUILD_RPATH问题
C语言实现顺序栈和链队列
Quectel EC20 4G module dial related
随机推荐
IQ Products CMV Brite Turbo试剂盒的原理
常用Oracle命令
缓存技术使用
C语言的内置宏(定义日志宏)
C language implements sequential stack and chain queue
Reverse Engineering
Excel受保护的工作表怎么操作?
【R语言】对文件进行归一化整理到各文件类型文件夹
事务总结
Integer 线程安全的
【R语言】交互作用 测试数据
SIGINT,SIGKILL,SIGTERM信号区别,各类信号总结
中英文说明书丨CalBioreagents 醛固酮单克隆抗体
2022.8.8DAY628
Unity Gobang Game Design and Simple AI(3)
[R language] interaction test data
简单使用Lambda表达式
GNNExplainer applied to node classification task
flask创建数据库失败未报错
String.toLowerCase(Locale.ROOT)