【问题标题】:Greedy algorithm not functioning properly贪心算法无法正常运行
【发布时间】:2020-08-03 20:29:26
【问题描述】:

这个代码应该给我最少数量的硬币(硬币、硬币、镍和便士),加起来就是所欠的金额。 当我输入 0.25 的倍数的值时,它可以无缝工作。但是当我在终端中输入其他值时,它只是插入一个新行而不做任何事情。我怎么搞砸了?

owed = float(input("how much change is owed?"))
coins = 0

if owed % 0.25 == 0:
    coins = owed / 0.25
    print(int(coins))
    exit()
elif owed % 0.25 != 0:
    while owed > 0:
        if (owed - 0.25) >= 0:
            coins += 1
            owed -= 0.25
        elif (owed - 0.10) >= 0:
            coins += 1
            owed -= 0.10
        elif (owed - 0.05) >= 0:
            coins += 1
            owed -= 0.05
        elif (owed - 0.01) >= 0:
            coins += 1
            owed -= 0.01
    print(int(coins))
    exit()

【问题讨论】:

  • 以下两个答案都建议保留float 以获取输入(GrandPhuba 至少只使用一次)。一般来说,我建议直接进入 int,如果需要,请执行 "".split('.') 并将美元乘以 100 以获得美分。

标签: python python-3.x while-loop


【解决方案1】:

由于浮点数的内部表示引起的与浮点值相关的一些舍入错误,您的程序正在运行无限 while 循环。因此,为了修复这些错误,我们可以使用round() 函数在每个while 循环迭代结束时将owed 的值四舍五入:

elif owed % 0.25 != 0:
    while owed > 0:
        if (owed - 0.25) >= 0:
            coins += 1
            owed -= 0.25
        elif (owed - 0.10) >= 0:
            coins += 1
            owed -= 0.10
        elif (owed - 0.05) >= 0:
            coins += 1
            owed -= 0.05
        elif (owed - 0.01) >= 0:
            coins += 1
            owed -= 0.01
        owed = round(owed, 3) # In this line, we roundoff the value of owed
    print(int(coins))
    exit()

这很好用。

希望这会有所帮助:)

【讨论】:

    【解决方案2】:

    由于浮点错误,您的程序卡在while 循环中。尝试在while 循环中添加以下代码,您会看到虽然owed 确实变得无限小,但它永远不会变为零:

    ...
    while owed > 0:
        print(owed)
        ...
    
    

    输出:

    ...
    8.326672684688674e-17
    8.326672684688674e-17
    8.326672684688674e-17
    8.326672684688674e-17
    ...
    

    考虑将输入乘以100,然后将其作为整数处理:

    owed = int(float(input("How much change is owed? $")) * 100)
    
    quarters = int(owed / 25)
    dimes = int((owed - quarters * 25) / 10)
    nickels = int((owed - quarters * 25 - dimes * 10) / 5)
    cents = int((owed - quarters * 25 - dimes * 10 - nickels * 5))
    
    coins = (quarters + dimes + nickels + cents)
    
    print('Quarters (${}): {}'.format(quarters*0.25, quarters))
    print('Dimes (${}): {}'.format(dimes*0.1, dimes))
    print('Nickels (${}): {}'.format(nickels*0.05, nickels))
    print('Cents (${}): {}'.format(cents, cents))
    print('Coins:', coins)
    

    或者如果你想坚持使用贪心算法:

    owed = int(float(input("How much change is owed? $")) * 100)
    
    while owed > 0:
        if (owed - 25) >= 0:
            coins += 1
            owed -= 25
        elif (owed - 10) >= 0:
            coins += 1
            owed -= 10
        elif (owed - 5) >= 0:
            coins += 1
            owed -= 5
        elif (owed - 1) >= 0:
            coins += 1
            owed -= 1
    
    coins = (quarters + dimes + nickels + cents)
    
    print('Quarters (${}): {}'.format(quarters*0.25, quarters))
    print('Dimes (${}): {}'.format(dimes*0.1, dimes))
    print('Nickels (${}): {}'.format(nickels*0.05, nickels))
    print('Cents (${}): {}'.format(cents, cents))
    print('Coins:', coins)
    

    输出

    >>> How much change is owed? $1.42
    Quarters ($1.25): 5
    Dimes ($0.1): 1
    Nickels ($0.05): 1
    Cents ($2): 2
    Coins: 9
    

    有关浮点限制的更多信息,请查看以下内容:https://docs.python.org/3.8/tutorial/floatingpoint.html

    【讨论】:

    • 非常感谢,我将数字四舍五入到 3 位,解决了。此外,该文档适用于 Python 2。
    • @NourHabra 你是对的,更新了链接,尽管想法保持不变。请注意,舍入浮点数通常不是处理此问题的最佳方法,因为虽然舍入确实可以防止您陷入无限循环,但它只是掩盖了内部错误,这使您的程序容易受到某些极端情况的影响
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2022-12-07
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    相关资源
    最近更新 更多