Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class MyClass {
- public static void main(String args[]) {
- java.util.Scanner scanner = new java.util.Scanner(System.in);
- int numberOfPeople = Integer.parseInt(scanner.nextLine());
- String[] wagons = scanner.nextLine().split(" ");
- int[] wagon = new int[wagons.length];
- for(int i = 0;i < wagons.length;i++) {
- wagon[i] = Integer.parseInt(wagons[i]);
- }
- int maxPeoplePerWagon = 4;
- for(int index = 0; index < wagon.length; index++) {
- if (wagon[index] < maxPeoplePerWagon) {
- if (numberOfPeople <= (maxPeoplePerWagon - wagon[index])) {
- wagon[index] += numberOfPeople;
- numberOfPeople = 0;
- } else {
- numberOfPeople = numberOfPeople - maxPeoplePerWagon + wagon[index];
- wagon[index] = maxPeoplePerWagon;
- }
- }
- }
- if (checkForEmptyWagonSpaces(wagon)) {
- System.out.println("The lift has empty spots!");
- System.out.println(printAllWagons(wagon));
- } else {
- if (numberOfPeople == 0) {
- System.out.println(printAllWagons(wagon));
- } else {
- System.out.printf("There isn't enough space! %d people in the queue!\n %s",numberOfPeople,printAllWagons(wagon));
- }
- }
- }
- public static boolean checkForEmptyWagonSpaces(int[] allWagons) {
- for (int index = 0; index < allWagons.length; index++) {
- if (allWagons[index] < 4) {
- return true;
- }
- }
- return false;
- }
- public static String printAllWagons(int[] allWagons) {
- String allWagonsString = "";
- for(int i=0;i<allWagons.length;i++) {
- allWagonsString = allWagonsString + allWagons[i] + " ";
- }
- allWagonsString = allWagonsString.substring(0, allWagonsString.length()-1);
- return allWagonsString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement