Design an algorithm and write the program for a machine that must give the current amount of change from rs500 for any amount of change costing less than rs 500. The program must specify the number and all notes and coins as possible. Assume purc

1 次查看(过去 30 天)

采纳的回答

Stephen23
Stephen23 2016-3-17
编辑:Stephen23 2016-3-17
Here is a simple greedy algorithm, based on Geoff's answer:
function chg = change(num)
den = [200,100,50,20,10,5,2,1,0.5,0.2,0.1,0.05];
vec = int32(100*den);
lft = int32(100*num);
tmp = zeros(size(vec),'int32');
for v = 1:numel(vec)
tmp(v) = idivide(lft, vec(v));
lft = mod(lft, vec(v));
end
chg(:,2) = den;
chg(:,1) = double(tmp);
end
You should be able to adapt its output to suit your homework. You will have to change the denominations to suit your requirements, and also consider how to display the output (hint: use fprintf). Currently the functions simply returns a matrix showing how many of each denomination are used:
>> change(499)
ans =
2 200
0 100
1 50
2 20
0 10
1 5
2 2
0 1
0 0.5
0 0.2
0 0.1
0 0.05
>> change(99)
ans =
0 200
0 100
1 50
2 20
0 10
1 5
2 2
0 1
0 0.5
0 0.2
0 0.1
0 0.05
>> change(1.75)
ans =
0 200
0 100
0 50
0 20
0 10
0 5
0 2
1 1
1 0.5
1 0.2
0 0.1
1 0.05

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Particle Swarm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by