Решение, что называется, "в лоб" (далеко, очень далеко, не самое лучшее):
var s,k1,k5,k10,k50,k100,k500,k1000,k10000:integer;
begin
write('s = '); readln(s);
k1:=0; k5:=0; k10:=0; k50:=0;
k100:=0; k500:=0; k1000:=0; k10000:=0;
while s>=10000 do begin s:=s-10000; k10000:=k10000+1; end;
while s>=1000 do begin s:=s-1000; k1000:=k1000+1; end;
while s>=500 do begin s:=s-500; k500:=k500+1; end;
while s>=100 do begin s:=s-100; k100:=k100+1; end;
while s>=50 do begin s:=s-50; k50:=k50+1; end;
while s>=10 do begin s:=s-10; k10:=k10+1; end;
while s>=5 do begin s:=s-5; k5:=k5+1; end;
while s>=1 do begin s:=s-1; k1:=k1+1; end;
writeln('k10000=',k10000,' k1000=',k1000,' k500=',k500,' k100=',k100,' k50=',k50,' k10=',k10,' k5=',k5,' k1=',k1);
writeln(' Всего купюр = ',k1+k5+k10+k50+k100+k500+k1000+k10000);
end.
Пример:
s = 27879
k10000=2 k1000=7 k500=1 k100=3 k50=1 k10=2 k5=1 k1=4
Всего купюр = 21
program t;
var n, i, a, b, c, d, k, m: integer; x1, x2, x3, y1, y2, y3: array[1..10000] of integer; f1, f2: text;
function icl(x, y: integer): boolean;
var i: integer;
begin
icl:=false;
for i:=1 to n do
begin
if (x>=x1[i]) and (y>=y1[i]) and (x<=x2[i]) and (y<=y2[i]) then
begin
icl:=true;
break
end
end
end;
procedure re(st, fn: integer);
var nst, nfn, i, j, jj, xx, yy: integer; eq, ff: boolean;
begin
m:=m+1;
nst:=k+1;
ff:=false;
for i:=st to fn do
begin
for j:=0 to 11 do
begin
if j mod 3=0 then continue;
xx:=x3[i]+trunc(cos(j*pi/6)*3);
yy:=y3[i]+trunc(sin(j*pi/6)*3);
if not icl(xx, yy) then continue;
eq:=false;
for jj:=1 to k do if (xx=x3[jj]) and (yy=y3[jj]) then
begin
eq:=true;
break
end;
if eq then continue;
if (xx=c) and (yy=d) then
begin
ff:=true;
break
end;
k:=k+1;
x3[k]:=xx;
y3[k]:=yy;
end;
if ff then break;
end;
if ff then exit;
nfn:=k;
if nst>nfn then
begin
m:=-1;
exit
end;
re(nst, nfn)
end;
begin
assign(f1, 'horse.in');
reset(f1);
assign(f2, 'horse.out');
rewrite(f2);
readln(f1, n);
for i:=1 to n do readln(f1, x1[i], y1[i], x2[i], y2[i]);
readln(f1, a, b);
readln(f1, c, d);
k:=1;
x3[1]:=a;
y3[1]:=b;
m:=0;
re(1, 1);
writeln(f2, m);
close(f1);
close(f2)
end.