Advertisement
desislava_topuzakova

02. The Lift (с Лист)

Feb 15th, 2023
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. package ExamPreparation;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class SecondTask {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. int peopleWait = Integer.parseInt(scanner.nextLine()); //15
  13. List<Integer> wagons = Arrays.stream(scanner.nextLine().split("\\s+"))
  14. .map(Integer::parseInt).collect(Collectors.toList());
  15. //{0, 0, 0 , 0}
  16.  
  17.  
  18. for (int wagon = 0; wagon <= wagons.size() - 1; wagon++) {
  19. if (peopleWait > 0) {
  20. int currentCountPeople = wagons.get(wagon); //текущ брой на хората
  21. while (currentCountPeople < 4) {
  22. //добавям човек във вагона ако има чакащи
  23. if (peopleWait <= 0) {
  24. break;
  25. }
  26. currentCountPeople++;
  27. wagons.set(wagon, currentCountPeople);
  28. peopleWait--;
  29. }
  30. }
  31. }
  32.  
  33. if (peopleWait <= 0 && wagons.get(wagons.size() - 1) < 4) {
  34. //имаме останало място
  35. System.out.println("The lift has empty spots!");
  36. } else if (peopleWait > 0 && wagons.get(wagons.size() - 1) >= 4) {
  37. //нямаме повече място
  38. System.out.printf("There isn't enough space! %d people in a queue!%n", peopleWait);
  39. }
  40.  
  41. //отпечатваме вагоните
  42. System.out.println(wagons.toString()
  43. .replace("[", "")
  44. .replace("]", "")
  45. .replace(",", ""));
  46. }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement