sunfresh

Noah's Ark

Jan 18th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. Noah's Ark
  2. Help out the Modern-day Noah in his quest to save the animal species from the extinction. The only difference between Old-day Noah, and Modern-day Noah is that Modern-day Noah wants more than two of each species on the ark, but still wants them to be a even count.
  3.  
  4. Write a program that keeps count of how many animals of each kind are on the ark and whether they are an even or not.
  5.  
  6. Oh by the way he wants the list sorted in alphabetical order. Jeez... picky much?
  7.  
  8. Input
  9. Read from the standard input
  10.  
  11. On the first line there is the number N - lines of input to follow;
  12.  
  13. On the next N lines, there are the animals' species.
  14.  
  15. Output
  16. Print to the standard output
  17.  
  18. On separate lines print how many of each animal have boarded the ark, and weather there is an even amount of them.
  19.  
  20. See sample tests for examples
  21. Sample tests
  22. Input
  23. 5
  24. fish
  25. crocodile
  26. moose
  27. moose
  28. bald eagle
  29. Output
  30. bald eagle 1 No
  31. crocodile 1 No
  32. fish 1 No
  33. moose 2 Yes
  34.  
  35. package SetAndMap;
  36. import java.util.*;
  37. public class NoahsArk {
  38.  
  39. public static void main(String[] args) {
  40. Scanner scanner = new Scanner(System.in);
  41. int n = Integer.parseInt(scanner.nextLine());
  42. HashMap<String,Integer> animals = new HashMap<>();
  43. for (int i = 0; i < n; i++) {
  44. String animalInput = scanner.nextLine();
  45. int count = 1;
  46. if (animals.containsKey(animalInput)){
  47. count = animals.get(animalInput);
  48. animals.put(animalInput, count+1);
  49. } else {
  50. animals.putIfAbsent(animalInput, 1);
  51. }
  52. }
  53. Set<String> sortedSet = new TreeSet<>(animals.keySet());
  54. for (String key : sortedSet) {
  55. String twoOrOne = "";
  56. if (animals.get(key) % 2 == 0){
  57. twoOrOne = "Yes";
  58. } else {
  59. twoOrOne = "No";
  60. }
  61. System.out.printf("%s %s %s%n", key, animals.get(key), twoOrOne);
  62. }
  63. }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment