I'm trying to solve the following problem:
Australian ballots require that voters rank all the candidates in order of choice. Initially only the first choices are counted, and if one candidate receives more than 50% of the vote then that candidate is elected. However, if no candidate receives more than 50%, all candidates tied for the lowest number of votes are eliminated. Ballots ranking these candidates first are recounted in favor of their highest-ranked non-eliminated candidate. This process of eliminating the weakest candidates and counting their ballots in favor of the preferred non-eliminated candidate continues until one candidate receives more than 50% of the vote, or until all remaining candidates are tied.
Input
The input begins with a single positive integer on a line by itself indicating the number of cases following, each as described below. This line is followed by a blank line. There is also a blank line between two consecutive inputs. The first line of each case is an integer n ≤ 20 indicating the number of candidates. The next n lines consist of the names of the candidates in order, each up to 80 characters in length and containing any printable characters. Up to 1,000 lines follow, each containing the contents of a ballot. Each ballot contains the numbers from 1 to n in some order. The first number indicates the candidate of first choice; the second number indicates candidate of second choice, and so on.
Output
The output of each test case consists of either a single line containing the name of the winner or several lines containing the names of all candidates who are tied. The output of each two consecutive cases are separated by a blank line.
Sample Input
1
3
John Doe
Jane Smith
Jane Austen
1 2 3
2 1 3
2 3 1
1 2 3
3 1 2Sample Output
John Doe
This is the code I wrote:
/*
* australian-voting.cpp
*/
#include <iostream>
#include <string>
#include <sstream>
void parse_case(int &candidates_count, int &votes_count, char candidates[][80], int votes[][1000]) {
std::cin >> candidates_count;
std::cin.get();
for (int j=0; j<candidates_count; j++) {
std::cin.getline(candidates[j], sizeof(candidates[j]), '\n');
}
votes_count = 0;
std::string line;
while (std::getline(std::cin, line)) {
if (line.empty()) {
break;
}
std::stringstream ss(line);
for (int k=0; k<candidates_count; k++) {
ss >> votes[k][votes_count];
}
votes_count++;
}
}
void reset_losers(bool losers[20]) {
for (int k=0; k<20; k++) {
losers[k] = false;
}
}
void count_votes(int candidates_count, int votes_count, int candidate_votes[20], int votes[][1000]) {
for (int j=0; j<candidates_count; j++) {
candidate_votes[j] = 0;
}
for (int j=0; j<votes_count; j++) {
candidate_votes[(votes[0][j]-1)]++;
}
}
int find_winners(int candidates_count, int remaining_candidates_count, int votes_count, int candidate_votes[20], int subroutine_results[20]) {
int max_votes = 0;
int winners_count = 0;
for (int k=0; k<candidates_count; k++) {
if (candidate_votes[k] > max_votes) {
max_votes = candidate_votes[k];
subroutine_results[0] = k;
winners_count = 1;
} else if (candidate_votes[k] == max_votes) {
subroutine_results[(++winners_count-1)] = k;
}
}
if (winners_count == remaining_candidates_count || ( winners_count == 1 && (double) max_votes / votes_count > 0.5 ) ) {
return winners_count;
}
return 0;
}
int find_losers(int candidates_count, int votes_count, int candidate_votes[20], int subroutine_results[20], bool losers[20]) {
int min_votes = 1001;
int losers_count = 0;
for (int k=0; k<candidates_count; k++) {
if (!losers[k]) {
if (candidate_votes[k] < min_votes) {
min_votes = candidate_votes[k];
subroutine_results[0] = k;
losers_count = 1;
} else if (candidate_votes[k] == min_votes) {
subroutine_results[(++losers_count-1)] = k;
}
}
}
for (int i=0; i<losers_count; i++) {
losers[subroutine_results[i]] = true;
}
return losers_count;
}
void eliminate_loser(int remaining_candidates_count, int votes_count, int votes[][1000], int loser) {
for (int i = 0; i<remaining_candidates_count; i++) {
for (int j = 0; j<votes_count; j++) {
if (votes[i][j] == loser + 1) {
for (int l = i; l<remaining_candidates_count; l++) {
votes[l][j] = votes[l + 1][j];
}
}
}
}
}
int main( int argc, char * argv[] ) {
int cases_count;
int candidates_count;
int remaining_candidates_count;
int votes_count;
char candidates[20][80];
int votes[20][1000];
int candidate_votes[20];
int subroutine_results[20];
bool losers[20];
int winners_count;
int losers_count;
std::cin >> cases_count;
std::cin.get();
std::cin.get();
for (int i=0; i<cases_count; i++) {
parse_case(candidates_count, votes_count, candidates, votes);
reset_losers(losers);
remaining_candidates_count = candidates_count;
winners_count = 0;
while (!winners_count) {
count_votes(candidates_count, votes_count, candidate_votes, votes);
winners_count = find_winners(candidates_count, remaining_candidates_count, votes_count, candidate_votes, subroutine_results);
if (winners_count == 0) {
losers_count = find_losers(candidates_count, votes_count, candidate_votes, subroutine_results, losers);
for (int k = 0; k<losers_count; k++) {
eliminate_loser(remaining_candidates_count, votes_count, votes, subroutine_results[k]);
remaining_candidates_count--;
}
}
}
for (int j=0; j<winners_count; j++) {
std::cout << candidates[subroutine_results[j]] << std::endl;
}
std::cout << std::endl;
}
return 0;
}
The automatic submission to https://uva.onlinejudge.org/ and/or to http://www.programming-challenges.com/ is failing, so there must be a problem.
Trying to reproduce the problem I wrote an input data set generator and I compared my results with the results of a program accepted by the online judge (you can do that at http://www.udebug.com/), but my results are consistent with the expected ones (see the data sets I'm using here).
I suppose there must be some edge case that makes it fail but so far I couldn't find it. Any suggestion is welcome.