Advertisement
Ligh7_of_H3av3n

RapidCourier

Jun 22nd, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3.  
  4. public class RapidCourier {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String[] input = scanner.nextLine().split(" ");
  11.         String[] courier = scanner.nextLine().split(" ");
  12.  
  13.         ArrayDeque<Integer> packages = new ArrayDeque<>();
  14.         ArrayDeque<Integer> couriers = new ArrayDeque<>();
  15.  
  16.         for (String w : input) {
  17.             packages.add(Integer.parseInt(w));
  18.         }
  19.  
  20.         for (String c : courier) {
  21.             couriers.add(Integer.parseInt(c));
  22.         }
  23.  
  24.         int total = 0;
  25.  
  26.         while (!packages.isEmpty() && !couriers.isEmpty()){
  27.             int weight = packages.peek();
  28.             int capacity = couriers.peek();
  29.  
  30.             if (capacity >= weight){
  31.                 int newCapacity = capacity - weight; // коригирано capacity - weight
  32.                 total += weight;
  33.                 packages.poll();
  34.  
  35.                 if (newCapacity > 0){
  36.                    couriers.add(couriers.poll() - weight); // коригирано couriers.poll() - weight
  37.                 } else {
  38.                     couriers.poll();
  39.                 }
  40.             } else {
  41.                 int remaining = weight - capacity;
  42.  
  43.                 packages.poll();
  44.                 couriers.poll();
  45.  
  46.                 total += capacity;
  47.  
  48.                 packages.addFirst(remaining);
  49.             }
  50.         }
  51.  
  52.         System.out.printf("Total weight: %d kg\n", total);
  53.  
  54.         if (packages.isEmpty()) {
  55.             System.out.println("Congratulations, all packages were delivered successfully by the couriers today.");
  56.         } else if (couriers.isEmpty()) {
  57.             System.out.print("Unfortunately, there are no more available couriers to deliver the following packages: ");
  58.             StringBuilder remainingPackages = new StringBuilder();
  59.  
  60.             while (!packages.isEmpty()) {
  61.                 remainingPackages.append(packages.poll());
  62.                 if (!packages.isEmpty()) {
  63.                     remainingPackages.append(", ");
  64.                 }
  65.             }
  66.             System.out.println(remainingPackages);
  67.         } else {
  68.             System.out.println("Couriers are still on duty: " + couriers.toString() + " but there are no more packages to deliver.");
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement