Advertisement
Guest User

Train

a guest
Feb 20th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class train {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. List<Integer> train = readIntList(scanner);
  9. int wagonCapacity = Integer.parseInt(scanner.nextLine());
  10.  
  11. String input = scanner.nextLine();
  12. while (!"End".equals(input)) {
  13. String[] tokens = input.split("\\s+");
  14. if (tokens[0].equals("Add")) {
  15. int people = Integer.parseInt(tokens[1]);
  16. train.add(people);
  17.  
  18. } else {
  19. int people = Integer.parseInt(tokens[0]);
  20. for (int i = 0; i < train.size(); i++) {
  21. int currentWaggon = train.get(i);
  22.  
  23. int totalCount = currentWaggon + people;
  24. if (totalCount <= wagonCapacity) {
  25. train.set(i, totalCount);
  26. break;
  27.  
  28. }
  29. }
  30. }
  31. input = scanner.nextLine();
  32. }
  33. printTrain(train);
  34.  
  35. }
  36.  
  37. public static ArrayList<Integer> readIntList(Scanner scanner) {
  38. String[] input = scanner.nextLine().split("\\s+");
  39. ArrayList<Integer> outputList = new ArrayList<>();
  40. for (String element : input) {
  41. outputList.add(Integer.parseInt(element));
  42. }
  43. return outputList;
  44. }
  45.  
  46. public static void printTrain(List<Integer> train) {
  47. for (int wagon : train) {
  48. System.out.print(wagon + " ");
  49. }
  50. return;
  51. }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement