编程语言
首页 > 编程语言> > python+ 酒馆做活动的问题

python+ 酒馆做活动的问题

作者:互联网

题目:
酒馆做活动,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)

标签:gaizi,python,cap,酒馆,jiu,bottle,total,活动,left
来源: https://www.cnblogs.com/a438842265/p/16187247.html