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!