Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayDeque;
- import java.util.Arrays;
- import java.util.Deque;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int wavesCount = Integer.parseInt(scan.nextLine());
- Deque<Integer> spartansPlateQueue = new ArrayDeque<>();
- Arrays.stream(scan.nextLine().split("\\s+"))
- .map(Integer::parseInt)
- .forEach(spartansPlateQueue::offer);
- Deque<Integer> trojanWarriorsStack = new ArrayDeque<>();
- for (int i = 1; i <= wavesCount; i++) {
- if (!spartansPlateQueue.isEmpty()) {
- Arrays.stream(scan.nextLine().split("\\s+"))
- .map(Integer::parseInt)
- .forEach(trojanWarriorsStack::push);
- }else {
- break;
- }
- if (i % 3 == 0){
- spartansPlateQueue.addLast(Integer.parseInt(scan.nextLine()));
- }
- while (!spartansPlateQueue.isEmpty() && !trojanWarriorsStack.isEmpty()){
- int currentPlate = spartansPlateQueue.pop();
- int currentWarrior = trojanWarriorsStack.poll();
- if (currentPlate != currentWarrior){
- if (currentWarrior > currentPlate){
- currentWarrior -= currentPlate;
- trojanWarriorsStack.push(currentWarrior);
- }else {
- currentPlate -= currentWarrior;
- spartansPlateQueue.addFirst(currentPlate);
- }
- }
- }
- }
- if (spartansPlateQueue.isEmpty()){
- System.out.println("The Trojans successfully destroyed the Spartan defense.");
- System.out.println("Warriors left: " + trojanWarriorsStack.stream()
- .map(String::valueOf)
- .collect(Collectors.joining(", ")));
- }else {
- System.out.println("The Spartans successfully repulsed the Trojan attack.");
- System.out.println("Plates left: " + spartansPlateQueue.stream()
- .map(String::valueOf)
- .collect(Collectors.joining(", ")));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement