我想写一个计算机器给客户的变化的函数。该功能接收产品的成本,客户提供的金额以及包含机器所用金额的字典。
该功能应该提供最小的硬币和账单,并考虑到可用的资金。
还应该避免任何一种钱用尽;例如,如果它有10€中的2和5€中的8,则不应在同一更改中使用10€中的2。
这是我的字典:
d = {0.01: 10,
0.02: 5,
0.05: 2,
0.1: 10,
0.2: 5,
0.5: 2,
1: 5,
2: 5,
5: 2,
10: 4,
20: 5,
50: 1,
100: 0,
200: 0,
500: 0,
}
这是我目前的代码:
def change(cost, given, d):
last_change = 0
change = given - cost #calculates how much we own the customer
if change == 0: #if we don't own the customer anything
return change
else:
if change in d and d[change] != 0: #if change is in the dictionary and its value is not 0 we can give it to the customer
return change
else:
euro = int(change)
cent = change - euro #calculates if we have to give any cents
if cent == 0: #if we only have to give bills
for item in d:
if item > last_change and item < change and d[item] != 0: #biggest bill we can give the customer
last_change = item
我不知道接下来该做什么。
答案 0 :(得分:1)
from math import *
dict1 = {0.01: 10,
0.02: 5,
0.05: 2,
0.1: 10,
0.2: 5,
0.5: 2,
1: 5,
2: 5,
5: 2,
10: 4,
20: 5,
50: 1,
100: 0,
200: 0,
500: 0,
}
def change(cost, given, dict1):
last_change = 0
change = given - cost
if change == 0:
print change
else:
if change in dict1 and dict1[change] != 0:
print change
else:
if change >= 500 and dict1[change] != 0:
a = floor( change / 500 )
print a, " --500 's"
change = change - ( a * 500 )
if change >= 200 and dict1[change] != 0:
b = floor( change / 200 )
print b, " --200 's"
change = change - ( b * 200 )
if change >= 100 and dict1[change] != 0:
c = floor( change / 100 )
print c, " --100 's"
change = change - ( c * 100 )
if change >= 50 and dict1[change] != 0:
d = floor( change / 50 )
print d, " --50 's"
change = change - ( d * 50 )
if change >= 20 and dict1[change] != 0:
e = floor( change / 20 )
print e, " --20 's"
change = change - ( e * 20 )
if change >= 10 and dict1[change] != 0:
f = floor( change / 10 )
print f, " --10 's"
change = change - ( f * 20 )
if change >= 5 and dict1[change] != 0:
g = floor( change / 5 )
print g, " --5 's"
change = change - ( g * 5 )
if change >= 2 and dict1[change] != 0:
h = floor( change / 2 )
print h, " --2 's"
change = change - ( h * 2 )
if change >= 1 and dict1[change] != 0:
i = floor( change / 1 )
print i, " --1 's"
change = change - ( i * 1 )
if change >= 0.5 and dict1[change] != 0:
j = floor( change / 0.5 )
print j, " --0.5 's"
change = change - ( j * 0.5 )
if change >= 0.2 and dict1[change] != 0:
k = floor( change / 0.2 )
print k, " --0.2 's"
change = change - ( k * 0.2 )
---------------------------
---------------------------
---------------------------
---------------------------
implement similar steps for 0.1,0.05,0.02,0.01
输出将如下:
1 - 500 's
2 - 200 's
2 - 100 's
1 - 20 's
答案 1 :(得分:0)
这是一些伪代码 - 您需要填写一些细节:
amount = ... the input amount ...
change = {}
for denomination in [500, 200, 100, 50, ...]:
if amount == 0:
break
n = ... number of coins of this denomination to use
change[denomination] = n # store it
amount = amount - n*denomination # subtract from amount
# the dictionary change contains how to make the change