Prosedura
Procedure uncle(k, p, s, n : longint); {uncle – amaki}
begin
if n = 1 then write(s)
else
begin
k := k + 1;
p := 2*p + k;
uncle(k, p, s + p, n - 1)
end
end;
Proseduraning rasmiy parametrlariga boshlang’ich qiymatlar beriladi: k:=1; p:=1; s:=1; n – foydalanovchi tomonidan asosiy dastur orqali kiritiladi (e’tibor bergan bo’lsangiz, bu prosedurada oldingisidagidek chiquvchi parametrlar va proseduraning o’zida o’zgaruvchilar yo’q, lekin bu holatni o’zgartirish mumkin).
Yoshlar soni ortib boradi: k := k + 1; k-nchi tug’ilgan kunga berilgan sovg’a miqdori hisoblanadi: p := 2*p + k; s olingan pullar yig’indisini p ga orttiradigan va tug’ilgan kunlar sonini 1 ga kamaytiradigan prosedura chaqiriladi:
uncle(k, p, s + p, n - 1)
So’ngra n 1 ga teng bo’lib qolmagunicha butun jarayon takrorlanadi.
Dastur
Program L110; { rich man - boy}
uses Crt;
var
n : integer;
Procedure uncle(k, p, s, n : longint); {uncle - amaki}
begin
if n = 1 then write(s)
else
begin
k := k + 1;
p := 2*p + k;
uncle(k, p, s + p, n - 1)
end
end;
begin
write('Jiyanning yoshlar sonini kiriting'); readln(n);
write('Men', n, '-chi tug’ilgan kunimga ');
uncle(1, 1, 1, n);
writeln(' dollar olaman')
end.
Shartning ikkinchi qismida olingan pullar yig’indisi 100 dollarga teng bo’lganda yoki 100 dollardan oshib ketganida yoshlar sonini aniqlash kerak. Buning uchun prosedurada tayanch sharti o’zgaradi:if s >= 100 then write(n), qolgan hamma narsa esa o’zgartirilmaydi.
Program L110a;
uses Crt;
var
n : integer;
Procedure uncle1(k, p, s, n : longint);
Do'stlaringiz bilan baham: |