当前位置:网站首页>Calculate pie chart percentage
Calculate pie chart percentage
2022-04-23 16:40:00 【Relaxed Xiao Xi】
Requirements describe :
Given an array of integers , for example :[2, 3, 4], Calculate the percentage of each element , The required percentage is accumulated as 100%.
input = [2, 3, 4]
output = [22.22, 33.33, 44.45]
Project dependence :
<?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>
Code implementation :
public class PercentUtils {
/** * Calculate the percentage of pie chart * * @param arr * @param idx * @param precision * @return */
public static double getPercentValue(int[] arr, int idx, int precision) {
// Judging index
if (idx < 0 || (arr.length - 1) < idx) {
return 0;
}
// Array sum
double sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
if (sum <= 0) {
return 0;
}
// Calculation accuracy
double digits = Math.pow(10, precision);
// Expansion ratio
double[] votesPerQuota = new double[arr.length];
for (int i = 0; i < arr.length; i++) {
double val = arr[i] / sum * digits * 100;
votesPerQuota[i] = val;
}
// Expanded total
double targetSeats = digits * 100;
// Value down
double[] seats = new double[arr.length];
for (int i = 0; i < votesPerQuota.length; i++) {
seats[i] = Math.floor(votesPerQuota[i]);
}
// Add up again
double currentSum = 0;
for (int i = 0; i < seats.length; i++) {
currentSum += seats[i];
}
// Remainder array
double[] remainder = new double[arr.length];
for (int i = 0; i < seats.length; i++) {
remainder[i] = votesPerQuota[i] - seats[i];
}
// Maximum remainder
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;
}
}
// Add one to the maximum balance
++seats[maxId];
// This remainder has been used
remainder[maxId] = 0;
// Add one to the total
++currentSum;
}
// Return proportion
return seats[idx] / digits;
}
}
Running effect :
@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
版权声明
本文为[Relaxed Xiao Xi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231640089593.html
边栏推荐
- ◰GL-着色器处理程序封装
- MySQL master-slave replication
- Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons
- MySQL master-slave synchronization pit avoidance version tutorial
- 04 Lua 运算符
- 昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
- How magical is the unsafe class used by all major frameworks?
- Execution plan calculation for different time types
- English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
- File system read and write performance test practice
猜你喜欢
Installation and management procedures
JMeter installation tutorial and solutions to the problems I encountered
欣旺达:HEV和BEV超快充拳头产品大规模出货
Use if else to judge in sail software - use the title condition to judge
Deepinv20 installation MariaDB
人脸识别框架之dlib
漫画:什么是IaaS、PaaS、SaaS?
VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
New project of OMNeT learning
Download and install mongodb
随机推荐
MySQL personal learning summary
UWA Pipeline 功能详解|可视化配置自动测试
关于局域网如何组建介绍
Loading order of logback configuration file
JMeter installation tutorial and solutions to the problems I encountered
Hyperbdr cloud disaster recovery v3 Release of version 3.0 | upgrade of disaster recovery function and optimization of resource group management function
Zhongang Mining: Fluorite Flotation Process
The first line and the last two lines are frozen when paging
博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
众昂矿业:萤石浮选工艺
浅谈 NFT项目的价值、破发、收割之争
PHP 零基础入门笔记(13):数组相关函数
聊一聊浏览器缓存控制
Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
Pseudo Distributed installation spark
The most detailed Backpack issues!!!
JIRA screenshot
G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
LVM与磁盘配额