SHOW:
|
|
- or go back to the newest paste.
| 1 | import java.util.ArrayList; | |
| 2 | import java.util.Arrays; | |
| 3 | import java.util.List; | |
| 4 | import java.util.Scanner; | |
| 5 | import java.util.stream.Collectors; | |
| 6 | ||
| 7 | public class Main {
| |
| 8 | public static void main(String[] args) {
| |
| 9 | Scanner scanner = new Scanner(System.in); | |
| 10 | //"32 54 21 12 4 0 23" -> ["32", "54", "21", "12", "4", "0", "23"] | |
| 11 | List<Integer> wagons = Arrays.stream(scanner.nextLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList());
| |
| 12 | int maxCapacity = Integer.parseInt(scanner.nextLine()); //макс брой пасажери във всеки един вагон | |
| 13 | ||
| 14 | String command = scanner.nextLine(); | |
| 15 | while(!command.equals("end")) {
| |
| 16 | //"Add {passengers}" -> ["Add", "5"]
| |
| 17 | //"{passengers}" -> ["45"]
| |
| 18 | String[] commandData = command.split(" ");//["Add", "5"] или ["45"]
| |
| 19 | if (commandData[0].equals("Add")) {
| |
| 20 | //команда Add | |
| 21 | int passengers = Integer.parseInt(commandData[1]); | |
| 22 | wagons.add(passengers); | |
| 23 | } else {
| |
| 24 | //команда -> число | |
| 25 | int passengersToAdd = Integer.parseInt(commandData[0]); | |
| 26 | for(int index = 0; index < wagons.size(); index++) {
| |
| 27 | int currentWagon = wagons.get(index); | |
| 28 | if(currentWagon + passengersToAdd <= maxCapacity) {
| |
| 29 | wagons.set(index, currentWagon + passengersToAdd); | |
| 30 | break; | |
| 31 | } | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | command = scanner.nextLine(); | |
| 36 | } | |
| 37 | ||
| 38 | for (int wagon : wagons) {
| |
| 39 | System.out.print(wagon + " "); | |
| 40 | } | |
| 41 | ||
| 42 | //wagons.forEach(wagon -> System.out.print(wagon + " ")); | |
| 43 | ||
| 44 | } | |
| 45 | } |