kalinikov

14. Dragon Army

Dec 19th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5.  
  6. public class DragonArmy {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. int n = Integer.parseInt(scanner.nextLine());
  11.  
  12. Map<String, TreeMap<String, int[]>> dragons = new LinkedHashMap<>();
  13.  
  14. for (int i = 0; i < n; i++) {
  15. String[] tokens = scanner.nextLine().split(" ");
  16. String type = tokens[0];
  17. String name = tokens[1];
  18.  
  19. int damage = tokens[2].equals("null") ? 45 : Integer.parseInt(tokens[2]);
  20. int health = tokens[3].equals("null") ? 250 : Integer.parseInt(tokens[3]);
  21. int armor = tokens[4].equals("null") ? 10 : Integer.parseInt(tokens[4]);
  22.  
  23. dragons.putIfAbsent(type, new TreeMap<>());
  24. dragons.get(type).put(name, new int[] {damage, health, armor});
  25. }
  26.  
  27. dragons.forEach((key, value) -> {
  28.  
  29. double averageDamage = value.values().stream()
  30. .mapToInt(ints -> ints[0])
  31. .average()
  32. .orElse(0.0);
  33.  
  34. double averageHealth = value.values().stream()
  35. .mapToInt(ints -> ints[1])
  36. .average()
  37. .orElse(0.0);
  38.  
  39. double averageArmor = value.values().stream()
  40. .mapToInt(ints -> ints[2])
  41. .average()
  42. .orElse(0.0);
  43.  
  44. System.out.println(String.format("%s::(%.2f/%.2f/%.2f)",
  45. key, averageDamage, averageHealth, averageArmor));
  46.  
  47. value.forEach((key1, value1) -> System.out.printf("-%s -> damage: %d, health: %d, armor: %d%n",
  48. key1,
  49. value1[0],
  50. value1[1],
  51. value1[2]));
  52.  
  53. });
  54.  
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment