plamenavylcheva

Offroad

Oct 21st, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. package FirstTask;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class OffroadWithArrays {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. //initialFuel ->last -> stack
  10. //additional consumption index
  11. //amountOfFuelNeeded
  12. List<Integer> initialFuel = Arrays.stream(scanner.nextLine().split("\\s+"))
  13. .map(Integer::parseInt).collect(Collectors.toList());
  14. Collections.reverse(initialFuel);
  15. List<Integer> additionalConsumption = Arrays.stream(scanner.nextLine().split("\\s+"))
  16. .map(Integer::parseInt).collect(Collectors.toList());
  17. List<Integer> amountOfFuel = Arrays.stream(scanner.nextLine().split("\\s+"))
  18. .map(Integer::parseInt).collect(Collectors.toList());
  19. List<Integer> listOfAltitude = new ArrayList<>();
  20. int numberOfAltitude = 0;
  21. boolean hasReached = false;
  22. while (initialFuel.size() > 0 && additionalConsumption.size() > 0) {
  23. int fuel = initialFuel.get(0);
  24. int additional = additionalConsumption.get(0);
  25. int compare = amountOfFuel.get(0);
  26. int result = fuel - additional;
  27. if (result >= compare) {
  28. initialFuel.remove(0);
  29. additionalConsumption.remove(0);
  30. amountOfFuel.remove(0);
  31. numberOfAltitude++;
  32. System.out.printf("John has reached: Altitude %d%n", numberOfAltitude);
  33. // listOfAltitude.add(numberOfAltitude);
  34. } else {
  35. System.out.printf("John did not reach: Altitude %d%n", numberOfAltitude + 1);
  36. break;
  37. }
  38. if (numberOfAltitude == 4) {
  39. hasReached = true;
  40. }
  41. }
  42. if (!hasReached && numberOfAltitude > 0) {
  43. System.out.printf("John failed to reach the top.%n");
  44. System.out.print("Reached altitudes: ");
  45. for (int i = 1; i <= numberOfAltitude; i++) {
  46. if (i < numberOfAltitude) {
  47. System.out.printf("Altitude %d, ", i);
  48. } else {
  49. System.out.printf("Altitude %d", i);
  50. }
  51. }
  52. System.out.println();
  53. } else if (!hasReached && numberOfAltitude == 0) {
  54. System.out.println("John failed to reach the top.");
  55. System.out.println("John didn't reach any altitude.");
  56. } else if (hasReached && numberOfAltitude == 4) {
  57. System.out.println("John has reached all the altitudes and managed to reach the top!");
  58. }
  59. }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment