Advertisement
desislava_topuzakova

02. The Lift (with array)

Feb 15th, 2023
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 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. int [] wagons = Arrays.stream(scanner.nextLine().split("\\s+"))
  14. .mapToInt(Integer::parseInt).toArray();
  15. //[0, 0, 0 , 0]
  16.  
  17.  
  18. for (int wagon = 0; wagon <= wagons.length - 1; wagon++) {
  19. if (peopleWait > 0) {
  20. int currentCountPeople = wagons[wagon]; //текущ брой на хората
  21. while (currentCountPeople < 4) {
  22. //добавям човек във вагона ако има чакащи
  23. if (peopleWait <= 0) {
  24. break;
  25. }
  26. currentCountPeople++;
  27. wagons[wagon] = currentCountPeople;
  28. peopleWait--;
  29. }
  30. }
  31. }
  32.  
  33. if (peopleWait <= 0 && wagons[wagons.length - 1] < 4) {
  34. //имаме останало място
  35. System.out.println("The lift has empty spots!");
  36. } else if (peopleWait > 0 && wagons[wagons.length - 1] >= 4) {
  37. //нямаме повече място
  38. System.out.printf("There isn't enough space! %d people in a queue!%n", peopleWait);
  39. }
  40.  
  41.  
  42.  
  43. //отпечатваме вагоните
  44. for (int wagon : wagons) {
  45. System.out.print(wagon + " ");
  46. }
  47. }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement