Ocena użytkownikóww: ***** / 1
Nadesłany przez Tomasz Lubiński, 22 kwietnia 2006 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.
Pobierz pełne rozwiązanie.Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
hare_niemeyer - Delphi/hare_niemeyer_d.dpr:
program hare_niemeyer_d;
{$APPTYPE CONSOLE}
uses
SysUtils;
//
// Algorytm Hare-Niemeyer'a
//
// www.algortym.org
// (c)2006 Tomasz Lubinski
//
var
mandates : Array [1..100] of Integer;
votes : Array [1..100] of Integer;
calcTab : Array [1..100] of Real;
max : Real;
groupCount, mandatesCount, maxInd, mandatesCounter, votesCount, i : Integer;
begin
//get all data
writeln('Podaj liczbe partii (max.100)');
readln(groupCount);
writeln('Podaj liczbe mandatow');
readln(mandatesCount);
for i:=1 to groupCount do
begin
writeln('Podaj liczbe glosow na partie ' + IntToStr(i));
readln(votes[i]);
end;
//calculate number of total votes
votesCount := 0;
for i:=1 to groupCount do
votesCount := votesCount + votes[i];
//initial number of mandates (Hare-Niemeyer)
mandatesCounter := 0;
for i:=1 to groupCount do
begin
calcTab[i] := (votes[i]*mandatesCount*1.0) / votesCount;
mandates[i] := Trunc(calcTab[i]);
calcTab[i] := calcTab[i] - mandates[i];
mandatesCounter := mandatesCounter + mandates[i];
end;
//calculate rest of mandates
while (mandatesCounter < mandatesCount) do
begin
max := -1;
maxInd := 1;
for i:=1 to groupCount do
begin
if (max<calcTab[i]) then
begin
max := calcTab[i];
maxInd := i;
end;
end;
//give mandate for party with max
mandates[maxInd] := mandates[maxInd] + 1;
mandatesCounter := mandatesCounter + 1;
calcTab[maxInd] := 0;
end;
//present results
for i:=1 to groupCount do
begin
writeln('Partia ' + IntToStr(i) + ' ma ' + IntToStr(mandates[i]) + ' mandatow');
end;
readln;
end.