Hi there, can anyone help me create a function which calculates the least amount of notes and coins needed for any amount entered in ATM please ?

3 visualizaciones (últimos 30 días)
%variables
Amount = X
Notes = [50 , 20 , 10 , 5];
Coins = [2,1,0.50,0.20,0.10,0.05,0.01];

Respuestas (1)

Stephan
Stephan el 2 de Dic. de 2020
Editada: Stephan el 2 de Dic. de 2020
You can treat it as an optimization problem - since intlinprog is used you can be sure to find optimal solutions:
Amount = input('\n\nInput Amount: ');
Notes = [50 , 20 , 10 , 5];
Coins = [2,1,0.50,0.20,0.10,0.05,0.01];
Money = [Notes, Coins];
n = numel(Money);
f = ones(n,1);
intcon = 1:n;
A = [];
b = [];
Aeq = Money;
beq = Amount;
lb = zeros(1,n);
ub = inf(1,n);
options = optimoptions('intlinprog','Display','off');
sol = intlinprog(f, intcon, A, b, Aeq, beq, lb, ub, [], options);
fprintf('\nMoney:\n')
fprintf('\n%d Notes: %u',[Notes; ceil(sol(1:numel(Notes)))'])
fprintf('\n%1.2g Coins: %d',[Coins; ceil(sol(numel(Notes)+1:end))'])
fprintf('\n\nCalculated Amount: %g\n\n',Money*double(sol))

Categorías

Más información sobre Linear Programming and Mixed-Integer Linear Programming en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by