Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package P04;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class Snowwhite2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = "";
- LinkedHashMap<String, Integer> dwarfs = new LinkedHashMap<>();
- while (true) {
- input = scanner.nextLine();
- if (input.equals("Once upon a time")) {
- break;
- }
- String[] info = input.split(" <:> ");
- String name = info[0];
- String color = info[1];
- int physics = Integer.parseInt(info[2]);
- String id = name + ":" + color;
- if (!dwarfs.containsKey(id)) {
- dwarfs.put(id, physics);
- } else {
- int temp = Math.max(dwarfs.get(id), physics);
- dwarfs.put(id, temp);
- }
- }
- dwarfs.entrySet().stream().sorted((x1, x2) -> {
- int result = Integer.compare(x2.getValue(), x1.getValue());
- if (result == 0) {
- String[] id1 = x1.getKey().split(":");
- String color1 = id1[1];
- String[] id2 = x2.getKey().split(":");
- String color2 = id2[1];
- if (!color1.equals(color2)) {
- int occurrences1 = 0;
- int occurrences2 = 0;
- for (Map.Entry<String, Integer> dwarf : dwarfs.entrySet()) {
- if (dwarf.getKey().contains(color1)) {
- occurrences1++;
- } else if (dwarf.getKey().contains(color2)) {
- occurrences2++;
- }
- }
- result = Integer.compare(occurrences2, occurrences1);
- }
- }
- return result;
- }).forEach(dwarf -> {
- String[] id = dwarf.getKey().split(":");
- String name = id[0];
- String color = id[1];
- System.out.printf("(%s) %s <-> %d%n", color, name, dwarf.getValue());
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment