Python 函数:从购买金额中查找零钱

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/621062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:29:28  来源:igfitidea点击:

Python function: Find Change from purchase amount

pythondictionarycoin-change

提问by Jb.

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?

最好设置一本字典吗?

回答by Charlie Martin

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 美元的支票,你递给收银员一个 200 美元。然后他们会说“三点五十二分”来改变

  • 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 美元的钞票(二十)

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.

当然,您可以像上面那样反复进行。

回答by Triptych

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

回答by Ben Hayden

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.

使用数论中的整数分区可以很容易地解决这个问题。我编写了一个递归函数,它接受一个数字和一个分区列表,并返回构成给定数字的可能组合数。

http://sandboxrichard.blogspot.com/2009/03/integer-partitions-and-wiki-smarts.html

http://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.

这不完全是您想要的,但可以轻松修改以获得您的结果。

回答by user3234931

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

回答by Matthew Steeples

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 语法。希望这足以继续下去。