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

2015-10-31 14:34:29 +08:00
 webjin
2 元钱一瓶啤酒, 4 个瓶子可以换 1 瓶啤酒, 2 个盖子可以换一瓶啤酒。问 10 元钱可以喝到多少批啤酒。这个用python怎么写出公式计算?
1572 次点击
所在节点    程序员
5 条回复
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
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
2015-10-31 15:32:40 +08:00
为什么空格不对。。。
webjin
2015-10-31 16:19:50 +08:00
@aluo1 a 好像都允许不起来
aluo1
2015-11-01 05:39:12 +08:00
@webjin 欸?运行不起来嘛?是不是空格打错了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/232514

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX