Advertisement
SensaBG

Untitled

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