啤酒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)

  

posted @ 2020-07-21 15:57  屁桃  阅读(527)  评论(0编辑  收藏  举报