Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Scanner;
- public class RapidCourier {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split(" ");
- String[] courier = scanner.nextLine().split(" ");
- ArrayDeque<Integer> packages = new ArrayDeque<>();
- ArrayDeque<Integer> couriers = new ArrayDeque<>();
- for (String w : input) {
- packages.add(Integer.parseInt(w));
- }
- for (String c : courier) {
- couriers.add(Integer.parseInt(c));
- }
- int total = 0;
- while (!packages.isEmpty() && !couriers.isEmpty()){
- int weight = packages.peek();
- int capacity = couriers.peek();
- if (capacity >= weight){
- int newCapacity = capacity - weight; // коригирано capacity - weight
- total += weight;
- packages.poll();
- if (newCapacity > 0){
- couriers.add(couriers.poll() - weight); // коригирано couriers.poll() - weight
- } else {
- couriers.poll();
- }
- } else {
- int remaining = weight - capacity;
- packages.poll();
- couriers.poll();
- total += capacity;
- packages.addFirst(remaining);
- }
- }
- System.out.printf("Total weight: %d kg\n", total);
- if (packages.isEmpty()) {
- System.out.println("Congratulations, all packages were delivered successfully by the couriers today.");
- } else if (couriers.isEmpty()) {
- System.out.print("Unfortunately, there are no more available couriers to deliver the following packages: ");
- StringBuilder remainingPackages = new StringBuilder();
- while (!packages.isEmpty()) {
- remainingPackages.append(packages.poll());
- if (!packages.isEmpty()) {
- remainingPackages.append(", ");
- }
- }
- System.out.println(remainingPackages);
- } else {
- System.out.println("Couriers are still on duty: " + couriers.toString() + " but there are no more packages to deliver.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement