1518。水瓶
簡單
有 numBottles 個水瓶最初裝滿了水。您可以在市場上用 numExchange 個空水瓶兌換一瓶滿水。
喝滿水瓶的操作會將其變成空瓶。
給定兩個整數 numBottles 和 numExchange,傳回您可以飲用的最大水瓶數。
範例1:
範例2:
約束:
解:
class Solution { /** * @param Integer $numBottles * @param Integer $numExchange * @return Integer */ function numWaterBottles($numBottles, $numExchange) { $totalDrunk = 0; $emptyBottles = 0; while ($numBottles > 0) { // Drink all the current full bottles $totalDrunk += $numBottles; // Collect the empty bottles $emptyBottles += $numBottles; // Exchange the empty bottles for new full ones $numBottles = floor($emptyBottles / $numExchange); $emptyBottles = $emptyBottles % $numExchange; } return $totalDrunk; } }
以上是水瓶的詳細內容。更多資訊請關注PHP中文網其他相關文章!