当前位置:网站首页>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
边栏推荐
- Differences between MySQL BTREE index and hash index
- On the value, breaking and harvest of NFT project
- G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
- File system read and write performance test practice
- LVM与磁盘配额
- Gartner 發布新興技術研究:深入洞悉元宇宙
- 面试百分百问到的进程,你究竟了解多少
- Cartoon: what are IAAs, PAAS, SaaS?
- Loggie source code analysis source file module backbone analysis
- JSP learning 3
猜你喜欢
Use itextpdf to intercept the page to page of PDF document and divide it into pieces
Dlib of face recognition framework
[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
Pycham connects to the remote server and realizes remote debugging
You need to know about cloud disaster recovery
Questions about disaster recovery? Click here
How to choose the wireless gooseneck anchor microphone and handheld microphone scheme
Gartner publie une étude sur les nouvelles technologies: un aperçu du métacosme
面试百分百问到的进程,你究竟了解多少
Project framework of robot framework
随机推荐
Government cloud migration practice: Beiming digital division used hypermotion cloud migration products to implement the cloud migration project for a government unit, and completed the migration of n
Detailed explanation of UWA pipeline function | visual configuration automatic test
Installation and management procedures
Creation of RAID disk array and RAID5
Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme
Pytorch: the pit between train mode and eval mode
文件系统读写性能测试实战
面试百分百问到的进程,你究竟了解多少
Use case execution of robot framework
MySQL master-slave synchronization pit avoidance version tutorial
Query the data from 2013 to 2021, and only query the data from 2020. The solution to this problem is carried out
How to build tiktok user trust and drive fan growth
Detailed explanation of gzip and gunzip decompression parameters
Flask如何在内存中缓存数据?
Nacos 详解,有点东西
Custom implementation of Baidu image recognition (instead of aipocr)
VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
Postman batch production body information (realize batch modification of data)
Force buckle - 198 raid homes and plunder houses
Summary according to classification in sail software