import java.util.Scanner; import java.io.File; import java.io.PrintStream; import java.io.FileNotFoundException; /** * Solution to Skins Game problem. * @author Dave Reed */ public class Skins { public static final int NUM_PLAYERS = 4; public static final int NUM_HOLES = 18; public static void main(String[] args) throws FileNotFoundException { Scanner infile = new Scanner(new File("input3.txt")); PrintStream outfile = new PrintStream("output3.txt"); int[][] scores = new int[NUM_HOLES+1][NUM_PLAYERS+1]; int numGames = infile.nextInt(); for (int game = 1; game <= numGames; game++) { for (int player = 1; player <= NUM_PLAYERS; player++) { for (int hole = 1; hole <= NUM_HOLES; hole++) { scores[hole][player] = infile.nextInt(); } } Skins.processGame(outfile, game, scores); } } public static void processGame(PrintStream outfile, int gameNum, int[][] scores) throws FileNotFoundException { int[] skins = new int[NUM_PLAYERS+1]; for (int i = 1; i <= NUM_PLAYERS; i++) { skins[i] = 0; } int skinCount = 1; for (int h = 1; h <= NUM_HOLES; h++) { int winner = Skins.findWinner(scores[h]); if (winner == 0) { skinCount++; } else { skins[winner] += skinCount; skinCount = 1; } } outfile.println("DATA SET " + gameNum); for (int p = 1; p <= NUM_PLAYERS; p++) { outfile.print("PLAYER " + p + ": " + skins[p]); if (skins[p] == 1) { outfile.println(" SKIN"); } else { outfile.println(" SKINS"); } } if (skinCount > 1) { outfile.print("UNCLAIMED: " + (skinCount-1)); if (skinCount-1 == 1) { outfile.println(" SKIN"); } else { outfile.println(" SKINS"); } } } public static int findWinner(int[] scores) { int low = 1; boolean dupe = false; for (int i = 2; i <= 4; i++) { if (scores[i] < scores[low]) { low = i; dupe = false; } else if (scores[i] == scores[low]) { dupe = true; } } if (dupe) { return 0; } else { return low; } } }