Python功能:从购买金额查找更改 [英] Python function: Find Change from purchase amount

查看:117
本文介绍了Python功能:从购买金额查找更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找最有效的方式来从购买金额中找出变化金额(季度,数量,镍价和便士)。购买金额必须少于1美元,更改为1美元。我需要知道有人会回来多少个季度,数量,镍币和便士。

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.

最好设置字典?

推荐答案

你的意思是这不是每个编程课程中的2b问题吗?呃,可能不是,他们似乎没有教人们如何改变任何一个。 (或者也许他们这样做:这是一个家庭作业吗?)

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

如果你找到超过50岁的人,让他们为你改变,它的工作原理就是这样。说你有一张支票$ 3.52,你把收银员交给了一个twnty。他们会改变说三五二,然后

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


  • 返回三便士,说三,四,五( 3.55)

  • 退回2镍,(3.60,3.65)

  • 计算一毛钱(3.75)

  • < (4美元)
  • 一美元(5美元)

  • a $ 5(10美元)

  • a $ 10 bill(二十)

  • 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.)

这是一个递归过程:你计算当前面值,直到当前数额加下一个面额出来甚至。然后移动到下一个面额。

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.

这篇关于Python功能:从购买金额查找更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆