how do you calculate the minimum-coin change for transaction?

后端 未结 3 1642
面向向阳花
面向向阳花 2021-01-27 13:43

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

Now what code would I use? I have it

3条回答
  •  灰色年华
    2021-01-27 13:57

    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.

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

    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.


    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
    

    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.


    In response to your update:

    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"
    

提交回复
热议问题