一道简单的问题,面试时曾遇到过这道问题,答完之后解答时被告知还能向店家借一瓶,当时的想法是,这一瓶,我没亏,那谁亏了呢!
题目如下:
解题思路
循环,当瓶子的数目大于交换的数目时,我们可以换取一瓶,相应我们扣除换取新瓶子的个数后 瓶子的个数还需增加1新瓶子。直到剩下的瓶子再也不够换取结束。
// python3
class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
bottles, ans = numBottles, numBottles
while(bottles >= numExchange):
ans+=1
bottles -=numExchange
bottles +=1
return ans;