kalinikov

04. International SoftUniada

Dec 6th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class InternationalSoftUniada {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. String input = scanner.nextLine();
  8.  
  9. Map<String, List<String>> countries = new LinkedHashMap<>();
  10. Map<String, Integer> contestants = new LinkedHashMap<>();
  11. Map<String, Integer> countriesPoints = new LinkedHashMap<>();
  12.  
  13.  
  14. while (!input.equals("END")) {
  15. String[] tokens = input.split(" -> ");
  16. String country = tokens[0];
  17. String contestant = tokens[1];
  18. int points = Integer.parseInt(tokens[2]);
  19.  
  20. if (!countries.containsKey(country)) {
  21. countries.put(country, new ArrayList<>());
  22. countries.get(country).add(contestant);
  23. contestants.put(contestant, points);
  24. countriesPoints.put(country, points);
  25. } else {
  26. if (!countries.get(country).contains(contestant)) {
  27. countries.get(country).add(contestant);
  28. contestants.put(contestant, points);
  29. } else {
  30. contestants.put(contestant, contestants.get(contestant) + points);
  31. }
  32. countriesPoints.put(country, countriesPoints.get(country) + points);
  33. }
  34.  
  35. input = scanner.nextLine();
  36. }
  37.  
  38. countriesPoints.entrySet()
  39. .stream()
  40. .sorted((f, s) -> {
  41. return s.getValue().compareTo(f.getValue());
  42. })
  43. .forEach(e -> {
  44. System.out.printf("%s: %d%n", e.getKey(), e.getValue());
  45. for (String name : countries.get(e.getKey())) {
  46. System.out.printf("-- %s -> %d%n"
  47. , name, contestants.get(name));
  48. }
  49.  
  50. });
  51.  
  52.  
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment