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)
应该没错吧。。。求大神指教