diff --git a/example/tests/out.txt b/example/tests/out.txt index ea82012..d623820 100644 --- a/example/tests/out.txt +++ b/example/tests/out.txt @@ -1,3 +1,3 @@ Enter the number of vacancies: Enter the number of votes: Enter the number of candidates: Enter the names of the candidates (space-separated): Enter the ballots (space-/line-separated): -Elected candidates: JoseBishop AntonyMorrow JadenWall VanessaPacheco +Elected candidates: JoseBishop JadenWall AntonyMorrow VanessaPacheco diff --git a/src/hare-clark.hpp b/src/hare-clark.hpp index e5c1f0f..3d306da 100644 --- a/src/hare-clark.hpp +++ b/src/hare-clark.hpp @@ -75,7 +75,7 @@ class HareClark { std::vector votes(candidates.size()); // Current value of votes for each candidate for (const auto& [ballot, weight] : ballots_with_weights) votes[ballot.back()] += weight; - while (number_of_vacancies) { + while (number_of_vacancies < candidates.size() - unavailable_candidates.size()) { int winner = -1; int loser = -1; for (int i = 0; i < candidates.size(); ++i) { @@ -97,6 +97,9 @@ class HareClark { votes[loser] = -1; } } + if (number_of_vacancies) { + for (int i = 0; i < candidates.size(); ++i) if (!unavailable_candidates.count(i)) elected_candidates.push_back(candidates[i]); + } return elected_candidates; } };