I'd like to ask for the change and the number of coins.

Asked 2 years ago, Updated 2 years ago, 116 views

I'd like to ask for the number of change bills and coins.I want to solve it with Ruby, but I don't know how to do it.

The results of a 468 yen purchase and a 10,000 yen bill are as follows.

Number of 5,000 yen bills = 1
Number of 1,000 yen bills = 4
Number of 500 yen coins = 1
Number of 100-yen coins = 0
Number of 50-yen coins = 0
Number of 10-yen coins = 3
Number of 5-yen coins = 0
Number of yen coins = 2

ruby algorithm

2022-09-30 21:35

3 Answers

"I'd like to pay x yen by combining the prepared types of money.Think about solving the question "Which one and how many should I pay to pay while minimizing the number of tickets?" (If you don't mind any number, you can pay more than one yen.)

In the case of the kind of money you offer, this can be sought by the greedy law.In other words, if you pay as much as you can with a large kind of money, and if you can't, you can think of paying with a larger kind of money next time.Proof of this is long enough to write here, so I'll skip it, but if you're interested, please see the page linked to the end of the post.

As a partial example, I will show Ruby's code for calculating "How many 5000 yen bills do you use if you pay as much as you can with a 5000 yen bill and how many yen is left after that?"

$irb
irb(main): 001:0>x=9532# Assume the variable x is substituted for the money you want to pay now.
= > 9532
irb(main): 002:0>n5000=x/5000#5000# You can find out how many 5000 yen bills you can pay by dividing them as integers.Let's substitute the variable n5000.
=>1
irb(main): 003:0>x=x-5000*n5000# You can find the balance by subtracting the amount of the number of sheets you requested above.
=>4532

Also, please note that depending on the type of coin, it may not be solved by greedy method.For more detailed information, please visit the following website (you can find it if you search the coin avarice method, etc.).


2022-09-30 21:35

n5000, amari=(10000-468).divmod(5000)
n1000, amari=amari.divmod(1000)
n500, amari=amari.divmod(500)
n100, amari=amari.divmod(100)
n50, amari=amari.divmod(50)
n10, amari=amari.divmod(10)
n5, n1 = amari.divmod(5)

puts<<EOS
Number of 5,000 yen bills = #{n5000}
Number of 1,000 yen bills = #{n1000}
Number of 500 yen coins = #{n500}
Number of 100-yen coins = #{n100}
Number of 50-yen coins = #{n50}
Number of 10-yen coins = #{n10}
Number of 5-yen coins = #{n5}
Number of yen coins = #{n1}
EOS


2022-09-30 21:35

I'll just give you some tips on how to think (I can't write Ruby in Solar either).

"Total amount of change" can be obtained by subtracting the price of the product from 10,000 yen, right?
Break the "change" you asked for in order with large denomination bills or coins.

9532 55,000=1.9064

If you round off the few and look at the whole number, you will need the number of tickets at the face value (this time "1").
Now that I know 5,000 yen is one ticket, I will subtract it from the original change (the rest is xxx yen).
Next time, I will calculate how many 1,000 yen bills I need in the same way.
If you repeat this at each face value, you will be asked how many tickets you will need at each face value.


2022-09-30 21:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.