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 次查看(过去 30 天)
显示 更早的评论
%variables
Amount = X
Notes = [50 , 20 , 10 , 5];
Coins = [2,1,0.50,0.20,0.10,0.05,0.01];
1 个评论
Stephen23
2020-12-2
Implement one of the algorithms given here:
The change-giving algorithm is a specific case of this:
回答(1 个)
Stephan
2020-12-2
编辑:Stephan
2020-12-2
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))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Programming and Mixed-Integer Linear Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!