Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class train {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> train = readIntList(scanner);
- int wagonCapacity = Integer.parseInt(scanner.nextLine());
- String input = scanner.nextLine();
- while (!"End".equals(input)) {
- String[] tokens = input.split("\\s+");
- if (tokens[0].equals("Add")) {
- int people = Integer.parseInt(tokens[1]);
- train.add(people);
- } else {
- int people = Integer.parseInt(tokens[0]);
- for (int i = 0; i < train.size(); i++) {
- int currentWaggon = train.get(i);
- int totalCount = currentWaggon + people;
- if (totalCount <= wagonCapacity) {
- train.set(i, totalCount);
- break;
- }
- }
- }
- input = scanner.nextLine();
- }
- printTrain(train);
- }
- public static ArrayList<Integer> readIntList(Scanner scanner) {
- String[] input = scanner.nextLine().split("\\s+");
- ArrayList<Integer> outputList = new ArrayList<>();
- for (String element : input) {
- outputList.add(Integer.parseInt(element));
- }
- return outputList;
- }
- public static void printTrain(List<Integer> train) {
- for (int wagon : train) {
- System.out.print(wagon + " ");
- }
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement