Guest User

Judge

a guest
Apr 1st, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. package com.asen.test;
  2.  
  3. import java.util.*;
  4.  
  5. public class Judge {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         Map<String, Map<String, Integer>> judge = new LinkedHashMap<>();
  9.         String command = scanner.nextLine();
  10.         while (!"no more time".equals(command)) {
  11.             String[] split = command.split(" -> ");
  12.             String name = split[0];
  13.             String course = split[1];
  14.             int points = Integer.parseInt(split[2].trim());
  15.             Map<String, Integer> currentMap = judge.get(course);
  16.             if (currentMap == null) {
  17.                 currentMap = new LinkedHashMap<>();
  18.             }
  19.             currentMap.putIfAbsent(name, 0);
  20.             if (points > currentMap.get(name)) {
  21.                 currentMap.put(name, points);
  22.             }
  23.             judge.put(course, currentMap);
  24.             command = scanner.nextLine();
  25.         }
  26.         int[] number = {1};
  27.         judge.entrySet().
  28.                 stream().
  29.                 sorted((a1, a2) -> {
  30.                     int sec = a2.getValue().size();
  31.                     int first = a1.getValue().size();
  32.                     if (first == sec) {
  33.                         return a1.getKey().compareTo(a2.getKey());
  34.                     } else {
  35.                         return Integer.compare(sec, first);
  36.                     }
  37.                 }).forEach(e -> {
  38.             System.out.println((e.getKey() + ": " + e.getValue().size() + " participants"));
  39.             e.getValue().entrySet().stream().sorted((s1, s2) -> {
  40.                 int compare = s2.getValue() - (s1.getValue());
  41.                 if (compare == 0) {
  42.                     return s1.getKey().compareTo(s2.getKey());
  43.                 } else {
  44.                     return compare;
  45.                 }
  46.             }).forEach(x -> {
  47.  
  48.                 System.out.println(String.format("%s. %s <::> %s", number[0], x.getKey(), x.getValue()));
  49.                 number[0]++;
  50.                 if (number[0] > e.getValue().size()) {
  51.                     number[0] = 1;
  52.                 }
  53.             });
  54.         });
  55.         System.out.println("Individual standings:");
  56.         Map<String, Integer> finalResults = new LinkedHashMap<>();
  57.         for (Map<String, Integer> map : judge.values()) {
  58.             for (Map.Entry<String, Integer> entry : map.entrySet()) {
  59.                 String name = entry.getKey();
  60.                 int points = entry.getValue();
  61.                 finalResults.putIfAbsent(name, 0);
  62.                 finalResults.put(name, points + finalResults.get(name));
  63.             }
  64.         }
  65.         int[] ranking = {1};
  66.         finalResults.entrySet().stream().sorted((a1, a2) -> {
  67.             int compare = a2.getValue() - a1.getValue();
  68.             if (compare == 0) {
  69.                 return a1.getKey().compareTo(a2.getKey());
  70.             } else {
  71.                 return compare;
  72.             }
  73.         }).forEach(e -> {
  74.                     System.out.println(String.format("%s. %s -> %s", ranking[0], e.getKey(), e.getValue()));
  75.                     ranking[0]++;
  76.                 }
  77.         );
  78.  
  79.  
  80.     }
  81. }
Add Comment
Please, Sign In to add comment