Didart

The Lift

Oct 21st, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class TheLift {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int people = Integer.parseInt(scanner.nextLine());
  9.         String input = scanner.nextLine();
  10.  
  11.         int[] wagons = Arrays
  12.                 .stream(input.split(" "))
  13.                 .mapToInt(Integer::parseInt)
  14.                 .toArray();
  15.  
  16.         int maxCapacity = 4;
  17.         boolean noMorePeople = false;
  18.  
  19.         for (int i = 0; i < wagons.length; i++) {
  20.             int currentWagon = wagons[i];
  21.  
  22.             if (people - (maxCapacity - currentWagon) == 0) {
  23.                 people -= maxCapacity - currentWagon;
  24.                 wagons[i] = 4;
  25.                 noMorePeople = true;
  26.                 break;
  27.             } else if (people - (maxCapacity - currentWagon) < 0) {
  28.                 wagons[i] = people;
  29.                 people = 0;
  30.                 noMorePeople = true;
  31.                 break;
  32.             } else {
  33.                 people -= maxCapacity - currentWagon;
  34.                 wagons[i] = 4;
  35.             }
  36.         }
  37.         boolean emptySpaces = false;
  38.  
  39.         for (int i = 0; i < wagons.length; i++) {
  40.             if (wagons[i] < 4) {
  41.                 emptySpaces = true;
  42.                 break;
  43.             }
  44.         }
  45.         if (noMorePeople && emptySpaces) {
  46.             System.out.println("The lift has empty spots!");
  47.         } else if (people > 0 && !emptySpaces) {
  48.             System.out.printf("There isn't enough space! %d people in a queue!%n", people);
  49.         }
  50.         System.out.print(Arrays.toString(wagons).replaceAll("[\\[\\]]", "").
  51.                 replaceAll(", ", " "));
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment