Total Distance Traveled
To calculate the maximum distance traveled by the truck with the given fuel injection rules, we’ll follow these steps:
- Initialize a variable
distance
to keep track of the distance traveled. - While there is fuel in the main tank:
a. Determine how much fuel can be consumed from the main tank without needing to inject from the additional tank.
b. Calculate the distance that can be covered using that fuel, and update the
distance
. c. If possible, inject 1 liter from the additional tank to the main tank for every 5 liters consumed. d. Update the main and additional tanks according to the fuel consumed and injected. - Once the main tank is empty, add the distance that can be covered using the remaining fuel in the main tank.
- Return the final
distance
.
Here’s the code:
|
|
This code accounts for the injection rules and calculates the maximum distance that can be traveled with the given fuel in the main and additional tanks.