当前位置:网站首页>计算饼状图百分比
计算饼状图百分比
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
边栏推荐
- Questions about disaster recovery? Click here
- 深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
- The solution of not displaying a whole line when the total value needs to be set to 0 in sail software
- English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
- File system read and write performance test practice
- 299. Number guessing game
- 05 Lua 控制结构
- Nacos detailed explanation, something
- LVM与磁盘配额
- Dlib of face recognition framework
猜你喜欢
【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
Deepinv20 installation MariaDB
【Pygame小游戏】10年前风靡全球的手游《愤怒的小鸟》,是如何霸榜的?经典回归......
Hypermotion cloud migration helped China Unicom. Qingyun completed the cloud project of a central enterprise and accelerated the cloud process of the group's core business system
Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin
漫画:什么是IaaS、PaaS、SaaS?
Review 2021: how to help customers clear the obstacles in the last mile of going to the cloud?
深度学习100例 | 第41天-卷积神经网络(CNN):UrbanSound8K音频分类(语音识别)
详解牛客----手套
力扣-746.使用最小花费爬楼梯
随机推荐
JSP learning 3
VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
安装及管理程序
最详细的背包问题!!!
建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细
The system research problem that has plagued for many years has automatic collection tools, which are open source and free
力扣-746.使用最小花费爬楼梯
Gartner 發布新興技術研究:深入洞悉元宇宙
最詳細的背包問題!!!
ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
Public variables of robotframework
Cartoon: what are IAAs, PAAS, SaaS?
Disk management and file system
There is a problem with the light switch from 1 to 100
第九天 static 抽象类 接口
299. Number guessing game
Force buckle - 198 raid homes and plunder houses
UWA Pipeline 功能详解|可视化配置自动测试
DanceNN:字节自研千亿级规模文件元数据存储系统概述