你怎么计算的最小硬币找零的交易? [英] how do you calculate the minimum-coin change for transaction?

查看:142
本文介绍了你怎么计算的最小硬币找零的交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿大家。我有一个问题。我工作的Visual Basic防爆preSS和我应该来计算交易的变化。

Hey everyone. I have a question. I am working on Visual Basic Express and I am supposed to calculate the change from a transaction.

现在什么code,我会用?我有这部分工作,但它开始变得有点混乱。

Now what code would I use? I have it partly working but its starting to get a little confusing.

感谢你。

有关你们谁想要了解更多信息:

For you guys who wanted more information:

说我有一块钱,我去商店购买的东西。我要问的用户将在他们花费的时间量,然后计算的变化,并打印到屏幕上。

Say I have one dollar and I go to the store to purchase something. I have to ask the user to put in the amount they spent and then calculate the change and print to the screen.

然后我应该用最少数量的宿舍,硬币镍和硬币,并将其打印到屏幕上。

Then I am supposed to use the least number of quarters, dimes nickels and pennies and print it to screen.

任何帮助将是很大的AP preciated。

Any help would be greatly appreciated.

推荐答案

我要出去肢体这里假设OP在谈论变化从一个交易回来的钱。

I'm going to go out on a limb here and assume the OP is talking about change as in money returned from a transaction.

如果是这样的话,那么它可能的功课,所以伪code而已。

If that's the case, then it's probably homework, so pseudo-code only.

做如下的简单的一尝试的方式。让成本是交易成本和招标是移交(无论是在美分)的金额,和让我们进一步假设你的经济只有美钞,宿舍和便士(让我的code较小)。

The simplest first-attempt way of doing it is as follows. Let cost be the cost of the transaction and tendered be the amount of money handed over (both in cents), and let's further assume your economy only has dollar bills, quarters and pennies (to make my code smaller).

change = tendered - cost

if change < 0:
    print "Pay up some more cash, cheapskate!"
    stop

dollars = 0
quarters = 0
cents = 0

while change >= 100:
    dollars = dollars + 1
    change = change - 100

while change >= 25:
    quarters = quarters + 1
    change = change - 25

while change >= 1:
    cents = cents + 1
    change = change - 1

print dollars " dollar(s), " quarters " quarter(s), and " cents " cent(s)."

现在这无疑可取得与使用模和除法运营商更有效,但我离开,作为一个练习留给读者。

Now this can no doubt be made more efficient with the use of modulo and divide operators but I leave that as an exercise for the reader.

我的建议是坐下来用铅笔和一张纸有以下的列(作为移交十块钱一两美元和-93购买以上):

My suggestion is to sit down with a pencil and a bit of paper with the following columns (for handing over ten dollars for a two-dollar-and-ninety-three cent purchase):

tendered      cost    change   dollars  quarters     cents
--------  --------  --------  --------  --------  --------
    1000       293

和通过在你的头上行的的code线运行,的从纸张使用当前值并记下新的价值,他们改变。

and run through the code line by line in your head, using the current values from the paper and writing down the new values where they change.

这将大大有助于您的理解。

This will greatly assist your understanding.

在回应您的更新:

我有一块钱,我去商店购买的东西。我要问的用户将在他们花费的时间量,然后计算的变化,并打印到屏幕上。那么我应该使用次数最少的季度,助攻,镍币和硬币然后将其打印到屏幕上。

I have one dollar and I go to the store to purchase something. I have to ask the user to put in the amount they spent and then calculate the change and print to the screen. Then I am supposed to use the least number of quarters, dimes, nickels and pennies then print it to screen.

这是非常相似,我有上面:

That's remarkably similar to what I had above:

tendered = 100
input cost
cost = int (cost * 100)
change = tendered - cost
if change < 0:
    print "Pay up some more cash, cheapskate!"
    stop
print "Change is ", (format "$9.99", change / 100)

quarters = 0, dimes = 0, nickels = 0, pennies = 0

while change >= 25:
    quarters = quarters + 1
    change = change - 25

while change >= 10:
    dimes = dimes + 1
    change = change - 10

while change >= 5:
    nickels = nickels + 1
    change = change - 5

while change >= 1:
    pennies = pennies + 1
    change = change - 1

print quarters, " quarters"
print dimes   , " dimes"
print nickels , " quarters"
print pennies , " pennies"

这篇关于你怎么计算的最小硬币找零的交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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