zoukankan      html  css  js  c++  java
  • 啤酒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)
    

      

  • 相关阅读:
    二叉树的镜像
    判断树B是不是树A的子结构
    LeetCode-21. Merge Two Sorted Lists
    LeetCode-Reverse Linked List I & II
    LeetCode-Permutations & Permutations II
    Linux常用命令
    Mac OS 快捷键
    Git 常用命令
    SVM参数寻优:grid search
    转载:Normal Equation证明及应用
  • 原文地址:https://www.cnblogs.com/wangtaobiu/p/13355579.html
Copyright © 2011-2022 走看看