購入金額から変更金額 (クォーター、ダイム、ニッケル、およびペニー) を計算する最も効率的な方法を探しています。購入金額は 1 ドル未満で、おつりは 1 ドルからです。誰かがどれだけの 4 セント硬貨、10 セント硬貨、5 セント硬貨、1 セント硬貨を取り戻すかを知る必要があります。
辞書を設定した方がよいでしょうか。
購入金額から変更金額 (クォーター、ダイム、ニッケル、およびペニー) を計算する最も効率的な方法を探しています。購入金額は 1 ドル未満で、おつりは 1 ドルからです。誰かがどれだけの 4 セント硬貨、10 セント硬貨、5 セント硬貨、1 セント硬貨を取り戻すかを知る必要があります。
辞書を設定した方がよいでしょうか。
ええ、これはもうすべてのプログラミング コースの問題 2b ではないということですか? ええ、おそらくそうではありません。彼らは人々に変化の仕方を教えていないようです。(または、そうかもしれません。これは宿題ですか?)
50 歳くらいの人を見つけて両替してもらう場合は、次のようになります。$3.52 の小切手を持っていて、レジ係に 20 ドルを渡したとします。彼らは「3 52」と言って変更を加えます。
これは本質的に再帰的なプロセスです。現在の金額と次の金額が均等になるまで、現在の金額をカウントバックします。その後、次の宗派に移動します。
もちろん、上記のように繰り返し行うこともできます。
これはおそらくかなり高速です。金種ごとに数回の操作のみです。
def change(amount):
money = ()
for coin in [25,10,5,1]
num = amount/coin
money += (coin,) * num
amount -= coin * num
return money
この問題は、数論の整数分割を使用して簡単に解決できます。数値とパーティションのリストを受け取り、指定された数値を構成する可能な組み合わせの数を返す再帰関数を作成しました。
http://sandboxrichard.blogspot.com/2009/03/integer-partitions-and-wiki-smarts.html
それはまさにあなたが望むものではありませんが、結果を得るために簡単に変更できます。
上記の解決策が機能しています。
amount=int(input("Please enter amount in pence"))
coins = [50, 25, 10, 5, 2, 1]
coinsReturned = []
for i in coins:
while amount >=i:
coinsReturned.append(i)
amount = amount - i
print(coinsReturned)
別の方法として、floor 関数と mod 関数を使用して解決策に到達することもできます。
amount = int(input( "Please enter amount in pence" ))
# math floor of 50
fifty = amount // 50
# mod of 50 and floor of 20
twenty = amount % 50 // 20
# mod of 50 and 20 and floor of 10
ten = amount % 50 % 20 // 10
# mod of 50 , 20 and 10 and floor of 5
five = amount % 50 % 20 % 10 // 5
# mod of 50 , 20 , 10 and 5 and floor of 2
two = amount % 50 % 20 % 10 % 5 // 2
# mod of 50 , 20 , 10 , 5 and 2 and floor of 1
one = amount % 50 % 20 % 10 % 5 % 2 //1
print("50p>>> " , fifty , " 20p>>> " , twenty , " 10p>>> " , ten , " 5p>>> " , five , " 2p>>> " , two , " 1p>>> " , one )
または別の解決策
amount=int(input("Please enter the change to be given"))
endAmount=amount
coins=[50,25,10,5,2,1]
listOfCoins=["fifty" ,"twenty five", "ten", "five", "two" , "one"]
change = []
for coin in coins:
holdingAmount=amount
amount=amount//coin
change.append(amount)
amount=holdingAmount%coin
print("The minimum coinage to return from " ,endAmount, "p is as follows")
for i in range(len(coins)):
print("There's " , change[i] ,"....", listOfCoins[i] , "pence pieces in your change" )
あなたの最善の策は、おそらくコインサイズのソートされた辞書を用意し、それらをループして、変更が値より大きいかどうかを確認し、そのコインを追加して値を減算し、そうでない場合は辞書の次の行に移動することです.
例えば
Coins = [50, 25, 10, 5, 2, 1]
ChangeDue = 87
CoinsReturned = []
For I in coins:
While I >= ChangeDue:
CoinsReturned.add(I)
ChangeDue = ChangeDue - I
私のお粗末な python 構文を許してください。続行するのに十分であることを願っています。
上記のソリューションから改善されたソリューションがあります
coins=[]
cost = float(input('Input the cost: '))
give = float(input('Tipe Amount given: '))
change = (give - cost)
change2 = change-int(change)
change2 = round(change2,2)*100
coin = [50,25,10,5,1]
for c in coin:
while change2>=c:
coins.append(c)
change2 = change2-c
half=coins.count(50)
qua = coins.count(25)
dime=coins.count(10)
ni=coins.count(5)
pen=coins.count(1)
dolars = int(change)
if half !=0 or qua != 0 or dime!=0 or ni!=0 or pen!=0:
print ('The change of', round(give,2), 'is:',change, 'like \n-dolas:',dolars,'\n-halfs:',half,'\n-quarters:',qua,'\n-dime:',dime,'\n-nickels:',ni,'\n-pennies:',pen)
else:
print ('The change from', round(give,2), 'is:',change,'and no coins')