第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

貪心算法無法正常運(yùn)行

貪心算法無法正常運(yùn)行

互換的青春 2022-10-18 16:55:55
這段代碼應(yīng)該給我最少數(shù)量的硬幣(25 美分硬幣、5 美分硬幣、5 美分硬幣和 1 美分硬幣),這些硬幣加起來等于所欠的金額。當(dāng)我輸入的值是 0.25 的倍數(shù)時(shí),它可以無縫工作。但是當(dāng)我在終端中輸入其他值時(shí),它只是插入一個(gè)新行而不做任何事情。我怎么搞砸了?owed = float(input("how much change is owed?"))coins = 0if 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()
查看完整描述

2 回答

?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

while由于浮點(diǎn)數(shù)的內(nèi)部表示引起的與浮點(diǎn)值相關(guān)的一些舍入錯(cuò)誤,您的程序正在運(yùn)行無限循環(huán)。因此,要修復(fù)這些錯(cuò)誤,我們可以使用round()函數(shù)在每次循環(huán)迭代owed結(jié)束時(shí)對(duì)值進(jìn)行四舍五入:while


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()

這很好用。


希望這可以幫助 :)


查看完整回答
反對(duì) 回復(fù) 2022-10-18
?
浮云間

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊

while由于浮點(diǎn)錯(cuò)誤,您的程序卡在循環(huán)中。嘗試在while循環(huán)中添加以下代碼,您會(huì)看到 whileowed確實(shí)變得無限小,它永遠(yuǎn)不會(huì)變?yōu)榱悖?/p>


...

while owed > 0:

    print(owed)

    ...


輸出:


...

8.326672684688674e-17

8.326672684688674e-17

8.326672684688674e-17

8.326672684688674e-17

...

考慮將輸入乘以100然后將其作為整數(shù)處理:


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)

或者,如果您想堅(jiān)持使用貪心算法:


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

有關(guān)浮點(diǎn)限制的更多信息,請(qǐng)查看以下內(nèi)容:https ://docs.python.org/3.8/tutorial/floatingpoint.html


查看完整回答
反對(duì) 回復(fù) 2022-10-18
  • 2 回答
  • 0 關(guān)注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)