计算各项分类百分比

package com.example.demo.tempTest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:sunting
 * @date:2023/8/17 10:56
 */
public class test {
    public static List<String> getDoubleRatio(List<Integer> prices) {
        double sum = 0;
        // 求和
        for (int i = 0; i < prices.size(); i++) {
            Integer price = prices.get(i);
            if (price == null) {
                continue;
            }
            sum += price;
        }

        List<String> aa = new ArrayList<>();
        for (int idx = 0; idx < prices.size(); idx++) {
            if ((prices.size() - 1) < idx) {
                aa.add("0%");
                break;
            }
            //求和
            if (sum <= 0) {
                for (int j : prices) {
                    sum += j;
                }
            }
            if (sum == 0) {
                aa.add(0 + "%");
                continue;
            }
            //10的2次幂是100,用于计算精度。
            double digits = Math.pow(10, 2);
            //扩大比例100
            double[] votesPerQuota = new double[prices.size()];
            for (int i = 0; i < prices.size(); i++) {
                Integer pr = prices.get(i);
                if (pr == null) {
                    pr = 0;
                }
                double val =  pr / sum * digits * 100;
                votesPerQuota[i] = val;
            }
            //总数,扩大比例意味的总数要扩大
            double targetSeats = digits * 100;
            //再向下取值,组成数组
            double[] seats = new double[prices.size()];
            for (int i = 0; i < votesPerQuota.length; i++) {
                seats[i] = Math.floor(votesPerQuota[i]);
            }
            //再新计算合计,用于判断与总数量是否相同,相同则占比会100%
            double currentSum = 0;
            for (double seat : seats) {
                currentSum += seat;
            }
            //余数部分的数组:原先数组减去向下取值的数组,得到余数部分的数组
            double[] remainder = new double[prices.size()];
            for (int i = 0; i < seats.length; i++) {
                remainder[i] = votesPerQuota[i] - seats[i];
            }
            while (currentSum < targetSeats) {
                double max = 0;
                int maxId = 0;
                for (int 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%
            aa.add(seats[idx] / digits + "%");
        }
        return aa;
    }

    public static void main(String[] args) {
        List<Integer> prices = new ArrayList<>();
        prices.add(65);
        prices.add(63);
        prices.add(63);
        prices.add(0);
        prices.add(0);
        System.out.println(getDoubleRatio(prices));
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值