Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Noah's Ark
- 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.
- 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.
- Oh by the way he wants the list sorted in alphabetical order. Jeez... picky much?
- Input
- Read from the standard input
- On the first line there is the number N - lines of input to follow;
- On the next N lines, there are the animals' species.
- Output
- Print to the standard output
- On separate lines print how many of each animal have boarded the ark, and weather there is an even amount of them.
- See sample tests for examples
- Sample tests
- Input
- 5
- fish
- crocodile
- moose
- moose
- bald eagle
- Output
- bald eagle 1 No
- crocodile 1 No
- fish 1 No
- moose 2 Yes
- package SetAndMap;
- import java.util.*;
- public class NoahsArk {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- HashMap<String,Integer> animals = new HashMap<>();
- for (int i = 0; i < n; i++) {
- String animalInput = scanner.nextLine();
- int count = 1;
- if (animals.containsKey(animalInput)){
- count = animals.get(animalInput);
- animals.put(animalInput, count+1);
- } else {
- animals.putIfAbsent(animalInput, 1);
- }
- }
- Set<String> sortedSet = new TreeSet<>(animals.keySet());
- for (String key : sortedSet) {
- String twoOrOne = "";
- if (animals.get(key) % 2 == 0){
- twoOrOne = "Yes";
- } else {
- twoOrOne = "No";
- }
- System.out.printf("%s %s %s%n", key, animals.get(key), twoOrOne);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment