javascript 计算值所占的百分比

硅谷探秘者 前端 196 0 0

js 计算值所占的百分比:

function getPercentValue(arr){
        //求和
        let sum = 0;
        if(sum <= 0){
            for (let i = 0; i < arr.length; i++) {
                sum += arr[i];
            }
        }
        //10的2次幂是100,用于计算精度。
        let digits = 1;
        //扩大比例100
        let votesPerQuota = [];
        for(let i = 0; i < arr.length; i++){
            let val = arr[i] / sum * digits * 100;
            votesPerQuota[i] = val;
        }
        //总数,扩大比例意味的总数要扩大
        let targetSeats = digits * 100;
        //再向下取值,组成数组
        let seats = [];
        for(let i = 0; i < votesPerQuota.length; i++){
            seats[i] = Math.floor(votesPerQuota[i]);
        }
        //再新计算合计,用于判断与总数量是否相同,相同则占比会100%
        let currentSum = 0;
        for (let i = 0; i < seats.length; i++) {
            currentSum += seats[i];
        }
        //余数部分的数组:原先数组减去向下取值的数组,得到余数部分的数组
        let remainder = [];
        for(let i = 0; i < seats.length; i++){
            remainder[i] = votesPerQuota[i] - seats[i];
        }
        while(currentSum < targetSeats){
            let max = 0;
            let maxId = 0;
            for(let i = 0;i < remainder.length;++i){
                if(remainder[i] > max){
                    max = remainder[i];
                    maxId = i;
                }
            }
            //对最大项余额加1
            ++seats[maxId];
            //已经增加最大余数加1,则下次判断就可以不需要再判断这个余额数。
            remainder[maxId] = 0;
            //总的也要加1,为了判断是否总数是否相同,跳出循环。
            ++currentSum;
        }
        // 这时候的seats就会总数占比会100%
        return seats;
    }

let res = getPercentValue([99,90,77,10])
console.log(res);

输出:[36, 32, 28, 4]

猜你喜欢
java基础,算法基础 184 输入一个整数数组a,输出一个同样长度数组b表示对应a数组各个位置上元素a数组总。publicstaticdouble[]getPercentValue(int[]arr){//求和
前端(h5) 2466 定义和用法splice()方法向/从数组中添加/删除项目,然后返回被删除项目。注意:该方法会改变原始数组。语法arrayObject.splice(index,howmany,item1
工具 4226 publicstaticvoidmain(String[]args){ inta=3;intb=9;NumberFormatnumberFormat=NumberFormat.getInstance();numberFormat.setMaximumFractionDigits(2);Stringresult=numberFormat.format((float)a/(float)b*100);S
nginx,前端,java基础 751   基于javanio+netty+websocket+protobuf+javascript等技术实现前后端高性能实时数据传输demo模型。  github地址:https
official 633   基于javanio+netty+websocket+protobuf+javascript等技术实现前后端高性能实时数据传输demo模型。  github地址:https
前端(h5) 1704 javascript代码实现scripttype="text/javascript"document.addEventListener('copy',function(event
前端(h5) 2769 语法:varmap=newMap();属性:size返回集合中元素数量方法:clear从映射中移除有元素。delete从映射中移除指定元素。forEach对映射中每个元素执行指定操作。get
weblog 2184 了解优先级队列详细叙述请访问(java实现):java用数组实现优先级队列(小顶堆) 实验目: 先按key优先,如果key相等再按value.hg优先。 插入数据案例
归档
2018年11月  12 2018年12月  33 2019年01月  28 2019年02月  28 2019年03月  32 2019年04月  27 2019年05月  33 2019年06月  6 2019年07月  12 2019年08月  12 2019年09月  21 2019年10月  8 2019年11月  15 2019年12月  25 2020年01月  9 2020年02月  5 2020年03月  16 2020年04月  4 2020年06月  1 2020年07月  7 2020年08月  13 2020年09月  9 2020年10月  5 2020年12月  3 2021年01月  1 2021年02月  5 2021年03月  7 2021年04月  4 2021年05月  4 2021年06月  1 2021年07月  7 2021年08月  2 2021年09月  8 2021年10月  9 2021年11月  16 2021年12月  14 2022年01月  7 2022年05月  1 2022年08月  3 2022年09月  2 2022年10月  2 2022年12月  5 2023年01月  3 2023年02月  1 2023年03月  4 2023年04月  2
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。