Advertisement
Ligh7_of_H3av3n

10.TheLift

Jan 26th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. package Uprajneniq;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class TheLift {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int peopleInQueue = Integer.parseInt(scanner.nextLine());
  10.         String[] liftState = scanner.nextLine().split(" ");
  11.         int[] wagons = new int[liftState.length];
  12.         for (int i = 0; i < liftState.length; i++) {
  13.             wagons[i] = Integer.parseInt(liftState[i]);
  14.         }
  15.  
  16.         for (int i = 0; i < wagons.length; i++) {
  17.             while (wagons[i] < 4 && peopleInQueue > 0) {
  18.                 wagons[i]++;
  19.                 peopleInQueue--;
  20.             }
  21.         }
  22.  
  23.         if (peopleInQueue == 0 && hasEmptySpots(wagons)) {
  24.             System.out.println("The lift has empty spots!");
  25.         } else if (peopleInQueue > 0 && !hasEmptySpots(wagons)) {
  26.             System.out.printf("There isn't enough space! %d people in a queue!%n", peopleInQueue);
  27.         }
  28.  
  29.         printWagons(wagons);
  30.     }
  31.  
  32.     private static boolean hasEmptySpots(int[] wagons) {
  33.         for (int wagon : wagons) {
  34.             if (wagon < 4) {
  35.                 return true;
  36.             }
  37.         }
  38.         return false;
  39.     }
  40.  
  41.     private static void printWagons(int[] wagons) {
  42.         for (int wagon : wagons) {
  43.             System.out.print(wagon + " ");
  44.         }
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement