Find minimum number of coins that make a given value: Difference between revisions

m
no edit summary
(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...")
 
mNo edit summary
Line 14:
1*1
<br><br>
 
 
=={{header|Julia}}==
Using a linear optimizer for this is serious overkill, but why mot?
<lang julia>using JuMP, GLPK
 
model = Model(GLPK.Optimizer)
@variable(model, ones, Int)
@variable(model, twos, Int)
@variable(model, fives, Int)
@variable(model, tens, Int)
@variable(model, twenties, Int)
@variable(model, fifties, Int)
@variable(model, onehundreds, Int)
@variable(model, twohundreds, Int)
@constraint(model, ones >= 0)
@constraint(model, twos >= 0)
@constraint(model, fives >= 0)
@constraint(model, tens >= 0)
@constraint(model, twenties >= 0)
@constraint(model, fifties >= 0)
@constraint(model, onehundreds >= 0)
@constraint(model, twohundreds >= 0)
@constraint(model, 988 == 1ones +2twos + 5fives + 10tens + 20twenties + 50fifties + 100onehundreds + 200twohundreds)
 
@objective(model, Min, ones + twos + fives + tens + twenties + fifties + onehundreds + twohundreds)
 
optimize!(model)
println("Optimized totasl coins: ", objective_value(model))
for val in [ones, twos, fives, tens, twenties, fifties, onehundreds, twohundreds]
println("Value of ", string(val), " is ", value(val))
end
</lang>{{out}}
<pre>
Optimized totasl coins: 11.0
Value of ones is 1.0
Value of twos is 1.0
Value of fives is 1.0
Value of tens is 1.0
Value of twenties is 1.0
Value of fifties is 1.0
Value of onehundreds is 1.0
Value of twohundreds is 4.0
</pre>
 
 
=={{header|Ring}}==
4,108

edits