当前位置:网站首页>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:
边栏推荐
猜你喜欢
Import the pycharm environment package into another environment
中英文说明书丨TRC D-阿卓糖(D-Altrose)
带头双向循环链表的增删查改(C语言实现)
C language implements sequential stack and chain queue
C语言的内置宏(定义日志宏)
字节跳动面试题之镜像二叉树2020
运放-运算放大器经典应用电路大全-应用电路大全
Altium designer软件常用最全封装库,包含原理图库、PCB库和3D模型库
Integer 线程安全的
ByteDance Interview Questions: Mirror Binary Tree 2020
随机推荐
String.toLowerCase(Locale.ROOT)
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
Redis 2 - 高级
io.lettuce.core。RedisCommandTimeoutException命令超时
AD的library中 库文件后缀有.intlib .schlib .pcblib 的区别
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
报错jinja2.exceptions.UndefinedError: ‘form‘ is undefined
BeautifulSoup4的介绍与使用
mmdetection源码解析--ResNet18
锁执行的过程
PDF不能打印和复制的问题如何解决?
普罗米修斯原理及节点发布
Remember a nest.js route that matches all the path problems that follow
golang zip aes base64
05 多线程与高并发 - ThreadPoolExecutor 源码解析
vim 程序编辑器的基本操作(积累)
Qt 学习(三) —— Qt 模块
Quectel EC20 4G module dial related
crc计算
ZIP压缩包文件删除密码的方法