Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FirstTask;
- import java.util.*;
- import java.util.stream.Collectors;
- public class OffroadWithArrays {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- //initialFuel ->last -> stack
- //additional consumption index
- //amountOfFuelNeeded
- List<Integer> initialFuel = Arrays.stream(scanner.nextLine().split("\\s+"))
- .map(Integer::parseInt).collect(Collectors.toList());
- Collections.reverse(initialFuel);
- List<Integer> additionalConsumption = Arrays.stream(scanner.nextLine().split("\\s+"))
- .map(Integer::parseInt).collect(Collectors.toList());
- List<Integer> amountOfFuel = Arrays.stream(scanner.nextLine().split("\\s+"))
- .map(Integer::parseInt).collect(Collectors.toList());
- List<Integer> listOfAltitude = new ArrayList<>();
- int numberOfAltitude = 0;
- boolean hasReached = false;
- while (initialFuel.size() > 0 && additionalConsumption.size() > 0) {
- int fuel = initialFuel.get(0);
- int additional = additionalConsumption.get(0);
- int compare = amountOfFuel.get(0);
- int result = fuel - additional;
- if (result >= compare) {
- initialFuel.remove(0);
- additionalConsumption.remove(0);
- amountOfFuel.remove(0);
- numberOfAltitude++;
- System.out.printf("John has reached: Altitude %d%n", numberOfAltitude);
- // listOfAltitude.add(numberOfAltitude);
- } else {
- System.out.printf("John did not reach: Altitude %d%n", numberOfAltitude + 1);
- break;
- }
- if (numberOfAltitude == 4) {
- hasReached = true;
- }
- }
- if (!hasReached && numberOfAltitude > 0) {
- System.out.printf("John failed to reach the top.%n");
- System.out.print("Reached altitudes: ");
- for (int i = 1; i <= numberOfAltitude; i++) {
- if (i < numberOfAltitude) {
- System.out.printf("Altitude %d, ", i);
- } else {
- System.out.printf("Altitude %d", i);
- }
- }
- System.out.println();
- } else if (!hasReached && numberOfAltitude == 0) {
- System.out.println("John failed to reach the top.");
- System.out.println("John didn't reach any altitude.");
- } else if (hasReached && numberOfAltitude == 4) {
- System.out.println("John has reached all the altitudes and managed to reach the top!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment