V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
webjin
V2EX  ›  程序员

这道题算法题, python 怎么实现?

  •  
  •   webjin · 2015-10-31 14:34:29 +08:00 · 1573 次点击
    这是一个创建于 3343 天前的主题,其中的信息可能已经有所发展或是发生改变。
    2 元钱一瓶啤酒, 4 个瓶子可以换 1 瓶啤酒, 2 个盖子可以换一瓶啤酒。问 10 元钱可以喝到多少批啤酒。这个用python怎么写出公式计算?
    aluo1
        1
    aluo1  
       2015-10-31 15:30:11 +08:00
    def how_many_beer(money):
    # The price of the beer
    beer_price = 2

    # The number of beer bought by the given money
    beer_bought = money / beer_price
    beer = beer_bought
    # Bottle and lid number is the same as the beer bought initially
    bottle = beer_bought
    lid = beer_bought

    # Check whether can trade in more beer
    get_more_beer = tradable(bottle, lid)

    while get_more_beer:
    # Get the trade-in result
    on_hand = trade_in(bottle, lid)

    # Get the number of trade-in beer
    more_beer = on_hand["beer"]
    # Beer drunk so far
    beer += more_beer
    bottle = on_hand["bottle"] + more_beer
    lid = on_hand["lid"] + more_beer

    get_more_beer = tradable(bottle, lid)

    return beer


    def tradable(bottle,lid):
    # How many bottle can trade in one beer
    bottle_to_beer = 4
    # How many lid can trade in one beer
    lid_to_beer = 2

    return (bottle / bottle_to_beer) | (lid/lid_to_beer)

    def trade_in(bottle, lid):
    # How many bottle can trade in one beer
    bottle_to_beer = 4
    # How many lid can trade in one beer
    lid_to_beer = 2

    beer_from_bottle = bottle / bottle_to_beer
    beer_from_lid = lid / lid_to_beer
    beer = beer_from_bottle + beer_from_lid

    remained_bottle = bottle % bottle_to_beer
    remained_lid = bottle % lid_to_beer

    on_hand = {"beer": beer,
    "bottle": remained_bottle,
    "lid": remained_lid}

    return on_hand

    print how_many_beer(10)

    应该没错吧。。。求大神指教
    aluo1
        2
    aluo1  
       2015-10-31 15:32:25 +08:00
    def how_many_beer(money):
    # The price of the beer
    beer_price = 2

    # The number of beer bought by the given money
    beer_bought = money / beer_price
    beer = beer_bought
    # Bottle and lid number is the same as the beer bought initially
    bottle = beer_bought
    lid = beer_bought

    # Check whether can trade in more beer
    get_more_beer = tradable(bottle, lid)

    while get_more_beer:
    # Get the trade-in result
    on_hand = trade_in(bottle, lid)

    # Get the number of trade-in beer
    more_beer = on_hand["beer"]
    # Beer drunk so far
    beer += more_beer
    bottle = on_hand["bottle"] + more_beer
    lid = on_hand["lid"] + more_beer

    get_more_beer = tradable(bottle, lid)

    return beer


    def tradable(bottle,lid):
    # How many bottle can trade in one beer
    bottle_to_beer = 4
    # How many lid can trade in one beer
    lid_to_beer = 2

    return (bottle / bottle_to_beer) | (lid/lid_to_beer)

    def trade_in(bottle, lid):
    # How many bottle can trade in one beer
    bottle_to_beer = 4
    # How many lid can trade in one beer
    lid_to_beer = 2

    beer_from_bottle = bottle / bottle_to_beer
    beer_from_lid = lid / lid_to_beer
    beer = beer_from_bottle + beer_from_lid

    remained_bottle = bottle % bottle_to_beer
    remained_lid = bottle % lid_to_beer

    on_hand = {"beer": beer,
    "bottle": remained_bottle,
    "lid": remained_lid}

    return on_hand

    print how_many_beer(10)
    aluo1
        3
    aluo1  
       2015-10-31 15:32:40 +08:00
    为什么空格不对。。。
    webjin
        4
    webjin  
    OP
       2015-10-31 16:19:50 +08:00
    @aluo1 a 好像都允许不起来
    aluo1
        5
    aluo1  
       2015-11-01 05:39:12 +08:00
    @webjin 欸?运行不起来嘛?是不是空格打错了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3054 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 13:38 · PVG 21:38 · LAX 05:38 · JFK 08:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.