luoni

Snowwhite100/100

Jun 23rd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. package P04;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Snowwhite2 {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. String input = "";
  12. LinkedHashMap<String, Integer> dwarfs = new LinkedHashMap<>();
  13.  
  14. while (true) {
  15. input = scanner.nextLine();
  16. if (input.equals("Once upon a time")) {
  17. break;
  18. }
  19.  
  20. String[] info = input.split(" <:> ");
  21. String name = info[0];
  22. String color = info[1];
  23. int physics = Integer.parseInt(info[2]);
  24.  
  25. String id = name + ":" + color;
  26. if (!dwarfs.containsKey(id)) {
  27. dwarfs.put(id, physics);
  28. } else {
  29. int temp = Math.max(dwarfs.get(id), physics);
  30. dwarfs.put(id, temp);
  31. }
  32. }
  33. dwarfs.entrySet().stream().sorted((x1, x2) -> {
  34. int result = Integer.compare(x2.getValue(), x1.getValue());
  35.  
  36. if (result == 0) {
  37.  
  38. String[] id1 = x1.getKey().split(":");
  39. String color1 = id1[1];
  40. String[] id2 = x2.getKey().split(":");
  41. String color2 = id2[1];
  42.  
  43. if (!color1.equals(color2)) {
  44. int occurrences1 = 0;
  45. int occurrences2 = 0;
  46.  
  47. for (Map.Entry<String, Integer> dwarf : dwarfs.entrySet()) {
  48. if (dwarf.getKey().contains(color1)) {
  49. occurrences1++;
  50. } else if (dwarf.getKey().contains(color2)) {
  51. occurrences2++;
  52. }
  53. }
  54. result = Integer.compare(occurrences2, occurrences1);
  55. }
  56. }
  57.  
  58. return result;
  59.  
  60. }).forEach(dwarf -> {
  61.  
  62. String[] id = dwarf.getKey().split(":");
  63. String name = id[0];
  64. String color = id[1];
  65. System.out.printf("(%s) %s <-> %d%n", color, name, dwarf.getValue());
  66. });
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment