필터 지우기
필터 지우기

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