I'm looking for the most efficient way to figure out a change amount (Quarters, dimes, nickels, and pennies) from a purchase amount. The purchase amount must be less than
I'm looking for the most efficient way to figure out a change amount (Quarters, dimes, nickels, and pennies) from a purchase amount. The purchase amount must be less than $1, and the change is from one dollar. I need to know how many quarters, dimes, nickels, and pennies someone would get back.
我正在寻找最有效的方法来计算购买金额的变化金额(季度,硬币,镍币和硬币)。购买金额必须低于1美元,更改金额为1美元。我需要知道有多少人,硬币,镍币和便士有人会回来。
Would it be best to set up a dictionary?
设置字典会是最好的吗?
5 个解决方案
#1
Gee, you mean this isn't problem 2b in every programming course any more? Eh, probably not, they don't seem to teach people how to make change any more either. (Or maybe they do: is this a homework assignment?)
哎呀,你的意思是这不是每个编程课程中的问题2b了吗?呃,可能不是,他们似乎也不会教人们如何改变。 (或许他们这样做:这是家庭作业吗?)
If you find someone over about 50 and have them make change for you, it works like this. Say you have a check for $3.52 and you hand the cashier a twnty. They'll make change by saying "three fifty-two" then
如果你找到一个超过50岁的人并让他们为你做出改变,那就像这样。假设你有一张3.52美元的支票,你可以把收银员交给他二十美元。他们会通过说“三五二”来改变
- count back three pennies, saying "three, four, five" (3.55)
- count back 2 nickels, (3.60, 3.65)
- count back a dime (3.75)
- a quarter (4 dollars)
- a dollar bill (five dollars)
- a $5 bill (ten dollars)
- a $10 bill (twenty.)
算三个便士,说“三,四,五”(3.55)
倒数2个镍币,(3.60,3.65)
算一毛钱(3.75)
四分之一(4美元)
一美元钞票(五美元)
5美元的钞票(10美元)
10美元的钞票(二十美元)
That's at heart a recursive process: you count back the current denomination until the current amount plus the next denomination comes out even. Then move up to the next denomination.
这是一个递归过程的核心:你倒数当前面额,直到当前金额加上下一个面额出现。然后上升到下一个面额。
You can, of course, do it iteratively, as above.
当然,您可以如上所述迭代地执行此操作。
#2
This is probably pretty fast - just a few operations per denomination:
这可能非常快 - 每个面额只有几个操作:
def change(amount):
money = ()
for coin in [25,10,5,1]
num = amount/coin
money += (coin,) * num
amount -= coin * num
return money
#3
Your best bet is to probably have a sorted dictionary of coin sizes, and then loop through them checking if your change is greater than the value, add that coin and subtract the value, otherwise move along to the next row in the dictionary.
您最好的选择是可能有一个硬币大小的排序字典,然后循环检查您的更改是否大于该值,添加该硬币并减去该值,否则移动到字典中的下一行。
Eg
Coins = [50, 25, 10, 5, 2, 1]
ChangeDue = 87
CoinsReturned = []
For I in coins:
While I >= ChangeDue:
CoinsReturned.add(I)
ChangeDue = ChangeDue - I
Forgive my lousy python syntax there. Hope that's enough to go on.
请原谅我糟糕的python语法。希望这足以继续下去。
#4
This problem could be solved pretty easy with integer partitions from number theory. I wrote a recursive function that takes a number and a list of partitions and returns the number of possible combinations that would make up the given number.
使用数论的整数分区可以很容易地解决这个问题。我写了一个递归函数,它接受一个数字和一个分区列表,并返回组成给定数字的可能组合的数量。
https://sandboxrichard.blogspot.com/2009/03/integer-partitions-and-wiki-smarts.html
It's not exactly what you want, but it could be easily modified to get your result.
这不完全是你想要的,但它可以很容易地修改,以获得你的结果。
#5
The above soloution working.
以上解决方案工作。
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)
Alternatively a solution can reached by using the floor and mod functions.
或者,可以通过使用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 )
Or another solution
或另一种解决方案
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" )
, and the change is from one dollar. I need to know how many quarters, dimes, nickels, and pennies someone would get back. I'm looking for the most efficient way to figur