Advertisement
meteor4o

JF-Maps-MoreExercise-02.Judge

Jul 18th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.HashMap;
  4. import java.util.LinkedHashMap;
  5. import java.util.Scanner;
  6.  
  7. public class Judge {
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner(System.in);
  10.  
  11.         String input = sc.nextLine();
  12.         HashMap<String, LinkedHashMap<String, Integer>> contests = new LinkedHashMap<>();
  13.         HashMap<String, Integer> users = new LinkedHashMap<>();
  14.  
  15.         while (!input.toLowerCase().equals("no more time")) {
  16.  
  17.             String[] tokens = input.split(" -> ");
  18.             String username = tokens[0];
  19.             String contest = tokens[1];
  20.             Integer points = Integer.parseInt(tokens[2]);
  21.  
  22.             if (contests.containsKey(contest)) {
  23.                 if (contests.get(contest).containsKey(username)) {
  24.                     int currPoints = contests.get(contest).get(username);
  25.                     if (points > currPoints) {
  26.                         contests.get(contest).put(username, points);
  27.                         users.put(username, users.get(username) + points - currPoints);
  28.                     }
  29.  
  30.                 } else {
  31.                     contests.get(contest).put(username, points);
  32.                     if (users.get(username) == null) {
  33.                         users.put(username, 0);
  34.                     }
  35.                     int cPoints = users.get(username);
  36.                     int pointsToAdd = cPoints + points;
  37.                     users.put(username, pointsToAdd);
  38.  
  39.                 }
  40.             } else {
  41.                 contests.put(contest, new LinkedHashMap<>());
  42.                 contests.get(contest).put(username, points);
  43.                 if (users.get(username) == null) {
  44.                     users.put(username, 0);
  45.  
  46.                 }
  47.                 users.put(username, users.get(username) + points);
  48.             }
  49.                 input = sc.nextLine();
  50.         }
  51.  
  52.             var ref = new Object() {
  53.                 int counter = 0;
  54.             };
  55.             contests.entrySet().stream().forEach(a -> {
  56.                 System.out.printf("%s: %d participants%n", a.getKey(), a.getValue().size());
  57.                 a.getValue().entrySet().stream().sorted((f, s) -> {
  58.                     int result = s.getValue().compareTo(f.getValue());
  59.                     if (result == 0) {
  60.                         result = f.getKey().compareTo(s.getKey());
  61.                     }
  62.                     return result;
  63.                 })
  64.                         .forEach(c -> {
  65.                             ref.counter++;
  66.                             System.out.printf("%d. %s <::> %d%n", ref.counter, c.getKey(), c.getValue());
  67.                         });
  68.                 ref.counter = 0;
  69.             });
  70.             System.out.println("Individual standings:");
  71.             users.entrySet().stream().sorted((f, s) -> {
  72.                 int result = s.getValue().compareTo(f.getValue());
  73.                 if (result == 0) {
  74.                     result = f.getKey().compareTo(s.getKey());
  75.                 }
  76.                 return result;
  77.             })
  78.                     .forEach(c -> {
  79.                         ref.counter++;
  80.                         System.out.printf("%d. %s -> %d%n", ref.counter, c.getKey(), c.getValue());
  81.                     });
  82.         }
  83.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement