Advertisement
petur_stoqnov

Java

Mar 26th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.LinkedHashMap;
  3. import java.util.LinkedList;
  4. import java.util.Scanner;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class Race {
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11. String[] participantsArray = sc.nextLine().split(", ");
  12. String participants = "";
  13. LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>();
  14. for (int i = 0; i < participantsArray.length; i++) {
  15. //participants += participantsArray[i] + " ";
  16. linkedHashMap.putIfAbsent(participantsArray[i], 0);
  17. }
  18.  
  19. String command;
  20. while (!"end of race".equals(command = sc.nextLine())) {
  21. StringBuilder name = new StringBuilder();
  22. int distance = 0;
  23. Pattern namePattern = Pattern.compile("[A-Za-z]");
  24. Pattern digitPattern = Pattern.compile("(?<digit>\\d)");
  25.  
  26. Matcher nameMatcher = namePattern.matcher(command);
  27. Matcher digitMatcher = digitPattern.matcher(command);
  28. while (nameMatcher.find()) {
  29. name.append(nameMatcher.group());
  30. }
  31. while (digitMatcher.find()) {
  32. distance += Integer.parseInt(digitMatcher.group());
  33. }
  34. /* if (participants.contains(name)) {
  35. linkedHashMap.putIfAbsent(String.valueOf(name), distance);
  36. }
  37. */
  38. if (linkedHashMap.containsKey(name.toString())) {
  39. int oldDistance = linkedHashMap.get(name.toString());
  40. distance += oldDistance;
  41. linkedHashMap.put(name.toString(), distance);
  42. }
  43.  
  44. }
  45. int[] counter = {1};
  46. linkedHashMap.entrySet().
  47. stream()
  48. .sorted((e1, e2) -> e2.getValue() - e1.getValue())
  49. .limit(3)
  50. .forEach(e -> {
  51. if (counter[0] == 1) {
  52. System.out.printf("1st place: %s%n", e.getKey());
  53. } else if (counter[0] == 2) {
  54. System.out.printf("2nd place: %s%n", e.getKey());
  55. } else {
  56. System.out.printf("3rd place: %s%n", e.getKey());
  57. }
  58. counter[0] = counter[0] + 1;
  59. });
  60.  
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement