惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
T
Tailwind CSS Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
PCI Perspectives
PCI Perspectives
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
TaoSecurity Blog
TaoSecurity Blog
云风的 BLOG
云风的 BLOG
C
Cybersecurity and Infrastructure Security Agency CISA
O
OpenAI News
Recorded Future
Recorded Future
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
量子位
博客园 - 叶小钗
V
Vulnerabilities – Threatpost
F
Full Disclosure
Recent Announcements
Recent Announcements
Vercel News
Vercel News
S
Schneier on Security
H
Heimdal Security Blog
Cisco Talos Blog
Cisco Talos Blog
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
B
Blog RSS Feed
宝玉的分享
宝玉的分享
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
爱范儿
爱范儿
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
N
Netflix TechBlog - Medium
S
Security @ Cisco Blogs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyberwarzone
Cyberwarzone

博客园 - xhan

提交给mysql java驱动的优化下个版本要发布了^_^ java 可伸缩阻塞队列实现 java mysql大数据量批量插入与流式读取分析 innodb next-key lock引发的死锁 jremoting的功能扩展点 java开源项目jremoting 数据库单元测试 元数据编程实战_使用Emit运行时生成Protobuf编码类 发布个c#版的HandlerSocket客户端类库 redis入门系列文章广告贴 九 redis学习笔记之虚拟内存 八 redis学习笔记之主从复制 七 redis学习笔记之持久化 六 redis学习笔记之发布订阅 五 redis学习笔记之pipeline 四 redis学习笔记之事务 三 redis学习笔记之排序 二 redis学习笔记之数据类型 - xhan - 博客园 一 redis学习笔记之环境搭建
largest remainder method java impl
xhan · 2012-01-23 · via 博客园 - xhan

/**
 * 最大余数分摊算法
 * 
@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;
    }
}