User Tools

Site Tools


contest_australian_voting_skeleton
/*
 * 110108 Australian Voting
 * 
 * Author: rushed for time teammate
 * Date: 8/30/2011
 * 
 */

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;


class Main {
    public static void main(String args[]) throws FileNotFoundException {
	doMain(System.in, System.out);
    }

    static void doMain(InputStream in, PrintStream out) {
	new Solution().main(in, out);
    }
}


class Solution {
    void main(InputStream in, PrintStream out) {
	Scanner scanner = new Scanner(in);

	// Read number of cases (elections) to be handled
	int cases = scanner.nextInt();
	scanner.nextLine();
	scanner.nextLine();

	// Handle each election
	for (int i = 0; i < cases; i++) {

	    // Read number of candidates and create Election object
	    int c = scanner.nextInt();
	    scanner.nextLine();
	    Election election = new Election(c);

	    // Read names of candidates
	    for (int j = 0; j < c; j++)
		election.setCandidate(j, scanner.nextLine());

	    // Read ballots; terminate with empty ballot or end of file
	    String sBallot = scanner.nextLine();
	    int cBallots = 0;

	    while (sBallot.length() > 0) {
		Scanner scan = new Scanner(sBallot);
		for (int rank = 0; rank < c; rank++)
		    election.setBallot(cBallots, rank, scan.nextInt() - 1);
		cBallots++;
		if (scanner.hasNextLine())
		    sBallot = scanner.nextLine();
		else
		    sBallot = "";
	    }

	    // Compute the winner(s) and print the results
	    String[] winners = election.computeWinners();

	    for (int j = 0; j < c; j++)
		if (winners[j].length() > 0)
		    out.format("%s\n", winners[j]);

	    // Separate each election result with a blank line, but no line at
	    // the end
	    if (i + 1 < cases)
		out.format("\n");
	}
    }
}


class Election {
    private int nCandidates;
    private String candidates[];
    private int nBallots;
    private int ballots[][]; // ballots[ballot_number][rank] -> candidate at
			     // that rank on that ballot

    Election(int nCandidates) {
	this.nCandidates = nCandidates;
	candidates = new String[nCandidates];
	ballots = new int[1000][nCandidates];
	nBallots = 0;
    }

    // TODO: Fill in details.  Sorry, gotta run!
}
contest_australian_voting_skeleton.txt · Last modified: 2011/08/28 11:06 by jtkorb