❔ Can someone help with this task in C#, it should be done with recursion
Determine the longest distance a car can travel under the following conditions:
The fuel is stored in 3 barrels, each with a capacity of 100 liters.
The car can only carry one barrel at a time.
The car's fuel tank capacity is 20 liters.
Fuel consumption is 10 liters per 100 kilometers.
11 Replies
I found the number 1.66666667, but I cannot make it in code with recursion.. thank you in advance
on one tank it can go 200km, and you have an answer 1.6666
?
Yes, I think this is the the number. it is not necessary to travel 20 liters or 200 km every time. First we travel -20l. One forward to 200 km and we leave the barrel, we refuel from it, it is already -40 and we return for the second. Then we get the second and refuel again from the second -80 and return for the third, it became -100 liters and -1 barrel. Then again with 20 liters of consumption to the 400th kilometer forward and back for the two left barrels and it becomes -60. At 400 kilometers we have 2 barrels - one with 100 liters, and the other with 40 liters. Now we divide 20 liters by 3 so we can go another 66.6667 kilometers with both barrels and from there we have a barrel with 20 and a barrel with 100, we refuel from the one with 20 and then we continue from the one with 100 and it becomes 1200. That is how it becomes 1200 + 400 + 66.66667
This is how it looks like
wait, you're adding the distance of each leg, not just the displacment from start to finish?
Only the distance it gets forward
ok, confused by the diagram
I think you need to look at it more generally, to put it in a recursive pattern.
If you have N barrels, the next step is when you have N-1 barrels, and that always occurs at a cost of 100litres
So given you have N barrels, what is the formula for how far forward you can go, ending up with N-1 barrels
Okay, thanks, thats a starting point I will look forward to.
what @phaseshift applied here you need for every recursive algorithm: break up the problem into steps, then within one recursion, solve just one step assuming all the other ones are handled, then enter the recursion with next step. This is usually the hard part when it comes to recursive algos, people usually dont have an issue figuring out the base case. And thats about it for recursion @EternalPurposE
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.