golang學習day2

作者: SecondRun (雨夜琴聲)   2024-01-24 10:35:58
2739. Total Distance Traveled
A truck has two fuel tanks. You are given two integers, mainTank representing the fuel present in the main tank in liters and additionalTank representing the fuel present in the additional tank in liters.
The truck has a mileage of 10 km per liter. Whenever 5 liters of fuel get used up in the main tank, if the additional tank has at least 1 liters of fuel, 1 liters of fuel will be transferred from the additional tank to the main tank.
Return the maximum distance which can be traveled.
Note: Injection from the additional tank is not continuous. It happens suddenly and immediately for every 5 liters consumed.
想法:
每消耗5的main就會補1,補到5也會再補=>使用遞迴
另外每次補的時候都檢查additional夠不夠
以下GO code
func distanceTraveled(mainTank int, additionalTank int) int {
if mainTank/5 == 0 {
return mainTank * 10
}
min := Min(mainTank/5, additionalTank)
return (mainTank - mainTank%5) * 10 + distanceTraveled(mainTank%5 + min, additionalTank - min)
}
func Min(a int, b int) int {
if a < b {
return a
}
return b
}
作者: JIWP (JIWP)   2024-01-24 10:39:00
大師
作者: sustainer123 (caster)   2024-01-24 10:41:00
大師
作者: Che31128 (justjoke)   2024-01-24 11:01:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com