Blue Bridge Cup Daily Question (13): beer and beverages (python)

Topic:

啤酒每罐2.3元,饮料每罐1.9元。小明买了若干啤酒和饮料,一共花了82.3元。
我们还知道他买的啤酒比饮料的数量少,请你计算他买了几罐啤酒。
注意:答案是一个整数。

Solution_1:

Perform two traversals directly to
satisfy that the sum of the price is equal to the requirement
and the quantity of beer is less than the quantity of beverages,
that is, the quantity of beer output is the result

Code_1:

for i in range(50):
    for j in range(50):
        if i * 2.3 + j * 1.9 == 82.3 and i < j:
            print(i)

Solution_2:

Use recursion to solve this problem.
Because floating-point numbers perform remainder operations will produce errors
that are difficult to correct, add an order of magnitude to the data to
ensure that the numbers are in integer form.

Code_2:

def beer(beers, price):
    if price % 19 == 0 and beers < (price / 19):
        return beers
    return beer(beers + 1, price - 23)


print(beer(0, 823))

Answer:

11

Guess you like

Origin blog.csdn.net/weixin_50791900/article/details/112813834