Advertisement
tripTiPscout

01. KAT

Oct 19th, 2022
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package Advanced;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         ArrayDeque<Long> licensePlatesQueue = Arrays.stream(scanner.nextLine().split(", "))
  14.                 .map(Long::parseLong)
  15.                 .collect(Collectors.toCollection(ArrayDeque::new));
  16.  
  17.         ArrayDeque<Long> carsStack = new ArrayDeque<>();
  18.  
  19.         Arrays.stream(scanner.nextLine().split(", "))
  20.                 .map(Long::parseLong)
  21.                 .forEach(carsStack::push);
  22.  
  23.         int daysCount = 0;
  24.         long registeredCarsCount = 0;
  25.  
  26.         while (!licensePlatesQueue.isEmpty() && !carsStack.isEmpty()) {
  27.             long currentPlatesCount = licensePlatesQueue.poll();
  28.             long currentCarsCount = carsStack.pop();
  29.             daysCount++;
  30.             long remainingPlates;
  31.             long remainingCars;
  32.             if (currentPlatesCount / 2 < currentCarsCount) {
  33.                 registeredCarsCount += currentPlatesCount / 2;
  34.                 remainingCars = currentCarsCount - currentPlatesCount / 2;
  35.                 carsStack.push(remainingCars);
  36.             } else if (currentPlatesCount / 2 > currentCarsCount) {
  37.                 remainingPlates = currentPlatesCount - currentCarsCount * 2;
  38.                 licensePlatesQueue.offer(remainingPlates);
  39.                 registeredCarsCount += currentCarsCount;
  40.             } else {
  41.                 registeredCarsCount += currentPlatesCount / 2;
  42.             }
  43.         }
  44.  
  45.         System.out.printf("%d cars were registered for %d days!\n", registeredCarsCount, daysCount);
  46.  
  47.         if (!licensePlatesQueue.isEmpty()) {
  48.             long platesRemain = 0;
  49.             while (!licensePlatesQueue.isEmpty()) {
  50.                 platesRemain += licensePlatesQueue.poll();
  51.             }
  52.             System.out.printf("%d license plates remain!", platesRemain);
  53.         } else if (!carsStack.isEmpty()) {
  54.             long unregisteredCars = 0;
  55.             while (!carsStack.isEmpty()) {
  56.                 unregisteredCars += carsStack.pop();
  57.             }
  58.             System.out.printf("%d cars remain without license plates!", unregisteredCars);
  59.         } else {
  60.             System.out.print("Good job! There is no queue in front of the KAT!");
  61.         }
  62.     }
  63.  
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement