"change-making" algorithm - Smallest amount of currencies possible

Asked

Viewed 40 times

0

Given the Change-Making algorithm:

change(C1, C2, ...., Cr: valores de denominações de moedas, onde C1 > C2 > ... > Cr; n: inteiro positivo)


for i := 1 para r
     di := 0 {di conta a denominação das moedas Ci usadas}
     while n >= Ci
          di := di + 1 {adiciona uma moeda na denominação Ci}
          n := n - Ci
{di é o número de moedas na denominação Ci na troca for i = 1, 2, ...}

And the question:
Consider the algorithm for making the exchange and the pattern of American currencies ($0.25, $0.10, $0.05, and $0.01). Show that if we enter $0.12, then the algorithm will not produce an exchange using the least amount of currencies possible.


I would like to ask for help to understand the logic or where to start, because I am lost on that.

Sorry for the translation a little "rude" because I’m studying this in English and I don’t know the correct terms in en.

  • 2

    Hello, welcome to Sopt. Do you have a specific question about any approach you have tried? The way the question is, it seems like you want someone to demonstrate what you were asked for (would it be a college exam?), but that’s not the purpose of this site.

  • It’s not a test, it’s just training exercises. I just don’t understand the logic, and I have no idea where to start, I just need help with that.

  • Well, edit the question to put your difficulty in a specific way. I would say debug mentally (the famous "table test") helps you understand the problem. Ignoring the fact that your n is an integer (and therefore cannot receive 0.12), you will notice the problem as soon as you arrive at the first loop interaction (at the first execution of the while): the variable n = 0.12 will not be greater than or equal to the first and largest currency value C1 = 0.25, and therefore the execution will be immediately interrupted.

  • All right, I got the idea in my head! Thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.