当前位置:网站首页>计算饼状图百分比
计算饼状图百分比
2022-04-23 16:40:00 【轻松的小希】
需求描述:
给定一个整数数组,例如:[2, 3, 4],计算各个元素的百分比,要求百分比累加为100%。
input = [2, 3, 4]
output = [22.22, 33.33, 44.45]
项目依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
代码实现:
public class PercentUtils {
/** * 计算饼状图百分比 * * @param arr * @param idx * @param precision * @return */
public static double getPercentValue(int[] arr, int idx, int precision) {
//判断索引
if (idx < 0 || (arr.length - 1) < idx) {
return 0;
}
//数组求和
double sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
if (sum <= 0) {
return 0;
}
//计算精度
double digits = Math.pow(10, precision);
//扩大比例
double[] votesPerQuota = new double[arr.length];
for (int i = 0; i < arr.length; i++) {
double val = arr[i] / sum * digits * 100;
votesPerQuota[i] = val;
}
//扩大总数
double targetSeats = digits * 100;
//向下取值
double[] seats = new double[arr.length];
for (int i = 0; i < votesPerQuota.length; i++) {
seats[i] = Math.floor(votesPerQuota[i]);
}
//重新累加
double currentSum = 0;
for (int i = 0; i < seats.length; i++) {
currentSum += seats[i];
}
//余数数组
double[] remainder = new double[arr.length];
for (int i = 0; i < seats.length; i++) {
remainder[i] = votesPerQuota[i] - seats[i];
}
//最大余数
while (currentSum < targetSeats) {
double max = Double.MIN_VALUE;
int maxId = 0;
for (int i = 0; i < remainder.length; ++i) {
if (remainder[i] > max) {
max = remainder[i];
maxId = i;
}
}
//对最大项余额加一
++seats[maxId];
//这个余数已经使用
remainder[maxId] = 0;
//总数也要相应加一
++currentSum;
}
//返回占比
return seats[idx] / digits;
}
}
运行效果:
@SpringBootTest
public class TestPercentUtils {
@Test
public void testGetPercentValue01() {
int[] arr = new int[]{
2, 3, 4};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + " percent = " + PercentUtils.getPercentValue(arr, i, 2));
}
}
@Test
public void testGetPercentValue02() {
int[] arr = new int[]{
2, 2, 2};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + " percent = " + PercentUtils.getPercentValue(arr, i, 2));
}
}
}
2 percent = 22.22
3 percent = 33.33
4 percent = 44.45
2 percent = 33.34
2 percent = 33.33
2 percent = 33.33
版权声明
本文为[轻松的小希]所创,转载请带上原文链接,感谢
https://caochenlei.blog.csdn.net/article/details/124319584
边栏推荐
- 5-minute NLP: text to text transfer transformer (T5) unified text to text task model
- JIRA screenshot
- VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
- 04 Lua 运算符
- Install MySQL on MAC
- 博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
- Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago
- Force buckle-746 Climb stairs with minimum cost
- Gartner announces emerging technology research: insight into the meta universe
- 基于GPU实例的Nanopore数据预处理
猜你喜欢

The font of the soft cell changes color

安装及管理程序

Solution of garbled code on idea console

人脸识别框架之dlib

299. Number guessing game

VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file

How to choose the wireless gooseneck anchor microphone and handheld microphone scheme

NVIDIA graphics card driver error

Gartner 发布新兴技术研究:深入洞悉元宇宙

Real time operation of vim editor
随机推荐
Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
True math problems in 1959 college entrance examination
批量制造测试数据的思路,附源码
Esxi encapsulated network card driver
力扣-746.使用最小花费爬楼梯
Use itextpdf to intercept the page to page of PDF document and divide it into pieces
Sort by character occurrence frequency 451
漫画:什么是IaaS、PaaS、SaaS?
Install MySQL on MAC
PyTorch:train模式与eval模式的那些坑
How magical is the unsafe class used by all major frameworks?
Oak-d raspberry pie cloud project [with detailed code]
Nacos 详解,有点东西
On the security of key passing and digital signature
人脸识别框架之dlib
磁盘管理与文件系统
各大框架都在使用的Unsafe类,到底有多神奇?
The font of the soft cell changes color
建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细
Cartoon: what are IAAs, PAAS, SaaS?