Find minimum number of coins that make a given value

From Rosetta Code
Revision as of 01:15, 9 August 2021 by CalmoSoft (talk | contribs) (Created page with "{{Draft task}} ;Task:Find minimum number of coins that make a given value <br> coins = [1,2,5,10,20,50,100,200] <br> value = 988 <br> <br>Coins are: <br> 4*200 <br> 1*100 <br...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Find minimum number of coins that make a given value is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task
Find minimum number of coins that make a given value


coins = [1,2,5,10,20,50,100,200]
value = 988

Coins are:
4*200
1*100
1*50
1*20
1*10
1*5
1*2
1*1

Ring

<lang ring> load "stdlib.ring"

see "working..." + nl see "Coins are:" + nl sum = 988

sumCoins = 0 coins = [1,2,5,10,20,50,100,200] coins = reverse(coins)

for n = 1 to len(coins)

   nr = floor(sum/coins[n])
   if nr > 0
      sumCoins= nr*coins[n]
      sum -= sumCoins    
      see "" + nr + "*" + coins[n] + nl
   ok

next

see "done..." + nl </lang>

Output:
working...
Coins are:
4*200
1*100
1*50
1*20
1*10
1*5
1*2
1*1
done...