Advertisement
psi_mmobile

Untitled

Oct 22nd, 2022
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. import java.util.Arrays;
  2. public class MyClass {
  3.     public static void main(String args[]) {
  4.         java.util.Scanner scanner = new java.util.Scanner(System.in);
  5.         int numberOfPeople = Integer.parseInt(scanner.nextLine());
  6.         String[] wagons = scanner.nextLine().split(" ");
  7.         int[] wagon = new int[wagons.length];
  8.         for(int i = 0;i < wagons.length;i++)
  9.         {
  10.            wagon[i] = Integer.parseInt(wagons[i]);
  11.         }
  12.         int maxPeoplePerWagon = 4;
  13.        
  14.         for(int index = 0; index < wagon.length; index++) {
  15.             if (wagon[index] < maxPeoplePerWagon) {
  16.                 if (numberOfPeople < maxPeoplePerWagon) {
  17.                     wagon[index] = numberOfPeople;
  18.                 } else {
  19.                     numberOfPeople -= maxPeoplePerWagon - wagon[index];
  20.                     wagon[index] = maxPeoplePerWagon;
  21.                 }
  22.             }
  23.         }
  24.         if (checkForEmptyWagonSpaces(wagon)) {
  25.             System.out.println("The lift has empty spots!");
  26.             System.out.println(printAllWagons(wagon));
  27.         } else {
  28.             if (numberOfPeople == 0) {
  29.                 System.out.println(printAllWagons(wagon));
  30.             } else {
  31.                 System.out.printf("There isn't enough space! %d people in the queue!\n %s",numberOfPeople,printAllWagons(wagon));
  32.             }
  33.         }
  34.     }
  35.    
  36.     public static boolean checkForEmptyWagonSpaces(int[] allWagons) {
  37.         for (int index = 0; index < allWagons.length; index++) {
  38.             if (allWagons[index] < 4) {
  39.                 return true;
  40.             }
  41.         }
  42.         return false;
  43.     }
  44.     public static String printAllWagons(int[] allWagons) {
  45.         String allWagonsString = "";
  46.         for(int i=0;i<allWagons.length;i++) {
  47.             allWagonsString = allWagonsString + allWagons[i] + " ";
  48.         }
  49.         allWagonsString = allWagonsString.substring(0, allWagonsString.length()-1);
  50.         return allWagonsString;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement