Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Test {
  8.  
  9. public static void main(String[] args) throws IOException {
  10. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.  
  12. // List works but Set has faster search
  13. Set<String> participants = Arrays.stream(reader.readLine().split(", ")).collect(Collectors.toSet());
  14. Map<String, Integer> winners = new LinkedHashMap<>();
  15.  
  16. String input = "";
  17. while (!"end of race".equals(input = reader.readLine())) {
  18. String name = "";
  19. int distance = 0;
  20. for (char ch : input.toCharArray()) {
  21. if (Character.isLetter(ch)) {
  22. name += ch;
  23. } else if (Character.isDigit(ch)) {
  24. distance += ch - '0';
  25. }
  26. }
  27.  
  28. if (participants.contains(name)) {
  29. winners.putIfAbsent(name, 0);
  30. winners.put(name, winners.get(name) + distance);
  31. }
  32. }
  33.  
  34. List<String> sortedNames = winners.keySet().stream()
  35. .sorted(Comparator.comparing(winners::get, Comparator.reverseOrder()))
  36. .collect(Collectors.toList());
  37.  
  38. System.out.println("1st place: " + sortedNames.get(0));
  39. System.out.println("2nd place: " + sortedNames.get(1));
  40. System.out.println("3rd place: " + sortedNames.get(2));
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement