#3个瓶盖可以兑换一瓶饮料,买了n瓶饮料,一共可以喝到多少瓶
def get_drink(n):
total = 0
left = 0
while n>=3:
total +=n
left += n%3
if left>=3:
total +=left//3
left = left%3
n = n//3
return total
t = get_drink(100)
print(t)
也可以写成:
def get_drink(n):
total = 0
left = 0
while n>=3:
total +=n
left += n%3
if left>=3:
add, left = divmod(left, 3)
total+= add
n = n//3
return total
t = get_drink(1200)
print(t)
也可以写成
def get_drink(n):
total = 0
left = 0
while n>=3:
total +=n
n, left_add = divmod(n, 3)
left += left_add
if left>=3:
total_add, left = divmod(left, 3)
total+= total_add
return total
print(get_drink(100))
注意:
print(5//4)
#结果为1,取到商的整数
print(5/4)
#结果为1.25
a, b = divmod(5, 4)
print(a, b)
#结果为1,1,divmod(a,b)可以取到商的整数