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中文网其他相关文章!