Alexander7337

ChampionsLeague

Apr 4th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4. import java.util.*;
  5.  
  6. public class Problem4ChampionsLeague {
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.         TreeMap<String, Integer> totalWins = new TreeMap<>();
  10.         TreeMap<String, TreeSet<String>> opponents = new TreeMap<>();
  11.  
  12.         String line = sc.nextLine();
  13.  
  14.         while (!line.equals("stop")) {
  15.             String[] tokens = line.split("([ ][|\\s]+)");
  16.             String host = tokens[0];
  17.             String guest = tokens[1];
  18.             String homeScore = tokens[2];
  19.             String awayScore = tokens[3];
  20.  
  21.             if (!totalWins.containsKey(host)) {
  22.                 totalWins.put(host, 0);
  23.                 opponents.put(host, new TreeSet<String>());
  24.             }
  25.             if (!totalWins.containsKey(guest)) {
  26.                 totalWins.put(guest, 0);
  27.                 opponents.put(guest, new TreeSet<String>());
  28.             }
  29.  
  30.             totalWins = getWins(host, guest, homeScore, awayScore, totalWins);
  31.             /*totalWins = getWins(guest, host, awayScore, totalWins);*/
  32.             opponents.get(host).add(guest);
  33.             opponents.get(guest).add(host);
  34.  
  35.             line = sc.nextLine();
  36.         }
  37.  
  38.         SortedSet<Map.Entry<String, Integer>> output = entriesSortedByValues(totalWins);
  39.  
  40.         for (Map.Entry<String, Integer> pair : output) {
  41.             System.out.println(pair.getKey());
  42.             System.out.println("- Wins: " + pair.getValue());
  43.             if (opponents.containsKey(pair.getKey())) {
  44.                 List<String> o = new ArrayList<>(opponents.get(pair.getKey()));
  45.                 System.out.print("- Opponents: ");
  46.                 for (int i = 0; i < o.size(); i++) {
  47.                     if (i < o.size()-1) {
  48.                         System.out.print(o.get(i) + ", ");
  49.                     } else {
  50.                         System.out.println(o.get(i));
  51.                     }
  52.                 }
  53. /*                System.out.println("Opponents: " + opponents.get(pair.getKey()));*/
  54.             }
  55.         }
  56.     }
  57.     public static TreeMap<String, Integer> getWins(String teamOne, String teamTwo, String score, String score2, TreeMap<String, Integer> totalWins){
  58.         TreeMap<String, Integer> trm = totalWins;
  59.         int hostGoals = score.charAt(0);
  60.         hostGoals += score2.charAt(2);
  61.         int guestGoals = score.charAt(2);
  62.         guestGoals += score2.charAt(0);
  63.         if (hostGoals > guestGoals){
  64.             int wins = trm.get(teamOne) + 1;
  65.             trm.put(teamOne, wins);
  66.         } else if (hostGoals < guestGoals) {
  67.             int wins = trm.get(teamTwo) + 1;
  68.             trm.put(teamTwo, wins);
  69.         } else if (score.charAt(2) > score2.charAt(2)) {
  70.             int wins = trm.get(teamTwo) + 1;
  71.             trm.put(teamTwo, wins);
  72.         } else if (score.charAt(0) < score2.charAt(0)){
  73.             int wins = trm.get(teamOne) + 1;
  74.             trm.put(teamOne, wins);
  75.         }
  76.         return trm;
  77.     }
  78.     static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(TreeMap<K,V> map) {
  79.         SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
  80.                 new Comparator<Map.Entry<K,V>>() {
  81.                     @Override
  82.                     public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
  83.                         int res = e2.getValue().compareTo(e1.getValue());
  84.                         return res != 0 ? res : 1;
  85.                     }
  86.                 }
  87.         );
  88.         sortedEntries.addAll(map.entrySet());
  89.         return sortedEntries;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment