Advertisement
CR7CR7

lift

Oct 21st, 2022
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.IntStream;
  3.  
  4. public class TheLift {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scan = new Scanner(System.in);
  9.         int people = Integer.parseInt(scan.nextLine());
  10.         int[] lift = Arrays.stream(scan.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  11.  
  12.         for (int i = 0; i < lift.length; i++) {
  13.             if ((lift[i] < 4)) {
  14.                 if (people >= 4 - lift[i]) {
  15.                     people -= 4 - lift[i];
  16.                     lift[i] = 4;
  17.                 } else {
  18.                     lift[i] += people;
  19.                     people = 0;
  20.                 }
  21.             }
  22.         }
  23.         boolean full = IntStream.range(0, lift.length).noneMatch(i -> lift[i] != 4);
  24.         if (people == 0 && !full) {
  25.             System.out.println("The lift has empty spots!");
  26.         } else if (people > 0 && full) {
  27.             System.out.printf("There isn't enough space! %d people in a queue!%n", people);
  28.         }
  29.         System.out.print(Arrays.toString(lift).replaceAll("[\\[\\]]", "").replaceAll(", ", " "));
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement