1518. 換酒問題
阿新 • • 發佈:2020-10-10
小區便利店正在促銷,用 numExchange 個空酒瓶可以兌換一瓶新酒。你購入了 numBottles 瓶酒。
如果喝掉了酒瓶中的酒,那麼酒瓶就會變成空的。
請你計算 最多 能喝到多少瓶酒。
示例 1:
輸入:numBottles = 9, numExchange = 3
輸出:13
解釋:你可以用 3 個空酒瓶兌換 1 瓶酒。
所以最多能喝到 9 + 3 + 1 = 13 瓶酒。
示例 2:
輸入:numBottles = 15, numExchange = 4
輸出:19
解釋:你可以用 4 個空酒瓶兌換 1 瓶酒。
所以最多能喝到 15 + 3 + 1 = 19 瓶酒。
示例 3:
輸入:numBottles = 5, numExchange = 5
輸出:6
示例 4:
輸入:numBottles = 2, numExchange = 3
輸出:2
提示:
1 <=numBottles <= 100
2 <=numExchange <= 100
來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/water-bottles
class Solution: def numWaterBottles(self, numBottles: int, numExchange: int) -> int: if numBottles<numExchange:return numBottlesif numBottles==numExchange:return numBottles+1 gain=numBottles//numExchange left=numBottles%numExchange res=gain*numExchange+gain+left while True: if gain+left>=numExchange: numBottles=gain+left gain=numBottles//numExchange left=numBottles%numExchange res+=gain else: break return res
class Solution: def numWaterBottles(self, numBottles: int, numExchange: int) -> int: if numBottles<numExchange:return numBottles left=numBottles%numExchange return numBottles-left+self.numWaterBottles(left+(numBottles-left)//numExchange,numExchange)
class Solution: def numWaterBottles(self, numBottles: int, numExchange: int) -> int: gain=numBottles while gain//numExchange>0: numBottles+=gain//numExchange gain=gain//numExchange+gain%numExchange return numBottles