package com.example.demo.tempTest;
import java.util.ArrayList;
import java.util.List;
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;
}
double digits = Math.pow(10, 2);
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]);
}
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;
}
}
++seats[maxId];
remainder[maxId] = 0;
++currentSum;
}
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));
}
}