Largest remainder method Java impl

Source: Internet
Author: User
/**
* Maximum remainder allocationAlgorithm
* @ 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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.