Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class milkmeasure {
- static class cow{
- String name;
- int milk;
- int day;
- int difference;
- }
- // each input contains this
- static class comp implements Comparator <cow>{
- public int compare(cow c1, cow c2) {
- if(c1.day < c2.day) {
- return -1;
- }
- else {
- return 1;
- }
- }
- }
- //sort by days in incremeneting order
- public static void main(String[] args) throws IOException{
- Scanner scan = new Scanner(new File("measurement.in"));
- PrintWriter fout = new PrintWriter(new File("measurement.out"));
- int n = scan.nextInt();
- int counter = 0;
- cow arr[] = new cow[n];
- for(int i = 0; i < n; i++) {
- arr[i] = new cow();
- arr[i].day = scan.nextInt();
- arr[i].name = scan.next();
- arr[i].difference = scan.nextInt();
- }
- //input
- Arrays.sort(arr, new comp());
- //sort cows by days added
- cow names[] = new cow[3];
- for(int i= 0 ;i < 3; i++) {
- names[i] = new cow();
- names[i].milk = 0;
- }
- names[0].name = "Bessie";
- names[1].name = "Mildred";
- names[2].name = "Elsie";
- // I use the cow class to just make it for Bessie, Mildred, and Elsie.
- // This way, we can store it easily
- String previousWinner = "";
- for(int i = 0; i < n; i++) {
- String winner = "";
- if(arr[i].name.equals("Bessie")) {
- names[0].milk += arr[i].difference;
- }
- // add all everything from input into each cows' profile
- else if(arr[i].name.equals("Mildred")){
- names[1].milk += arr[i].difference;
- }
- else {
- names[2].milk += arr[i].difference;
- }
- Arrays.sort(names, new comp());
- if(names[0].milk > names[1].milk && names[0].milk > names[2].milk) {
- winner = "Bessie";
- // Bessie wins
- }
- else if(names[1].milk > names[0].milk && names[1].milk > names[2].milk) {
- winner = "Mildred";
- //Mildred wins
- }
- else if(names[2].milk > names[0].milk && names[2].milk > names[1].milk) {
- winner = "Elsie";
- //Elsie wins
- }
- else if(names[0].milk == names[1].milk && names[0].milk > names[2].milk) {
- winner = "Bessie Mildred";
- //Bessie and Mildred tied for win
- }
- else if(names[0].milk == names[2].milk && names[0].milk > names[1].milk) {
- winner = "Bessie Elsie";
- //Bessie and Elsie are tied for win
- }
- else if(names[1].milk == names[2].milk && names[2].milk > names[0].milk) {
- winner = "Mildred Elsie";
- //Mildred and Elsie are tied for win
- }
- else {
- winner = "Bessie Mildred Elsie";
- // they are all tied for the win
- }
- if(!winner.equals(previousWinner)) {
- counter++;
- // if the previous winner isn't equal to the winner, the scoreboard changes
- }
- previousWinner = winner;
- //replace previousWinner with the winner;
- }
- fout.println(counter);
- //output.
- fout.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment