Wklejam uniwersalne rozwiązanie, które będzie dobrze działac przy dowolnych nawet dziwnych nominałach banknotów np 11,13,17,23,:
{$mode objfpc}
const Multiply=10;
const Banknots:array[0..2]of record Value,Count:Word end=
(
( Value: 2; Count:0 ),
( Value: 5; Count:0 ),
( Value:10; Count:0 )
);
function Bankomat(Sum:DWord):Boolean;
var Val:array of Integer;
var I,K:Integer;
var V:Word;
begin
for K:=High(Banknots) downto Low(Banknots) do Banknots[K].Count:=0;
SetLength(Val,Sum+1);
Val[0]:=0;
for I:=1 to Sum do
begin
Val[I]:=-1;
for K:=High(Banknots) downto Low(Banknots) do
begin
V:=Banknots[K].Value;
if (I>=V)and(Val[I-V]>=0) then
begin
Val[I]:=K+1;
Break;
end;
end;
end;
I:=Sum;
K:=Val[I];
Result:=(K>0);
if Result then
begin
while K>0 do
begin
Dec(K);
Inc(Banknots[K].Count);
Dec(I,Banknots[K].Value);
K:=Val[I];
end;
end;
end;
var Sum:Word;
var K:Integer;
var First:Boolean;
begin
for Sum:=0 to 100 do
begin
Write('Sume ',Sum*Multiply,' ');
if Bankomat(Sum) then
begin
Write('wydajemy jako: ');
First:=true;
for K:=High(Banknots) downto Low(Banknots) do
begin
if Banknots[K].Count>0 then
begin
if not First then Write(' + ');
if Banknots[K].Count>1 then Write(Banknots[K].Count,'x');
Write(Banknots[K].Value*Multiply);
First:=false;
end;
end;
WriteLn;
end
else WriteLn('nie da sie wydac');
end;
ReadLn;
end.
Pochodzi z http://komputery.katalogi.pl/pascal-t160461.html