题目: 酒馆做活动,1块钱喝一瓶啤酒,2个空瓶可以兑换一瓶啤酒,4个瓶盖可以兑换一瓶啤酒。如果有15块钱,最多可以喝多少瓶啤酒? 写个程序来计算,输入n元钱,输出可以喝多少
题目:
酒馆做活动,1块钱喝一瓶啤酒,2个空瓶可以兑换一瓶啤酒,4个瓶盖可以兑换一瓶啤酒。如果有15块钱,最多可以喝多少瓶啤酒? 写个程序来计算,输入n元钱,输出可以喝多少瓶酒的数量(不允许借用)
total = 0 # 最终喝多少left_bottle = 0 # 剩余的瓶子
left_cap = 0 # 剩余的盖子
def cal_num(bottle, bottle_gaizi):
global total, left_bottle, left_cap
beers = 0
# 瓶子,盖 换酒
jiu_gai, yu_gai = divmod(bottle_gaizi, 4)
jiu_ping, yu_jiu_ping = divmod(bottle, 2)
beers += (jiu_ping + jiu_gai)
total += beers
bottle_gaizi = yu_gai + beers # 剩余的盖子+ 啤酒
bottle = yu_jiu_ping + beers # 剩余的瓶子+ 啤酒
if bottle_gaizi // 4 > 0 or bottle // 2 > 0:
cal_num(bottle, bottle_gaizi)
else:
left_bottle = bottle
left_cap = bottle_gaizi
if __name__ == '__main__':
money = 15
total, left_bottle, left_cap = money // 1, money // 1, money // 1
cal_num(left_bottle, left_cap)
print(total, left_bottle, left_cap)