Advertisement
SensaBG

t01

Jun 22nd, 2024 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package RegularExam;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6. import java.util.function.Consumer;
  7.  
  8. public class T01 {
  9.     private static int deliveredWeight;
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         ArrayDeque<Integer> packagesWeight = new ArrayDeque<>();
  14.         fillArrayDeque(scanner, packagesWeight::push);
  15.  
  16.         ArrayDeque<Integer> couriersCapacity = new ArrayDeque<>();
  17.         fillArrayDeque(scanner, couriersCapacity::offer);
  18.  
  19.         while (!packagesWeight.isEmpty() && !couriersCapacity.isEmpty()) {
  20.  
  21.             int weight = packagesWeight.pollLast();
  22.             int capacity = couriersCapacity.pollFirst();
  23.  
  24.             if (capacity >= weight) {
  25.                 deliveredWeight += weight;
  26.                 int newCapacity = capacity - (weight * 2);
  27.                 if (newCapacity > 0) {
  28.                     couriersCapacity.offerLast(newCapacity);
  29.                 }
  30.             } else {
  31.                 deliveredWeight += capacity;
  32.                 packagesWeight.offerLast(weight - capacity);
  33.             }
  34.         }
  35.        
  36.         System.out.printf("Total weight: %d kg\n", deliveredWeight);
  37.  
  38.         if (packagesWeight.isEmpty() && couriersCapacity.isEmpty()) {
  39.             System.out.println("Congratulations, all packages were delivered successfully by the couriers today.");
  40.         } else {
  41.             if (!packagesWeight.isEmpty()) {
  42.                 System.out.print("Unfortunately, there are no more available couriers to deliver the following packages: ");
  43.                 System.out.println(packagesWeight.toString().replaceAll("[\\[\\]]", ""));
  44.             }
  45.             if (!couriersCapacity.isEmpty()) {
  46.                 System.out.print("Couriers are still on duty: ");
  47.                 System.out.println(couriersCapacity.toString().replaceAll("[\\[\\]]", "") + " but there are no more packages to deliver.");
  48.             }
  49.         }
  50.     }
  51.  
  52.     public static void fillArrayDeque(Scanner scanner, Consumer<Integer> insertion) {
  53.         Arrays.stream(scanner.nextLine().split("\\s+"))
  54.                 .map(Integer::parseInt)
  55.                 .forEach(insertion);
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement