Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class _01_TrojanInvasion {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int waves = Integer.parseInt(sc.nextLine());
- //Queue:
- ArrayDeque<Integer> defenders =
- Arrays.stream(sc.nextLine()
- .split("\\s+"))
- .map(Integer::parseInt)
- .collect(Collectors.toCollection(ArrayDeque::new));
- //Stack:
- ArrayDeque<Integer> attackers = new ArrayDeque<>();
- for (int i = 1; i <= waves && !defenders.isEmpty(); i++) {
- Arrays.stream(sc.nextLine()
- .split("\\s+"))
- .map(Integer::parseInt)
- .forEach(attackers::push);
- if (i % 3 == 0){
- int extraDef = Integer.parseInt(sc.nextLine());
- defenders.offer(extraDef);
- }
- while (!attackers.isEmpty() && !defenders.isEmpty()){
- int attack = attackers.pop();
- int defense = defenders.poll();
- if (attack > defense){
- attack -= defense;
- attackers.push(attack);
- }else if(defense > attack){
- defense -= attack;
- defenders.addFirst(defense);
- }
- }
- }
- if (defenders.isEmpty()){
- System.out.println("The Trojans successfully destroyed the Spartan defense.");
- System.out.printf("Warriors left: %s%n",
- String.join(", ", attackers.toString().replaceAll("[\\[\\]]", "")));
- } else {
- System.out.println("The Spartans successfully repulsed the Trojan attack.");
- System.out.printf("Plates left: %s%n",
- String.join(", ", defenders.toString().replaceAll("[\\[\\]]", "")));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement