Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class TheLift {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int people = Integer.parseInt(scanner.nextLine());
- String input = scanner.nextLine();
- int[] wagons = Arrays
- .stream(input.split(" "))
- .mapToInt(Integer::parseInt)
- .toArray();
- int maxCapacity = 4;
- boolean noMorePeople = false;
- for (int i = 0; i < wagons.length; i++) {
- int currentWagon = wagons[i];
- if (people - (maxCapacity - currentWagon) == 0) {
- people -= maxCapacity - currentWagon;
- wagons[i] = 4;
- noMorePeople = true;
- break;
- } else if (people - (maxCapacity - currentWagon) < 0) {
- wagons[i] = people;
- people = 0;
- noMorePeople = true;
- break;
- } else {
- people -= maxCapacity - currentWagon;
- wagons[i] = 4;
- }
- }
- boolean emptySpaces = false;
- for (int i = 0; i < wagons.length; i++) {
- if (wagons[i] < 4) {
- emptySpaces = true;
- break;
- }
- }
- if (noMorePeople && emptySpaces) {
- System.out.println("The lift has empty spots!");
- } else if (people > 0 && !emptySpaces) {
- System.out.printf("There isn't enough space! %d people in a queue!%n", people);
- }
- System.out.print(Arrays.toString(wagons).replaceAll("[\\[\\]]", "").
- replaceAll(", ", " "));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment