Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- import java.util.*;
- public class Problem4ChampionsLeague {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- TreeMap<String, Integer> totalWins = new TreeMap<>();
- TreeMap<String, TreeSet<String>> opponents = new TreeMap<>();
- String line = sc.nextLine();
- while (!line.equals("stop")) {
- String[] tokens = line.split("([ ][|\\s]+)");
- String host = tokens[0];
- String guest = tokens[1];
- String homeScore = tokens[2];
- String awayScore = tokens[3];
- if (!totalWins.containsKey(host)) {
- totalWins.put(host, 0);
- opponents.put(host, new TreeSet<String>());
- }
- if (!totalWins.containsKey(guest)) {
- totalWins.put(guest, 0);
- opponents.put(guest, new TreeSet<String>());
- }
- totalWins = getWins(host, guest, homeScore, awayScore, totalWins);
- /*totalWins = getWins(guest, host, awayScore, totalWins);*/
- opponents.get(host).add(guest);
- opponents.get(guest).add(host);
- line = sc.nextLine();
- }
- SortedSet<Map.Entry<String, Integer>> output = entriesSortedByValues(totalWins);
- for (Map.Entry<String, Integer> pair : output) {
- System.out.println(pair.getKey());
- System.out.println("- Wins: " + pair.getValue());
- if (opponents.containsKey(pair.getKey())) {
- List<String> o = new ArrayList<>(opponents.get(pair.getKey()));
- System.out.print("- Opponents: ");
- for (int i = 0; i < o.size(); i++) {
- if (i < o.size()-1) {
- System.out.print(o.get(i) + ", ");
- } else {
- System.out.println(o.get(i));
- }
- }
- /* System.out.println("Opponents: " + opponents.get(pair.getKey()));*/
- }
- }
- }
- public static TreeMap<String, Integer> getWins(String teamOne, String teamTwo, String score, String score2, TreeMap<String, Integer> totalWins){
- TreeMap<String, Integer> trm = totalWins;
- int hostGoals = score.charAt(0);
- hostGoals += score2.charAt(2);
- int guestGoals = score.charAt(2);
- guestGoals += score2.charAt(0);
- if (hostGoals > guestGoals){
- int wins = trm.get(teamOne) + 1;
- trm.put(teamOne, wins);
- } else if (hostGoals < guestGoals) {
- int wins = trm.get(teamTwo) + 1;
- trm.put(teamTwo, wins);
- } else if (score.charAt(2) > score2.charAt(2)) {
- int wins = trm.get(teamTwo) + 1;
- trm.put(teamTwo, wins);
- } else if (score.charAt(0) < score2.charAt(0)){
- int wins = trm.get(teamOne) + 1;
- trm.put(teamOne, wins);
- }
- return trm;
- }
- static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(TreeMap<K,V> map) {
- SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
- new Comparator<Map.Entry<K,V>>() {
- @Override
- public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
- int res = e2.getValue().compareTo(e1.getValue());
- return res != 0 ? res : 1;
- }
- }
- );
- sortedEntries.addAll(map.entrySet());
- return sortedEntries;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment