zoukankan      html  css  js  c++  java
  • largest remainder method java impl

    /**
     * 最大余数分摊算法
     * 
    @author xhan
     *{
    @link=http://en.wikipedia.org/wiki/Largest_remainder_method}
     
    */
    public class ShareCalculator {

        public static  double[] calculate(double[] votes , double totalSeats) {
            double[] seats = new double[votes.length];
            double[] reminders = new double[votes.length];
            
            double totalVotes = 0;
            for (double vote : votes) {
                totalVotes += vote;
            }
            
            double hareQuota = totalVotes / totalSeats ;
            double allocatedSeats = 0;
            
            for(int i = 0; i < votes.length ;i++) {
                double voteDivHareQuota = votes[i] / hareQuota;
                seats[i] = Math.floor(voteDivHareQuota);
                reminders[i] = voteDivHareQuota - seats[i];
                allocatedSeats += seats[i];
            }

            double leftSeats = totalSeats - allocatedSeats;
            
            //allocate left seats to party with largest reminder 
            for (int i = 0; i < leftSeats; i++) {
                double max = 0;
                int maxIndex = 0;
                for (int j = 0; j < reminders.length; j++) {
                    if(reminders[j] > max) {
                        max = reminders[j];
                        maxIndex = j;
                    }
                }
                seats[maxIndex] += 1;
                reminders[maxIndex] = 0;
            }
            
            return seats;
        }
    }
  • 相关阅读:
    如何在eclipse开发环境中连接数据库?oracle和db2
    oracle数据库表的记录误删后如何恢复
    人事经理眼中的好简历
    JavaScript常用方法
    如何解决plsql被用户锁定的问题?
    eclipse 快捷键
    刚开始用9.3的版本,一堆问题。(转)
    十条不错的编程观点(转)
    http://localhost:8099无法登录,出现incorrect info报错信息,怎么办?
    http://localhost:8399/arcgis/rest/services 无法访问,怎么办?
  • 原文地址:https://www.cnblogs.com/xhan/p/2328955.html
Copyright © 2011-2022 走看看