Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class _10_Race {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Integer> map = new LinkedHashMap<>();
- String[] input = scanner.nextLine().split(", ");
- for (int i = 0; i < input.length; i++) {
- map.putIfAbsent(input[i], 0);
- }
- String line = "";
- while (!"end of race".equals(line = scanner.nextLine())) {
- int result = 0;
- String name = "";
- Pattern pLetters = Pattern.compile("[A-Za-z]+");
- Matcher mLetters = pLetters.matcher(line);
- while (mLetters.find()) {
- name += mLetters.group();
- }
- Pattern pDigit = Pattern.compile("\\d");
- Matcher mDigit = pDigit.matcher(line);
- while (mDigit.find()) {
- result += Integer.parseInt(mDigit.group());
- }
- if (map.containsKey(name)) {
- map.put(name, map.get(name) + result);
- }
- }
- List<String> names = new ArrayList<>();
- map.entrySet()
- .stream()
- .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()).limit(3)
- .forEach(p -> names.add(p.getKey()));
- System.out.printf("1st place: %s%n2nd place: %s%n3rd place: %s%n"
- , names.get(0), names.get(1), names.get(2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement