其他分享
首页 > 其他分享> > 啤酒2元一瓶,2个空瓶能换一瓶,4个空瓶能换一瓶. 问: 10元最多能喝多少瓶?

啤酒2元一瓶,2个空瓶能换一瓶,4个空瓶能换一瓶. 问: 10元最多能喝多少瓶?

作者:互联网

class Beer(object):
    def __init__(self, money, price_money, price_bottle, price_cover):
        self.money = money     # 钱数
        self.price_money = price_money   # 每瓶花的钱
        self.price_bottle = price_bottle  # 一瓶值的空瓶数
        self.price_cover = price_cover   # 一瓶值的瓶盖数
        self.beer = self.money // self.price_money
        self.bottle = self.beer   # 瓶子
        self.cover = self.bottle   # 盖子

    def __repr__(self):
        return f'喝酒数:{self.beer}, 剩下空瓶数:{self.bottle}, 剩下瓶盖数:{self.cover}'

    def bottle_to_beer(self):
        while self.bottle >= self.price_bottle:
            add = self.bottle // self.price_bottle
            self.beer += add
            self.bottle += add
            self.cover += add
            self.bottle -= self.price_bottle * add

    def cover_to_beer(self):
        while self.cover >= self.price_cover:
            add = self.cover // self.price_cover
            self.beer += add
            self.bottle += add
            self.cover += add
            self.cover -= self.price_cover * add


if __name__ == '__main__':
    beer = Beer(money=10, price_money=2, price_bottle=2, price_cover=4)
    while beer.bottle >= 2 or beer.cover >= 4:
        beer.bottle_to_beer()
        beer.cover_to_beer()
    print(beer)

  

标签:money,price,cover,beer,一瓶,bottle,能换,空瓶,self
来源: https://www.cnblogs.com/wangtaobiu/p/13355579.html