Sie sind auf Seite 1von 192

CZU 004(075.

3)
70
Elaborat conform curriculumului disciplinar n vigoare i aprobat prin Ordinul ministrului educaiei al Republicii Moldova (nr. 267 din 11 aprilie 2014). Editat din sursele financiare ale Fondului Special pentru Manuale.
Comisia de evaluare: Gheorghe Curbet, profesor colar, grad didactic superior, Liceul Teoretic Mihai Eminescu, Bli; Arcadie Malearovici, ef direcie, Centrul Tehnologiilor Informaionale i Comunicaionale n
Educaie, MET; Varvara Vanovscaia, profesor colar, grad didactic superior, Liceul Teoretic Vasile Alecsandri, Chiinu
Recenzeni: Gheorghe Cpn, doctor-inginer, confereniar universitar, Universitatea de Stat din Moldova;
Alexei Colbneac, maestru n arte, profesor universitar, Academia de Muzic, Teatru i Arte Plastice, Chiinu;
Tatiana Cartaleanu, doctor n filologie, confereniar universitar, Universitatea Pedagogic de Stat Ion Creang, Chiinu; Mihai leahtichi, doctor n psihologie i n pedagogie, confereniar universitar, Universitatea
Liber Internaional din Moldova; Valeriu Cabac, doctor n tiine fizico-matematice, confereniar universitar, Universitatea de Stat Alecu Russo, Bli
Traducere din limba romn: Veronica Mustea (capit. 1, 2, 3); Arcadie Malearovici (capit. 4, 5, 6, 7)
Responsabil de ediie: Valentina Rbalchina
Redactor: Tatiana Bolgar
Redactor tehnic: Nina Duduciuc
Machetare computerizat: Anatol Andrichi
Copert: Vitalie Ichim

ntreprinderea Editorial-Poligrafic tiina,


str. Academiei, nr. 3; MD-2028, Chiinu, Republica Moldova;
tel.: (+373 22) 73-96-16; fax: (+373 22) 73-96-27;
e-mail: prini@stiinta.asm.md; prini_stiinta@yahoo.com
www.stiinta.asm.md
DIFUZARE:
Republica Moldova: M Societatea de Distribuie a Crii PRO-NOI
str. Alba-Iulia, 75; MD-2051, Chiinu;
tel.: (+373 22) 51-68-17, 71-96-74; fax: (+373 22) 58-02-68;
e-mail: info@pronoi.md; www.pronoi.md
Toate drepturile asupra acestei ediii aparin ntreprinderii Editorial-Poligrafice tiina.
Descrierea CIP a Camerei Naionale a Crii
,
: . 11 / ; trad. din lb. rom.: Veronica Mustea,
Arcadie Malearovici; Min. Educaiei al Rep. Moldova. Ch.: .E.P. tiina, 2014 (Tipografia BALACRON).
192 p.
ISBN 978-9975-67-932-9
004(075.3)

ISBN 978-9975-67-932-9

Anatol Gremalschi. 2008, 2014


Traducere: Veronica Mustea, Arcadie Malearovici.
.E.P. tiina. 2008, 2014


1.

1.1.
1.2.
1.3.
1.4.
1.5.
1.6.
1.7.
1.8.

2.

2.1. .
2.2.
2.3.
2.4.
2.5.
2.6.
2.7.
2.8.
2.9. m-
2.10. pointer
3.

3.1.
3.2.
3.3.
4.
4.1.
4.2.
4.3.
4.4.
4.5.

7.

5.
5.1. ?
5.2.
5.3. Greedy
5.4.
5.5.
5.6.
5.7.
5.8.
5.9.
6.

6.1.
6.2.

4
5
5
6
10
15
18
21
24
27
31
31
35
36
41
48
52
56
63
68
73
81
81
88
91
94
94
96
101
105
110
114
114
119
123
128
135
142
146
149
161
172
172
178
185


, , , .
, ,
: ,
Greedy, , , , , ,
, .

,
,
. ,
, ,
. ,
. ,
.

, . , ,
. , , , , , , , .
, ,
.
:
;
; ,
; , . ,

, ,
, .

1

1.1.
. , . ,
,
. ,
(. 1.1). ,
.

Programul

principal

. 1.1.

: .
<> ::= {< >; |< >;}

,
. ,
: sin, cos, eof ..
, ,
. ,
.
, ,
.
: read, readln, write, writeln . . ,
, . , .
.
.
, .


.
?
?
?
?
?
abs, chr, eof,
eoln, exp, ord, sin, sqr, sqrt, pred, succ, trunc.
read write.
read write?

1.2.
:
function f(x1, x2, ..., xn) : tr;
D;
begin
...
f := e;
...
end;

, :
f ;

(x1, x2, ; xn) ,


;
tr ; .
, D begin end.
(
): label, const, type, var, function, procedure.
f : f := e. , f, .
, (x1, x2, , xn) :
v1, v2, ..., vk : tp
v1, v2, , vk , tp .
f :
f (a1, a2, ..., an)
(a1, a2, , an) . ,
, .
. .
:
Program P97;
{ Putere}
type Natural=0..MaxInt;
var a : real;
b : Natural;
c : real;
s : integer;
t : integer;
v : real;
function Putere(x : real; n : Natural) : real;
{ x n }
var p : real;
i : integer;
begin
p:=1;
for i:=1 to n do p:=p*x;
Putere:=p;
end; { Putere }
begin
a:=3.0;

b:=2;
c:=Putere(a, b);
writeln(a:10:5, b:4, c:10:5);

s:=2;

t:=4;
v:=Putere(s, t);
writeln(s:5, t:4, v:10:5);
readln;
end.
Putere : x real n
Natural. real.
p i.
Putere (a, b) 3.0 2
, b x n.
, , b c n.
Putere(s, t) s, t x
n. , .


:
function Factorial(n : integer) : integer;
var p, i : integer;
begin
p:=1;
for i:=1 to n do p:=p * i;
Factorial:=p;
end;
, .
, . ,
n! n = 2, 3 7.
?
:
Program P98;
{ }
function Factorial(n : 0..7) : integer;
var p, i : integer;
begin
p:=1;
for i:=1 to n do p:=p*i;
Factorial:=p;
end; { Factorial }

begin
writeln(Factorial(4));
readln;
end.
:
function F(x : real; y : integer; z : char) : boolean;
?
a) F(3.18, 4, a)

e) F(3.18, 4, 4)

b) F(4, 4, 4)

f)

c)

F(4, 4, 4)

d) F(4, 3.18, a)

F(3.18, 4, 4)

g) F(15, 21, 3)
h) F(15,21,3)

, :
) a, b, c, d;
) i, j, k, m;
) a, b, c, d;
) ;
) ;
) ax + b = 0;
) n>0, 1;
) a b;
) a b;
) n > 0;
) n > 0;
) n > 0;
) .
:
const nmax=100;
type Vector=array [1..nmax] of real;
, :
) Vector;
) ;
) ;
) .
:
type Punct=record
x, y : real
end;

Segment=record
A, B : Punct
end;
Triunghi=record
A, B, C : Punct
end;
Dreptunghi=record
A, B, C, D : Punct
end;
Cerc=record
Centru : Punct;
Raza : real
end;
, :
a) ;
) ;
) ;
) ;
) .
:
var A : set of char;
, .
, , , .
. ,
:
a) ;
) ;
) .

1.3.
:
procedure p(x1, x2, ..., xn);
D;
begin
...
end;

p ;
(x1, x2, ; xn) .

10

:
D , , ;
begin end ,
.
, ,
( var) .
,
v1, v2, ..., vk : tp
-.
.
,
var v1, v2, ..., vk : tp
- .
:
p(a1, a2, ..., an)
(a1, a2, , an) . ,
.
, .
- ,
. - .
- .
, .
:
Program P99;
{ Lac }
var
a, b, c, t, q : real;
procedure Lac(r : real; var l, s : real);
{ }
{r - ; l - ; s - }
const Pi=3.14159;
begin
l:=2*Pi*r;
s:=Pi*sqr(r);
end; {Lac }

11

begin
a:=1.0;
Lac(a, b, c);
writeln(a:10:5, b:10:5, c:10:5);
Lac(3.0, t, q);
writeln(3.0:10:5, t:10:5, q:10:5);
readln;
end.

Lac : r, l, s. r
-, l s -.
Lac(a, b, c) 1.0 r, b
l s. ,
a:=1.0;
Lac(a, b, c)

b:=2*Pi*1.0;
c:=Pi*sqr(1.0).

Lac(3.0,t,q)

t:=2*Pi*3.0;
q:=Pi*sqr(3.0).


- - ?
:
var k, m, n : integer;
a, b, c : real;
procedure P(i : integer; var j : integer;
x : real; var y : real);
begin
{...}
end.
?
a) P(k,m,a,b)

12

b) P(3,m,a,b)

c)

P(k,3,a,b)

g) P(m,k,6.1,b)

d) P(m,m,a,b)

h) P(a,m,b,c)

e) P(m,k,6.1,b)

i)

P(i,i,i,i)

P(n,m,6,b)

j)

P(a,a,a,a)

f)

.
:
Program P100;
{ }
var a : real;
b : integer;
procedure P(x : real; var y : integer);
begin
{... }
end; { P }
begin
P(a, b);
P(0.1, a);
P(1, b);
P(a, 1);
end.
?
Program P101;
{- - }
var a, b : integer;
procedure P(x : integer; var y : integer);
begin
x:=x+1;
y:=y+1;
writeln(x=, x, y=, y);
end; {P }
begin
a:=0;
b:=0;
P(a, b);
writeln(a=, a, b=, b);
readln;
end.
.

13

, :
a) ax2 + bx + c = 0;
) , ;
) #;
) array [1..100] of real ;
) file of integer ;
) ,
n.
:
type Data = record
Ziua : 1..31;
Luna : 1..12;
Anul : integer;
end;
Persoana = record
NumePrenume : string;
DataNasterii : Data;
end;
ListaPersoane = array [1..50] of Persoana;
, :
) , z ;
) , l ;
) , a;
) , z.l.a;
) ;
) ;
) , ;
) v ;
) ;
) , ;
) ( ).
, :
) ;
) ;
) ;
) ;
) n (n > 2) .
, 20 .
.

14

1.4.
.
, , , ( ). .
i . 0, ,
1. , n, n+1.
. 1.2 105.
, ,
, , .. , . ,
, , .
, .
, ,
. .
,
. , , .
, 105 var a: real
, {1}-{7}.
var c: real , {2}, {3} {5}, {6}. var c: char
, {4} {5}.
, ,
.
, 105 :
c:=chr(d)
char. :
c:=b+1
real.
, /
. ,
/.

15

: / ,
. ,
.
Program P105;
{ }
var a : real;
{1}
procedure P(b : real);
var c : real;
{2}

{ 0}

{ 1}

procedure Q(d : integer);


{ 2}
{3}
var c : char;
{4}
begin
c:=chr(d);
writeln( Q c=, c);
end; {5}
begin
writeln(b=, b);
c:=b+1;
writeln( P
Q(35);
end; {6}

c=, c);

function F(x : real) : real;


begin
f:=x/2;
end;

{ 1}

begin
a:=F(5);
writeln(a=, a);
P(a);
readln;
end {7}.
. 1.2.

, procedure Q ,
{3} {6}.
d:integer , {3} {5}.

16


?
b:real x:real 105
(. 1.2).
, .
, .
Program P106;
{ }
const c=1;
function F1(x : integer) : integer;
begin
F1:=x+c;
end; { F1 }
function F2(c : real) : real;
const x=2.0;
begin
F2:=x+c;
end; { F2 }
function F3(x : char) : char;
const c=3;
begin
F3:=chr(ord(x)+c);
end; { F3 }
begin
writeln(F1=, F1(1));
writeln(F2=, F2(1));
writeln(F3=, F3(1));
readln;
end.
?
P F 105
(. 1.2).
:
Program P107;
{ }
var a : real;
procedure P(x : real);
var a : integer;

17

begin
a:=3.14;
writeln(x+a);
end; { P }
begin
a:=3.14;
P(a);
end.
, , ?

1.5.

.

. , . ,
, .
,
.

.
,
, .
, , , , .
:
Program P108;
{ }
var a,
{ P }
b : real;
{ P, F }
procedure P;
var c : integer;
begin
c:=2;
b:=a*c;
end; { P }

18

{ P }

function F : real;
var a : 1..5;
{ F }
begin
a:=3;
F:=a+b;
end; { F }
begin
a:=1;
P;
writeln(b);
writeln(F);
readln;
end.

{ 2.0000000000E+00 }
{ 5.0000000000E+00 }

, ,
. , , b. F b. , a F .
, ,
. ,
, ; , , ..


.
, 105
(. 1.2).
- ?
, . ?
Program P109;
{
var a : integer;

procedure P;
var b, c, d : integer;
procedure Q;
begin
c:=b+1;
end; { Q }

19

procedure R;
begin
d:=c+1;
end; { R }
begin
b:=a;
Q;
R;
a:=d;
end; { P }
begin
a:=1;
P;
writeln(a);
readln;
end.
:
Type

Ora=0..23;
Grade=-40..+40;
Temperatura=array [Ora] of Grade;

Temperatura , 24 . , :
a) ;
) (), ;
) ,
.
.
. , :
a) ;
) ;
) ( , );
) ;
) ;
) .
.

20

1.6.
.
, , .
, -.
( )
.
, .
,
.
Program P110;
{ - }
var a : integer; { }
function F(x : integer) : integer;
begin
F:=a*x;
a:=a+1;
{, }
end; { F }
begin
a:=1;
writeln(F(1)); { 1 }
writeln(F(1)); { 2 }
writeln(F(1)); { 3 }
readln;
end.

110 F *.
:=+1 . , 1 , .
Program P111;
{ - }
var a : integer;
function F(var x : integer) : integer;
begin
F:=2*x;
x:=x+1;
{ ,
}
end; { F }

21

begin
a:=2;
writeln(F(a));
writeln(F(a));
writeln(F(a));
readln;
end.

{ 4 }
{ 6 }
{ 8 }

111 F 2*.
-, :=+1
, , ..
. ,
F(a), F(a) F(a) ,
.
,
, . -,

.

, .

, .
. :
1.
- , .
2.
- , -.
3. , .


? ?
?
Program P112;
{ }
var a, b : integer;

22

function F(x : integer) : integer;


begin
F:=a*x;
b:=b+1;
end; { F }
function G(x : integer) : integer;
begin
G:=b+x;
a:=a+1;
end; { G }
begin
a:=1; b:=1;
writeln(F(1));
writeln(G(1));
writeln(F(1));
writeln(G(1));
readln;
end.
Program P113;
{ }
var a : integer;
b : real;
function F(var x : integer) : integer;
begin
F:=x;
x:=x+1;
end; { F }
procedure P(x,y:integer; var z:real);
begin
z:=x/y;
end; { P }
begin
a:=1;
P(F(a), a, b);
writeln(a,
, b);
readln;
end.
Program P114;
{ }
var a, b : real;

23

procedure P(var x, y : real);


{ }
begin
a:=x;
x:=y;
y:=a;
end; { P }
begin
a:=1; b:=2;
P(a, b);
writeln(a, b);
a:=3; b:=4;
P(a, b);
writeln(a, b);
readln;
end.
?

1.7.

. , , .
,
type Natural = 0..MaxInt;

, , :
function F(n : Natural) : Natural;
begin
if n=0 then F:=1
else F:=n*F(n-1)
end; {F}

F(7) F
6, 5, , 2, 1, 0:
F(7) -> F(6) -> F(5) -> ... -> F(1) -> F(0).

24

F(0) ,
;
F 1, 2, , 6, 7, F(7)
.

. , :
function Fib(n:Natural):Natural;
begin
if n=0 then Fib:=0
else if n=1 then Fib:=1
else Fib:=Fib(n-1)+Fib(n-2)
end; {Fib}

Fib n > 1 Fib(n-1),


Fib(n-2) . ., :
Fib(4) ->
Fib(3), Fib(2) ->
Fib(2), Fib(1), Fib(1), Fib(0) ->
Fib(1), Fib(0).

, , .
, : ,
.
,
. , , n 0;
Fib , n 0 1.
:
, ;
() -;
, . . , .
, , . (
for, while, repeat). :

25

function F(n: Natural): Natural;


var i, p : Natural;
begin
p:=1;
for i:=1 to n do p:=p*i;
F:=p;
end; {F}

, :
, ,
. .


? ?
?
.
, :
a) S(n)=1+3+5+...+(2n1);
) P(n) = 1 4 7 ... (3n 2);
) ;
) P(n) = 2 4 6 ... 2n.
, m, n :

(0, 0), (1, 2), (2, 1) (2, 2). (4, 4) (10, 10).
, .

type Vector=array [1..20] of integer;
, :
a) ;
) ;
) ;
) ;
) , ;
) ;
) , ,
.

26

:
function S(n:Natural):Natural;
begin
if n=0 then S:=0
else S:=n+S(n-1)
end; {S}
, true, s
<>::=<>|<><>
. .
function N(s : string) : boolean;
var
i : integer;
p : boolean
begin
p:=(s<>);
for i=1 to length(s) do
p:=p and (s[i] in [0..9]);
N:=p;
end;

<>::=<>{<>}
:
<>::=<>{<>}
<>::=+|
<>::=<>|<><><>
, true, s <>.

1.8.

:
<> ::= < >; <>
| < >; <>
| function <>; <>
< > ::=
function <> [< >]
: <>
. 1.3.

27

<>

function

< >
function

. 1.3.

:
<>::=< >;<>
| < >;<>
| procedure <>;<>
< > := procedure <> [<
>]
. 1.4.

<>

procedure

< >
procedure


. 1.4.

:
< > ::=
(< > {; < >})

28

< > ::=


[var] <> {, <>} : <>
| < >
| < >
. 1.5.

< >
(

var

;
. 1.5. < >

, var
-. var -.
() -
(). Turbo PASCAL ,
-. , , -.
:
< > ::= < > [< >]
:
< > ::= < > [< >]
:
< > ::=
(< > {,< >})
< > ::=<> | <>
| < > | < >
. 1.6.
, .
-
, .

29

. -
.
< >

< >

< >
(



,
. 1.6.

-
. - .
- () (), , .



function <>; <> ?
. 1.3 1.5 ,
106, 1.4.
- -?
. 1.4 1.5 ,
101, 1.3.
. 1.3 1.6 ,
105, 1.4.

30


2.1. .
, var , .
. ,
.
, ,
. . ,
, . , ,
.
,
, .

. , :
type Tr = ^ Tb;

Tr , Tb . ^
. , .
< > . 2.1.

< >
^

. 2.1. < >

.
,
.
nil (), .
:
type AdresaInteger=^integer;
AdresaChar=^char;

31

var

i : AdresaInteger;
r : ^real;
c : AdresaChar;

i integer. r real char. ,


AdresaInteger, AdresaChar ^real
.
= <>.
.
new
().
new(p)

.
.
:
^.
nil, .
:
new(i); i^:=1 integer; 1;
new(r); r^:=2.0 real; 2.0;
new(c); c^:=* char; *.
p^, new(p),
, . ,
new(p); new(p); ...; new(p)

v1, v2, ,
vn. p vn. ,
, .

dispose (). :
dispose(p)
.
:
dispose(i); dispose(r); dispose(c)

32

dispose(p) .
,
.
:
Program P117;
{ }
type AdresaInteger=^integer;
var i, j, k : AdresaInteger;
r, s, t : ^real;
begin
{ integer }
new(i); new(j); new(k);
{ }
i^:=1; j^:=2;
k^:=i^+j^;
writeln(k^);
{ real}
new(r); new(s); new(t);
{ }
r^:=1.0; s^:=2.0;
t^:=r^/s^;
writeln(t^);
{ }
dispose(i); dispose(j); dispose(k);
dispose(r); dispose(s); dispose(t);
readln;
end.

, , , ,
new.
dispose . , new
dispose :
.
, ,
, .
, new .
:
Program P118;
{: }
label 1;
var i : ^integer;

33

begin
1 : new(i);
goto 1;
end.

, , .


?
?
. 2.1 , 117.
:
type AdresaReal=^real;
var r : AdresaReal;
AdresaReal ,
r^.
?
?
:
type AdresaTablou = ^array [1..10] of integer;
var t : AdresaTablou;
AdresaTablou ,
t^.
:
Program P119;
{}
var r, s : ^real;
begin
r^:=1; s^:=2;
writeln(r^=, r^,
readln;
end.

s^=, s^);

,
.
.
?
Program P120;
var i : ^integer;

34

begin
new(i); i^:=1;
new(i); i^:=2;
new(i); i^:=3;
writeln(i^);
readln;
end.
:
Program P121;
{}
var i, j : ^integer;
begin
new(i);
i^:=1;
dispose(i);
new(j);
j^:=2;
dispose(j);
writeln(i^=, i^,
readln;
end.

j^=, j^);

2.2. C
. .
, , , , , . . , , s[i+1], ,
s[i]. , , ,
,
.
, . , .
, : ; , ,
, ;
.. , ,
, , ,

35

. . . ,
.
, ,
. , , . ,
, .
, , .

, , , .
,
.
.


. .
?
,
. .
.
?
.

2.3.
, . record, :
. , . ,
. , , , .
. 2.2 , . A, B, C D.
, ,
:
type AdresaCelula=^Celula;
Celula=record
Info : string;

36

Urm : AdresaCelula;
end;
var P : AdresaCelula;

, ,
Info, Urm. , Info . Urm nil. ( )
P (. 2.2).

nil

. 2.2.

, AdresaCelula
Celula . , ,
.
. , , .
:
Program P122;
{ A->B->C->D}
type AdresaCelula=^Celula;
Celula=record
Info : string;
Urm : AdresaCelula;
end;
var P,
{ }
R, V : AdresaCelula;
begin
{1 - }
P:=nil;
{2 - A}
new(R);
{ }
P:=R;
{ }

37

R^.Info:=A; { }
R^.Urm:=nil; { }
V:=R;
{ }
{3 - B}
new(R);
{ }
R^.Info:=B; { }
R^.Urm:=nil; { }
V^.Urm:=R;
{ A -> B}
V:=R;
{ }
{4 - C}
new(R);
{ }
R^.Info:=C; { }
R^.Urm:=nil; { }
V^.Urm:=R;
{ B -> C}
V:=R;
{ }
{5 - D}
new(R);
{ }
R^.Info:=D; { }
R^.Urm:=nil; { }
V^.Urm:=R;
{ C -> D}
V:=R;
{ }
{ }
R:=P;
while R<>nil do begin
writeln(R^.Info);
R:=R^.Urm
end;
readln;
end.

. 2.3. V^.Urm,
V ( ). ,
R^.Info R^.Urm .
123 :
Program P123;
{ }
type AdresaCelula=^Celula;
Celula=record
Info : string;
Urm : AdresaCelula;
end;

38

1
P

2 A
P
V
A

3 B
P

V
A

4 C
V
P
A

5 D
V
A

V
C

. 2.3.

39

var p,q,r : AdresaCelula;


s : string;
i: integer;
procedure Creare;
begin
p:=nil;
write(s=); readln(s);
new(r); r^.Info:=s; r^.Urm:=nil;
p:=r; q:=r;
write(s=);
while not eof do
begin
readln(s);write(s=);
new(r); r^.Info:=s; r^.Urm:=nil;
q^.Urm:=r; q:=r
end;
end; { Creare }
procedure Afisare;
begin
r:=p;
while r<>nil do
begin
writeln(r^.Info);
r:=r^.Urm;
end;
readln;
end; { }
begin
Creare;
Afisare;
end.

:
a) ;
) , , .
, , :
type Lista=^Celula;
Celula=record
Info : string;
Urm : Lista
end;
var P : Lista;

40

. , . 2.2 :
var L : array [1..4] of string;
...
L[1]:= A;
L[2]:= B;
L[3]:= C;
L[4]:= D;
...


, ?
, ? ? ?
, . 2.2,
.
, .
, . ,
, .
,
?

2.4.
:
;
, ;
;
..
, (. 2.2),
:
type

AdresaCelula=^Celula;
Celula=record;
Info : string;
Urm : AdresaCelula
end;

41

:
R:=P; { }
while R<>nil do
begin
{ R^.Info}
R:=R^.Urm; { }
end;

, , Cheie,
:
R:=P;
while R^.Info<>Cheie do R:=R^.Urm;

R.
,
, , ,
. , , R nil, R^
.
:
R:=P;
while R<>nil do
begin
if R^.Info=Cheie then goto 1;
R:=R^.Urm
end;
1: ...

,
:
type Lista=^AdresaCelula;
Celula=record;
Info : string;
Urm : Lista;
end;
var P : Lista;
...
function Caut(P : Lista; Cheie : string):Lista;
begin
if P=nil then Caut:=nil
else
if P^.Info=Cheie then Caut:=P
else Caut:=Caut(P^.Urm, Cheie)
end;

42

Caut , , , , Cheie.
, Q, , R
(. 2.4), :
Q^.Urm:=R^.Urm;
R^.Urm:=Q;

...
...
Q

a)
R

...
...
Q

)
. 2.4. :
;

Q
Q^.Urm (. 2.5):
Q:=P;
while Q^.Urm<>R do Q:=Q^.Urm;
Q^.Urm:=R^.Urm;

,
.
:
Program P124;
{ }
type AdresaCelula=^Celula;
Celula=record

43

...

...
a)
Q

...

...
)

. 2.5. :
a ;

Info : string;
Urm : AdresaCelula;
end;
var P : AdresaCelula; { }
c : char;
procedure Creare;
var R, V : AdresaCelula;
begin
if P<>nil then writeln( )
else begin
writeln( :);
while not eof do
begin
new(R);
readln(R^.Info);
R^.Urm:=nil;
if P=nil then begin P:=R; V:=R end
else begin V^.Urm:=R; V:=R end;
end;
end;
end; {Creare }
procedure Afis;
var R : AdresaCelula;
begin
if P=nil then writeln( )
else begin
writeln( :);
R:=P;
while R<>nil do

44

begin
writeln(R^.Info);
R:=R^.Urm;
end;
end;
readln;
end; {Afis }
procedure Includ;
label 1;
var Q, R : AdresaCelula;
Cheie : string;
begin
new(Q);
write( , );
writeln( :);
readln(Q^.Info);
write( , );
writeln( :);
readln(Cheie);
R:=P;
while R<>nil do
begin
if R^.Info=Cheie then goto 1;
R:=R^.Urm;
end;
1:if R=nil then begin
writeln( );
dispose(Q);
end;
else begin
Q^.Urm:=R^.Urm;
R^.Urm:=Q;
end;
end; { Includ }
procedure Exclud;
label 1;
var Q, R : AdresaCelula;
Cheie : string;
begin
write( , );
writeln( :);
readln(Cheie);
R:=P;
Q:=R;

45

while R<>nil do
begin
if R^.Info=Cheie then goto 1;
Q:=R;
R:=R^.Urm;
end;
1:if R=nil then writeln( )
else begin
if R=P then P:=R^.Urm else Q^.Urm:=R^.Urm;
dispose(R);
end;
end; { Exclud }
begin
P:=nil; { }
repeat
writeln(:);
writeln(C - );
writeln(I - );
writeln(E - );
writeln(A - );
writeln(O - );
write(=); readln(c);
case c of
C : Creare;
I : Includ;
E : Exclud;
A : Afis;
O :
else writeln( )
end;
until c=O;
end.

Creare . , ,
. Afis .
Includ , ,
, . , . ,
. Exclud , , , , .


, .

46

Includ Exclud 124


goto.
:
type

AdresaCandidat=^Candidat;
Candidat=record
NumePrenume : string;
NotaMedie : real;
Urm : AdresaCandidat
end;

, :
a) Candidat;
) ;
) , ;
) , ;
) , 7,5;
) , ,
9,0;
) , 6,0.
, :
a) ;
) ;
) ;
) , .
.
, :
a) ;
) ;
) ;
) .
? ,
100 .
, .
,
.

. , ,
, , .
:
a) ;
) .
.

47

2.5.
(- stack) ,
. , , . , , .
. 2.6 ,
A, B, C.
S

B
S
A

C
B
A

a)

. 2.6. :
;

, ,
:
type AdresaCelula=^Celula;
Celula=record
Info : string;
Prec : AdresaCelula
end;
var S : AdresaCelula;

S.
Prec.
(. 2.7) :
new(R); { }
{ R^.Info}
R^.Prec:=S; { }
S:=R; { }

R AdresaCelula.

48

S
S

a)

. 2.7. :
; D; D, C

(. 2.7)
:
R:=S; { }
{ R^.Info}
S:=S^.Prec; { }
dispose(R); { }

:
Program P127;
{ }
type AdresaCelula=^Celula;
Celula=record
Info : string;
Prec : AdresaCelula;
end;
var S : AdresaCelula; { }
c : char;
procedure Introduc;
var R : AdresaCelula;
begin
new(R);
write( , );
writeln(:);
readln(R^.Info);
R^.Prec:=S;
S:=R;
end; { Includ }

49

procedure Extrag;
var R : AdresaCelula;
begin
if S=nil then writeln( )
else begin
R:=S;
write();
writeln(:);
writeln(R^.Info);
S:=S^.Prec;
dispose(R);
end;
end; { Extrag }
procedure Afis;
var R : AdresaCelula;
begin
if S=nil then writeln( )
else begin
writeln( :);
R:=S;
while R<>nil do begin
writeln(R^.Info);
R:=R^.Prec;
end;
end;
readln;
end; { Afis }
begin
S:=nil; { }
repeat
writeln(:);
writeln(I - ;);
writeln(E - );
writeln(A - );
writeln(O - );
write( =); readln(c);
case c of
I : Introduc;
E : Extrag;
A : Afis;
O :
else writeln( )
end;
until c=O;
end.

50

LIFO (last in, first out , , )


. , , : array[1..n] of ,
n .


?
. , .
. 2.8 .
,
, . :
(integer);
(string);
(1960 ... 2000);
(string);
(real);
(string).

. 2.8.

:
. ,
. :
(string);
(string);
(19301985);
(, , ).
, : (, ), [, ], {, }. , :
a) ;
) , (), [A], {A} ;
) A B , AB .

51

, : ( ), [ ], { }, [( )], ((({[ ]}))([ ])) , : ([, ( )[ ]{{, ([)] . , , , , .


. , . (, [, {, .
( ), [ ] { },
.
, .
. , . ? , 100 .

2.6.
(- queue) , ,
. , ,
. . 2.9 ,
A, B, C.
U

a)
U

)
. 2.9. : ;

, , :
type

52

AdresaCelula=^Celula;
Celula=record
Info : string;

Urm : AdresaCelula
end;
var P, U : AdresaCelula;


, U.
Urm.
(. 2.10) :
new(R); { }
{ R^.Info }
R^.Urm:=nil; { }
U^.Urm:=R;
{ }
U:=R;
{ }

a)
U

)
U

)
. 2.10. : ;
D; A, B

53

(. 2.10) :
R:=P; { }
{ R^.Info }
P:=P^.Urm; { }
dispose(R); { }

:
Program P128;
{ }
type AdresaCelula=^Celula;
Celula=record
Info : string;
Urm : AdresaCelula;
end;
var P,
{ }
U : AdresaCelula; { }
c : char;
procedure Introduc;
var R : AdresaCelula;
begin
new(R);
write( );
writeln( :);
readln(R^.Info);
R^.Urm:=nil;
if P=nil then begin P:=R; U:=R end
else begin U^.Urm:=R; U:=R end;
end; { Introduc }
procedure Extrag;
var R : AdresaCelula;
begin
if P=nil then writeln( )
else begin
R:=P;
write();
writeln(:);
writeln(R^.Info);
P:=P^.Urm;
dispose(R);
end;
end; { Extrag }

54

procedure Afis;
var R : AdresaCelula;
begin
if P=nil then writeln( )
else begin
write( );
writeln(:);
R:=P;
while R<>nil do
begin
writeln(R^.Info);
R:=R^.Urm;
end;
end;
readln;
end; { Afis }
begin
P:=nil; U:=nil; { }
repeat
writeln(:);
writeln(I - ;);
writeln(E - ;);
writeln(A - ;);
writeln(O - );
write( =); readln(c);
case c of
I : Introduc;
E : Extrag;
A : Afis;
O :
else writeln( )
end;
until c=O;
end.

FIFO (first in, first out ,


, , ).
,

.


, .
, , .
, -

55

, .
:
(integer);
(string);
(integer).
,
, .
. , :
a) ;
) , ;
) ;
) .

2.7.
record,
,
.
:
a) ;
) , , .
, . . 2.11 , A, B, C, D, E, F, G, H, I, J. ,
, :
type AdresaNod=^Nod;
Nod=record
Info : string;
Stg, Dr : AdresaNod
end;
var T : AdresaNod;

,
, :
type Arbore=^Nod;
Nod=record
Info : string;
Stg, Dr : Arbore;
end;
var T : Arbore;

, , .
.
= nil.

56

, , .
Stg, Dr.
, , ,
i- , (i+1). :
0, , , 1 . . (. 2.11). (i+1)-
, i- , .
. 2.11 ,
; D , E B . .
T
A

a)
0

1
2

B
D

C
E

)
. 2.11. : ;

57

,
. . 2.11 ;
D E ..
, , . . , . . 2.11 3; D, H, F, I
J , A, B, C, E G .
.
:
;
;
, , , ;
;
, .
. 2.11
: A, B, C, D, E, F, G, H, I, J.
:
, ;
, , ;
;
, .
:
Program P129;
{ }
type AdresaNod=^Nod;
Nod=record
Info : string;
Stg, Dr : AdresaNod
end;
AdresaCelula=^Celula;
Celula=record
Info : AdresaNod;
Urm : AdresaCelula
end;
var T : AdresaNod;
{}
Prim,
{ }
Ultim : AdresaCelula; { }
procedure IntroduInCoada(Q : AdresaNod);
var R : AdresaCelula;

58

begin
new(R);
R^.Info:=Q;
R^.Urm:=nil;
if Prim=nil then begin Prim:=R; Ultim:=R end
else begin Ultim^.Urm:=R; Ultim:=R end;
end; {IntroduInCoada}
procedure ExtrageDinCoada(var Q : AdresaNod);
var R : AdresaCelula;
begin
if Prim=nil then writeln( )
else begin
R:=Prim;
Q:=R^.Info;
Prim:=Prim^.Urm;
dispose(R);
end;
end; { ExtrageDinCoada }
procedure CreareArboreBinar;
var R, Q : AdresaNod;
s : string;
begin
T:=nil;
{ }
Prim:=nil; Ultim:=nil; { }
writeln( :); readln(s);
if s<> then
begin
new(R); { }
R^.Info:=s;
T:=R;
{ }
IntroduInCoada(T);
end;
while Prim<>nil do { }
begin
ExtrageDinCoada(R);
writeln( , R^.Info);
write(: ); readln(s);
if s= then R^.Stg:=nil
else
begin
new(Q); R^.Stg:=Q;
Q^.Info:=s;
IntroduInCoada(Q);
end; { else }
write(: ); readln(s);
if s= then R^.Dr:=nil

59

else
begin
new(Q); R^.Dr:=Q;
Q^.Info:=s;
IntroduInCoada(Q);
end; { else }
end; { while }
end; { CreareArboreBinar }
procedure AfisareArboreBinar;
var R : AdresaNod;
begin
if T=nil then writeln( )
else
begin
writeln( :);
Prim:=nil; Ultim:=nil;
IntroduInCoada(T);
while Prim<>nil do
begin
ExtrageDinCoada(R);
writeln(, R^.Info);
write( : );
if R^.Stg=nil then write(nil, )
else begin
write(R^.Stg^.Info, , );
IntroduInCoada(R^.Stg);
end;
if R^.Dr=nil then writeln(nil)
else begin
writeln(R^.Dr^.Info);
IntroduInCoada(R^.Dr);
end;
end; { while }
end; { else }
readln;
end; { AfisareArboreBinar }
begin
CreareArboreBinar;
AfisareArboreBinar;
end.

, , .
<ENTER> ( ). , , 129, , .

60

:
;
;
.
. 2.11
: A, B, D, E, H, C, F, G, I, J.
:
Program P130;
{ }
type Arbore=^Nod;
Nod=record
Info : string;
Stg, Dr : Arbore
end;
var

T : Arbore;

{}

function Arb : Arbore;


{ }
var R : Arbore;
s : string;
begin
readln(s);
if s= then Arb:=nil
else begin
new(R);
R^.Info:=s;
write( );
writeln( , s, :);
R^.Stg:=Arb;
write( );
writeln(, s, :);
R^.Dr:=Arb;
Arb:=R;
end;
end; {Arb }
procedure AfisArb(T : Arbore; nivel : integer);
{ }
var i : integer;
begin
if T<>nil then
begin
AfisArb(T^.Stg, nivel+1);
for i:=1 to nivel do write(
);
writeln(T^.Info);

61

AfisArb(T^.Dr, nivel+1);
end;
end; {AfisareArb }
begin
writeln( :);
T:=Arb;
AfisArb(T, 0);
readln;
end.

Arb , . , nil. , Info


. , Stg ( ) Dr ( ), ,
.
AfisArb .
, .
.
129 130 , , : , .
,

.


? : , ,
, , , , ,
.
, .
?
, ,
- . , , .
AfisArb 130 , : ,
, ?
, . .
.
:

62

(string);
(string);
(, , );
(string).
, .
, . ,
.
,
.
. . . . .
Arb 130 ,
: A, C, G, J, I, F, B, E, H, D?
Arb 130 : , , .
, .
. , . .
, . .
.

2.8.
, , :
, ( );
, ( ,
. .).
.
,
.
. : , , .
:
;
;
.
:
;
;
.

63

:
;
;
.
, : ,
, . . 2.12.

. 2.12.

. 2.11
:
A, B, D, E, H, C, F, G, I, J;
:
D, B, E, H, A, F, C, I, G, J;
:
D, H, E, B, F, I, J, G, C, A.
.
Program P131;
{ }
type Arbore=^Nod;
Nod=record
Info : string;
Stg, Dr : Arbore
end;
var T : Arbore;
{}

64

function Arb : Arbore;


{ }
var R : Arbore;
s : string;
begin
readln(s);
if s= then Arb:=nil
else begin
new(R);
R^.Info:=s;
write( );
writeln( , s, :);
R^.Stg:=Arb;
write( );
writeln( , s, :);
R^.Dr:=Arb;
Arb:=R;
end;
end; {Arb }
procedure AfisArb(T : Arbore; nivel : integer);
{ }
var i : integer;
begin
if T<>nil then
begin
AfisArb(T^.Stg, nivel+1);
for i:=1 to nivel do write(
);
writeln(T^.Info);
AfisArb(T^.Dr, nivel+1);
end;
end; {AfisareArb }
procedure Preordine(T : Arbore);
{ }
begin
if T<>nil then begin
writeln(T^.Info);
Preordine(T^.Stg);
Preordine(T^.Dr)
end;
end; {Preordine }
procedure Inordine(T : Arbore);
{ }
begin
if T<>nil then begin

65

Inordine(T^.Stg);
writeln(T^.Info);
Inordine(T^.Dr)
end;
end; {Preordine }
procedure Postordine(T : Arbore);
{ }
begin
if T<>nil then begin
Postordine(T^.Stg);
Postordine(T^.Dr);
writeln(T^.Info)
end;
end; { Postordine }
begin
writeln( :);
T:=Arb;
AfisArb(T, 0);
readln;
writeln( :);
Preordine(T);
readln;
writeln( :);
Inordine(T);
readln;
writeln( :);
Postordine(T);
readln;
end.

, Arb ,
. AfisArb
, .


?
. .
,
. 2.13.
Preordine, Inordine
Postordine 131.
, .
, , .

66

1
2

5
7

10

. 2.13.

, :
a) ( );
) ( );
) ( ):
.
,
.
, ,
: +, , *, mod, div.
.
, , , .
, ,
.
, : +, , *, /. ,
, , . i :
a) , i,
, , ;
) E1E2, 1 2 , ,
, 1, 2.
, ,
, . , :
a) , ;
) , .
. .
+, , . * /
, ,
, +, .

67

2.9. m-
record,
m 2 . ,
.
m- :
a) m- ;
) , m m- , m- .
, , , , m . ,
.
2-
. 3-, 4-, 5- . .
(- multiway trees).
. 2.14 4- . ,
m- : , , , , , , , ,
. , , , , , , , , ., , , . ,
,
, ,
.
0
1

A
D

. 2.14. 4-

, m- , :
type Arbore = ^Nod;
Nod = record;
Info : string;
Dsc : array [1..m] of Arbore
end;
var T : Arbore;

68

: Dsc[1], Dsc[2], ,
Dsc[m] Dsc. .
m- .
.
, . 2.14
: A, B, C, D, E, F, G, H, I, J, K.
,
, : ,
.
: ,
; , , . . 2.14 : A, B, C, E, F,
J, K, G, H, D, I.
.
:
Program P133;
{ m- }
const m=4;
type Arbore=^Nod;
Nod=record
Info : string;
Dsc : array [1..m] of Arbore
end;
AdresaCelula=^Celula;
Celula=record
Info : Arbore;
Urm : AdresaCelula
end;
var

T : Arbore;
{}
Prim,
{ }
Ultim : AdresaCelula; { }

procedure IntroduInCoada(Q : Arbore);


var R : AdresaCelula;
begin
new(R);
R^.Info:=Q;
R^.Urm:=nil;
if Prim=nil then begin Prim:=R; Ultim:=R end
else begin Ultim^.Urm:=R; Ultim:=R end;
end; {IntroduInCoad }

69

procedure ExtrageDinCoada(var Q : Arbore);


var R : AdresaCelula;
begin
if Prim=nil then writeln( )
else begin
R:=Prim;
Q:=R^.Info;
Prim:=Prim^.Urm;
dispose(R);
end;
end; {ExtrageDinCoad }
procedure CreareArbore(var T : Arbore);
var R, Q : Arbore;
s : string;
i : integer;
begin
T:=nil;
{ }
Prim:=nil; Ultim:=nil; { }
writeln( : ); readln(s);
if s<> then
begin
new(R); { }
R^.Info:=s;
T:=R;
{ }
IntroduInCoada(T);
end;
while Prim<>nil do { }
begin
ExtrageDinCoada(R);
for i:=1 to m do R^.Dsc [i]:=nil;
writeln( ,R^.Info);
i:=1; readln(s);
while (i<=m) and (s<>) do
begin
new(Q); R^.Dsc [i]:=Q; Q^.Info:=s;
IntroduInCoada(Q);
i:=i+1; readln(s);
end;
end;
end; {CreareArbore }
procedure AfisareArbore(T : Arbore);
var R : Arbore;
i : integer;
begin
if T=nil then writeln( )

70

begin
writeln( :);
Prim:=nil; Ultim:=nil;
IntroduInCoada(T);
while Prim<>nil do
begin
ExtrageDinCoada(R);
writeln(, R^.Info);
write(: );
for i:=1 to m do
if R^.Dsc [i]<>nil then
begin
write(R^.Dsc [i]^.Info, );
IntroduInCoada(R^.Dsc [i]);
end; {then }
writeln;
end; {while }
end; {else }
readln;
end; {AfisareArbore }
procedure InLatime(T : Arbore);
var R : Arbore;
i : integer;
begin
if T<>nil then
begin
Prim:=nil; Ultim:=nil;
IntroduInCoada(T);
while Prim<>nil do
begin
ExtrageDinCoada(R);
writeln(R^.Info);
for i:=1 to m do
if R^.Dsc [i]<>nil then IntroduInCoada(R^.Dsc [i]);
end; {while }
end; {then }
end; {InLatime }
procedure InAdincime(T : Arbore);
var i : integer;
begin
if T<>nil then
begin
writeln(T^.Info);
for i:=1 to m do InAdincime(T^.Dsc [i]);
end;
end; {InAdincime }

71

begin
CreareArbore(T);
AfisareArbore(T);
writeln( :);
InLatime(T);
readln;
writeln( :);
InAdincime(T);
readln;
end.

. <ENTER>. ,
CreareArbore .
, AfisareArbore .
m- : , , , , . . m- ,
, .
MS-DOS, UNIX ..
.


3-, 5- 6- .
m- ? ?
m- . .
,
m- . , , .
, :
a) m- ;
) ;
) .
InAdincime
133.
InLatime 133,
. 2.14 : A, D, C, B, I, H, G, F, E, K, J?
InAdincime 133,
. 2.14 : A, D, I, C, H, G, F, K, J, E, B?
m- , , , . .

72

m- . , .
, , :
(string[8]);
(string[3]);
( , , , , );
(integer);
(A, H, R, S).
, ,
.
m ,
array [1..m] of Arbore.
.
,
.

2.10. pointer
Turbo PASCAL.
pointer () nil.
, , , pointer . , nil
. , pointer .
pointer : = <>.
.
pointer :
var p : pointer;

, p^ . ,
pointer ^, .
pointer
.
Program P134;
{ pointer }
var p : pointer;
i, j : ^integer;
x, y : ^real;
r, s : ^string;

73

begin
{p integer }
new(i); i^:=1;
p:=i;
new(i); i^:=2;
j:=p;
writeln(j^=, j^); { 1 }
{p real }
new(x); x^:=1;
p:=x;
new(x); x^:=2;
y:=p; writeln(y^=, y^); { 1.0000000000E+00 }
{p string }
new(r); r^:=AAA;
p:=r;
new(r); r^:=BBB;
s:=p;
writeln(s^=, s^); { AAA }
readln;
end.

pointer
. Turbo PASCAL , heap (). , , HeapOrg pointer.
HeapPtr pointer ,
heap-a (.2.15).

HeapPtr

HeapOrg

. 2.15. heap

heap new. heap ,


HeapPtr : -

74

,
.
, heap , dispose.
.
dispose , , new. heap .
new , .
, ,
, dispose .
, :
, . .,
. , dispose , ,
.
, .
, dispose , ,
. ,
mark release.
mark :
mark(p)

p pointer. HeapPtr p.
release :
release(p)

, mark: ,
pointer, HeapPtr.
, , :
1) mark ;
2) new ;
3) ;
4) , , heap, release.
:
:
var i, j, k, m, n : ^integer;
p : pointer;

75

, :
new(i); i^:=1;
new(j); j^:=2;
mark(p);
new(k); k^:=3;
new(m); m^:=4;
new(n); n^:=5;

heap . 2.16 .
k^, mark(p)
pointer HeapPtr.
)

HeapPtr

HeapOrg

n
m
k

HeapPtr

HeapOrg

. 2.16. heap () () release(p)


release(p)

,
mark, : k^, m^ n^, (. 2.16 ).
HeapOrg
heap, , , :
release(HeapOrg)

mark
release.
Program P135;
{ }
type Lista=^Celula;
Celula=record
Info : string;
Urm : Lista
end;

76

Stiva=Lista;
end;
var L : Lista;
S : Stiva;
T : Arbore;
p : pointer;
function Lst : Lista;
{ }
var R : Lista;
s : string;
begin
write(Info=); readln(s);
if s= then Lst:=nil
else
begin
new(R);
R^.Info:=s;
R^.Urm:=Lst;
Lst:=R;
end;
end; {Lst }
procedure AfisLst(L : Lista);
{ }
begin
if L<>nil then
begin
writeln(L^.Info);
AfisLst(L^.Urm);
end;
end; {AfisLst }
procedure Stv(var S : Stiva);
{ }
var R : Stiva;
st : string;
begin
S:=nil;
write(Info=); readln(st);
while st<> do
begin
new(R);
R^.Info:=st;
R^.Urm:=S;
S:=R;

77

write(Info=); readln(st);
end;
end; { Stv }
function Arb : Arbore;
{ }
var R : Arbore;
s : string;
begin
readln(s);
if s= then Arb:=nil
else begin
new(R);
R^.Info:=s;
write( );
writeln( , s, :);
R^.Stg:=Arb;
write( );
writeln( , s, :);
R^.Dr:=Arb;
Arb:=R;
end;
end; { Arb }
procedure AfisArb(T : Arbore; nivel : integer);
{ }
var i : integer;
begin
if T<>nil then
begin
AfisArb(T^.Stg, nivel+1);
for i:=1 to nivel do write(
);
writeln(T^.Info);
AfisArb(T^.Dr, nivel+1);
end;
end; {AfisArb }
begin
writeln( :);
L:=Lst;
writeln( :);
AfisLst(L);
mark(p);
{ p HeapPtr }
writeln( :);
T:=Arb;
writeln( :);
AfisArb(T, 0);
release(p);{ , }

78

writeln( :);
Stv(S);
writeln( );
AfisLst(S);
release(HeapOrg); { ,
}
readln;
end.

, Turbo PASCAL dispose


release nil , (. 2.16 ). , .


pointer? ?
:
Program P136;
{Eroare }
var i : ^integer;
j, k : integer;
p : pointer;
begin
new(i); i^:=1;
p:=i;
new(i); i^:=2;
j:=i^; k:=p^;
writeln(j+k=, j+k);
end.
pointer?
heap ? .
. , .
Program P137;
var i, j, k, m, n : ^integer;
p : pointer;
begin
{ i^, j^, k^ }
new(i); new(j); new(k);
i^:=1; j^:=2; k^:=3;
p:=j; {p j }
{ j^ m^}
dispose(j); new(m); m^:=4;

79

j:=p; { j }
writeln(i^=, i^, j^=, j^, k^=, k^);
{ m^ n^}
dispose(m); new(n); n^:=5;
writeln(i^=, i^, j^=, j^, k^=, k^);
readln;
end.
Program P138;
var i, j, k, m : ^integer;
begin
{ i^, j^ }
new(i); new(j);
i^:=1; j^:=2;
{ heap}
release(HeapOrg);
{ : k^ m^}
new(k); new(m);
k^:=1; m^:=2;
writeln(k^=, k^, m^=, m^);
i^:=3; j^:=4;
writeln(k^=, k^, m^=, m^);
readln;
end.
, , :
a) ;
) ;
) m- .
dispose
.
, , m- . , ,
.

80

3


3.1.
.
, .
, . , .
.
.
, .
Turbo PASCAL unit-.
:
unit <>;
interface
[uses <> {,<>};]
[<>]
[<>]
[<>]
[{< >; | < >;}]
implementation
[uses <> {,<};]
[<>]
[<>]
[<>]
[<>]
[<>]
[{function <>;
<Corp>; |
procedure <>;
<>;}]
[begin
[<> {; <}]]
end.

81

: ,
, .
interface.
, , , . , . ,
. ,
uses.
implementation.
, , , . , ,
. , . ,
, .
function procedure . ,
.
, ,
begin.
, . ,

uses .
:
Unit U1;
{ }
interface
const nmax=100;
type Vector=array [1..nmax] of real;
var n : 1..nmax;
function sum(V : Vector) : real;
function min(V : Vector) : real;
function max(V : Vector) : real;
procedure Citire(var V : Vector);
procedure Afisare(V : Vector);
implementation
var i : 1..nmax;
s : real;
function sum;
begin
s:=0;
for i:=1 to n do s:=s+V [i];

82

sum:=s;
end; {sum }
function min;
begin
s:=V [1];
for i:=2 to n do
if s>V [i] then s:=V [i];
min:=s;
end; {min }
function max;
begin
s:=V [1];
for i:=2 to n do
if s<V [i] then s:=V [i];
max:=s;
end; {max }
procedure Citire;
begin
for i:=1 to n do readln(V [i]);
end; {Citire }
procedure Afisare;
begin
for i:=1 to n do writeln(V [i]);
end; {Afisare }
begin
write(n=); readln(n);
end.

U1 nmax, Vector, n,
sum, min, max, Citire Afisare.
n . , : uses U1.
:
Program P139;
{ U1}
uses U1;
var A : Vector;
begin
writeln( :);
Citire(A);
writeln( :);

83

Afisare(A);
writeln(sum=, sum(A));
writeln(min=, min(A));
writeln(max=, max(A));
readln;
end.

:
1. .
2. :
;
, ;
, .
3. ,
.
:
Program P140;
uses U2;
var x : integer;
begin
x:=4;
writeln( P140:);
writeln(n=, U3.n);
writeln(m=, m);
writeln(x=, x);
readln;
end.
Unit U2;
interface
uses U3;
var m : integer;
x : real;
implementation
begin
writeln( U2:);
m:=2;
writeln(
m=, m);
x:=3.0;
writeln(
x=, x);
end.
Unit U3;
interface
var n : integer;
implementation

84

begin
writeln( U3:);
n:=1;
writeln(n=, n);
end.

140 U2 , U3
. n U3
U3.n. x var x: real U2
var x: integer P140. .
, Turbo PASCAL, , .
:
System Turbo PASCAL.
,
uses.
Crt
, .
uses crt.
Graph , : , , , , , . .
uses graph.
Printer lst. ,
. Printer
uses printer.
, , ,

Turbo PASCALs Online Help.

, :
, , ,
. . .
, .


? ?
?
, .

85

?
?
Program P141;
uses U4;
var s : string;
begin
s:=BBB;
writeln(U5.k=, U5.k);
writeln(U5.m=, U5.m);
writeln(U5.s=, U5.s);
writeln(U4.m=, U4.m);
writeln(U4.s=, U4.s);
writeln(m=, m);
writeln(s=, s);
readln;
end.
Unit U4;
interface
uses U5;
var m : real;
s : char;
implementation
begin
m:=4.0;
s:=A;
end.
Unit U5;
interface
var k, m : integer;
s : real;
implementation
begin
k:=1;
m:=2;
s:=3.0;
end.
:
Program P142;
{}
uses U6;
begin
writeln(k=, k);
writeln(m=, m);
readln;
end.

86

Unit U6;
interface
var k : integer;
implementation
var m : integer;
begin
k:=1;
m:=2;
end.
U1 , :
a) ;
) ;
) ;
) ;
) .
,
.
n, n 10254, ,
: +, , 0, 1, 2, , 9. ,
, :
a) ;
)
) +, , *, mod, div;
) ;
) .
Turbo PASCAL n, n 500 :
type lungime = 0..500;
SirDeCaractere = record
n: lungime;
s: array [1..500] of char
end;
, , :
a) ;
) ;
) () ;
) ;
) .
:
a) ;
) ;
) ;

87

) ;
) m- .
, 2.
Turbo PASCALs Online Help , . ,
.

3.2.
, :
a) ;
) , .
.
,
. ,
, . .
, . , .
:
;
.
, ,
.

, .
, .
,
.
.
. 143. , .
:
;
.
, .

88

:
a) :
;
, ;
, ;
) :
;
.

: , ,
, . . :
a) ( c, , goto);
) for , ;
) if, repeat, while true,
false ;
) , case.
. , :
Program P143;
{C }
var n, k : integer;
x, s : real;
begin
n:=0;
k:=0;
s:=0;
writeln( :);
while not eof do
begin
readln(x);
n:=n+1;
if x>0 then
begin
k:=k+1;
s:=s+x;
end;
end; { while }
if n=0 then writeln( )
else if k=0 then writeln(
)
else writeln(=, s/k);
readln;
end.

89

while if
true false : not eof, x>0, n=0 k=0.
, :
(not eof = false, n=0);
(not eof = true, n0);
, , , (x>0, k0);
,
(x0, k=0).

,
. , 143 , , , ,
, , ,
.
k:=k+1; s:=s+x s/k
k s.
- (if, case), (for,
while, repeat) (goto). , , .
() , , . ( ) ( ).
. ,
.
.
:
;
;
;
;
;
-, ..

Turbo PASCALs Online Help.
, ,
. , , ,
, , .

90

, .
.
, , . , , . ,
, ,
, .


?
?
?
124 2.4.
:
;
;
;
, .
, ,
:
a) P117 P120 2.1;
) P122 P123 2.2;
) P127 2.5 P128 2.6.
?
:
a) P117 P120 2.1;
) P122 P123 2.2;
) P127 2.5.
?
Turbo PASCALs Online Help . .

3.3.
, ,
.
, , , .

91


, , . ,

:
( /
);
(if then if then else);
(while do...).
, :
(case of...);
(repeat until);
(for do).
:
1.
: , ,
, .
2. , ,
.
3. 50100 . .
4. , , , , , , .
5. ,
.
6. - . .
7. if case.
, , ,
, . ,
, goto.
, ,
.


?
,
.
.
?

92

124, 130 135 2 ?


144
n .
Program P144;
var a,i,l,s,n : integer;
b : boolean;
begin
write(n=); readln(n);
b:=true;
for i:=1 to ((n+1) div 2) do
begin
a:=i;
s:=0;
while (s<n) do
begin
s:=s+a;
a:=a+1;
end;
if s=n then
begin
b:=false;
write(n, =, i);
for l:=i+1 to (a-1) do write(+,l);
writeln;
end;
end;
if b then writeln( );
readln;
end.
, n = 15 :
15=1+2+3+4+5;
15=4+5+6;
15=7+8.
, :
1, 2, 3, ..., k;
2, 3, ..., k;
3, ..., k
. ., k = (n+1) div 2. 1, 2, 3, k
a = a+1.

.

93

4

4.1.
. ,
(, ),
, .
, : , , , , , . , : , ,
, .
(, ) .
,
. :
n , ()
. n , ,
..;
V(n) ,
;
T(n) , .
.
, V(n) T(n) ,
, n . , V T n.
, ,
.
, ,
, A1 A2.
A1 :

94

V1(n) = 100n2 + 4;
T1(n) = n3 10-3 ,

A2:

V2(n) = 10n + 12;


T2(n) = 2n 10-6 .
,
.
A1, A2 n 4.1.
4.1
A1 A2
n

10

20

30

40

50

V1(n)

9,77

39,06

87,89

156,25

244,14

V2(n)

112

212

312

412

512

T1(n)

16

25

18

13

36

T2(n)

0,001 1,05

4.1 , A2 n>30.
A1 , V1(n)
, (64
Turbo PASCAL 7.0).
V(n) T(n)
.
, ,
.
,
, , .


. ,
.
, ?
?
?
A1 A2 ( 4.1) Turbo PASCAL 7.0. , : a) n = 10; b) n = 20; c) n = 30?
n A1 Turbo PASCAL 7.0?

95

, A3,
V3(n) = 600n3 + 18;
T3(n) = 3 n 10 -2 .
, n A3 Turbo PASCAL 7.0?
,
PASCAL.

4.2.
V(n)
, .
, integer, real,
boolean, char, , , . , Turbo PASCAL 7.0 4.2.
4.2
Turbo PASCAL 7.0

integer

real

boolean

char

pointer

, .
, A, B, p s
var A
B
p
s

:
:
:
:

array[1..n, 1..n] of real;


array[1..n] of integer;
boolean;
string[10];

96

V(n) = 6n2 + 2n + 11 ().

, .
(. 4.1):
, . var
;
, , , , .
,
. , - , -
;
(heap), . new dispose.

Vs(n)

Vd(n)


;

;

;

Vh(n)

. 4.1.

, (. 4.1):
Vd(n) ,
;
Vs(n) , ;
Vh(n) , .
Turbo PASCAL 7.0 , Vd(n) 64 , Vs(n)
16 Vh(n) 256 .
.

97

:
Program P145;
{ }
const n = 100;
type Matrice = array[1..n, 1..n] of real;
Vector = array[1..n] of real;
var A : Matrice;
i : integer;
p, q : ^Matrice;
procedure Prelucrare(var B:Matrice);
var C : Vector;
begin
{... B...}
end; { Prelucrare }
begin
{... A...}
Prelucrare(A);
new(p);
new(q);
{... p^ q^...}
dispose(p);
dispose(q);
{... ...}
writeln();
readln;
end.

A, i, p q P145
(. 4.2). :
Vd(n) = 6n2 + 2 + 2 4 = 6n2 + 10.
Pr(A) A,
C.
, :
Vs(n) = 6n + 8.
.
new(p) new(q) p^
q^ Matrice. ::
Vh(n) = 6n2 + 6n2 = 12n2 .
dispose(p) dispose(q)
, .

98


, ,
, .
, ,
?

q
q^

p
q

i
C
A

p^

. 4.2. P145

,
.
, , .
?
,
:
a)

var A : array[1..n, 1..n] of integer;


B : string;
C : array [1..n, 1..n, 1..n] of boolean;

b)

type Vector = array[1..n] of real;


Matrice = array[1..n] of Vector;
var A, B, C : Matrice;
D : Vector;

c)

type Elev = record


Nume : string;
Prenume : string;
NotaMedie : real
end;
ListaElevi = array[1..n] of Elev;
var A, B, C : ListaElevi;

99

d)

type Angajat = record


NumePrenume : string;
ZileLucrate : 1..31;
PlataPeZi : real;
PlataPeLuna : real
end;
ListaDePlata = array[1..n] of Angajat;
var L1, L2 : ListaDePlata;

e)

type Elev = record


Nume : string;
Prenume : string;
NotaMedie : real
end;
FisierElevi = file of Elev;
var FE : FisierElevi;
E : Elev;
str : string;
i, n : integer;

, . 50, 60, 70, 80 100 n.


.
Program P146;
{ }
const n = 50;
type Matrice = array[1..n, 1..n] of real;
var A, B : Matrice;
begin
{... ...}
{... A B...}
writeln();
readln;
end.
:
Program P147;
{ }
var n : integer;
function S(n:integer):real;
begin
if n=0 then S:=0
else S:=S(n-1)+n;
end; { S }

100

begin
write(n=); readln(n);
writeln(s=, S(n));
readln;
end.
:
S(n) = 0 + 1 + 2 + ... + n
:

, P147. n, P147 .
, .
n ?
Program P148;
{ (heap) }
type Vector = array[1..100] of real;
var p : ^Vector;
i, n : integer;
begin
write(n=); readln(n);
for i:=1 to n do new(p);
writeln();
readln;
end.

4.3.
T(n) . U7:
Unit U7;
{ i }
interface
function TimpulCurent : real;
implementation
uses Dos;
var ore : word;
minute : word;
secunde : word;
sutimi : word;

101

function TimpulCurent;
begin
GetTime(ore, minute, secunde, sutimi);
TimpulCurent:=3600.0*ore+60.0*minute+
1.0*secunde+0.01*sutimi;
end; { TimpulCurent }
end.

U7
TimpulCurent,
, . (, , )
GetTime, DOS
Turbo PASCAL 7.0.
P149,
Sortare:
Program P149;
{ Sortare }
uses U7;
type Vector = array[1..10000] of real;
var
A : Vector;
i, n : integer;
T1, T2 : real; { }
procedure Sortare(var A:Vector; n:integer);
{ A }
var i, j : integer;
r : real;
begin
for i:=1 to n do
for j:=1 to n-1 do
if A[j]>A[j+1] then
begin
r:=A[j];
A[j]:=A[j+1];
A[j+1]:=r;
end;
end; { Sortare }
begin
write( n=);
readln(n);
{ (n, n-1, ..., 3, 2, 1) }
for i:=1 to n do A[i]:=n-i+1;
T1:=TimpulCurent;

102

Sortare(A, n);
T2:=TimpulCurent;
writeln( , (T2-T1):7:2,
readln;
end.

);

Sortare A . A n , n1 A[j] A[j+1].


A[j]>A[j+1], .
,
P149 A :
A = (n, n-1, n-2, ..., 3, 2, 1).
, n=4 :
A=(4, 3, 2, 1).
:
i = 1,
A = (3, 2, 1, 4);
i = 2,
A = (2, 1, 3, 4);
i = 3,
A = (1, 2, 3, 4);
i = 4,
A = (1, 2, 3, 4).
Sortare Pentium 500 MHz 4.3,
. 4.3.
4.3
Sortare
n

1000

2000

3000

4000

5000

6000

7000

8000

9000 10000

T(n), s

0,27

1,10

2,47

4,50

7,03

10,16 13,84 18,02 22,85 28,18


, , ,
?
Sortare ( P149)
, . , . 4.3.

.
a)

procedure N2(n : integer);


var i, j, k : integer;
r : real;

103

t, s
30

20

10

2000

4000

6000 8000 10000

. 4.3. Sortare

begin
for i:=1 to n do
for j:=1 to n do
for k:=1 to 300 do
r:=1.0;
end; { N2 }

104

b)

procedure N3(n : integer);


var i, j, k : integer;
r : real;
begin
for i:=1 to n do
for j:=1 to n do
for k:=1 to n do
r:=1.0;
end; { N3 }

c)

procedure N4(n :
var i, j, k, m :
r :
begin
for i:=1 to n

integer);
integer;
real;
do

for j:=1 to n do
for k:=1 to n do
for m:=1 to n do
r:=1.0;
end; { N4 }
, . 4.4
Pentium, 500 MHz.
TimpulCurent?
.
t, s
13
12

N4
N3

11
10
9
8
7
6
5
4
3

N2

2
1
0

2000

4000

6000

8000 10000 n

.4.4. N2, N3 N4

4.4.
T(n) . ,
T(n).
, ,
.
, ,
(+, -, or, *, /, div, and, <, <=, not .) -

105

. [ ], :=
goto. ,
10 -9 ... 10 -7 . T(n)
T(n) = Q(n) ,
Q(n) , , , .. .
, E m k
F. , QE ,
E,
QE = m + k QF ,
QF ,
F.
:
a)

E
a*b+c

QE
2

b) (a<b)or(c>d)
c)

sin(x)+cos(y)

d)

a+M[i]

e)

sin(x+y)+sin(x-y)

3
1 + Qsin + Qcos
2
3 + 2Qsin

QI , I , 4.4.
4.4
,

1
1

:= E


3
QE + 1

QP + 1

3
4

106

if E then I1 else I2

case E of I1; I2; ...; Ik end

QE + max{QI , QI } + 1
1

QE + max{QI , QI , ..., QI } + k + 1
1


for := E1 to/downto E2 do I

QE + QE + mQI + m + 1


while E do I

(m + 1)QE + mQI + 1

1
7
8
9
10

2

repeat I until E

begin I1; I2; ...; Ik end

with do I
goto

3
mQI + mQE + 1
QI + QI + ... + QI + 1
1

QI + 1
1

4.4 . 4.4
:
v ;
E ;
I .
I for, while repeat
m. , if goto, .
Q(n),
:
procedure Sortare(var A:Vector; n:integer);
var i, j : integer;
r : real;
{1} begin
{2} for i:=1 to n do
{3}
for j:=1 to n-1 do
{4}
if A[j]>A[j+1] then
{5}
begin
{6}
r:=A[j];
{7}
A[j]:=A[j+1];
{8}
A[j+1]:=r;
end;
end; { Sortare }

I1, I2, ... , I8 Sortare {1}, {2}, ..., {8} . Qj


, Ij :
Q6 = 2;
Q7 = 4;
Q8 = 3;
Q5 = Q6 + Q7 + Q8 + 1 = 10;
Q4 = 4 + Q5 + 1 = 15;
Q3 = 0 + 1 + (n1)Q4 + (n1) + 1 = 16n 14;

107

Q2 = 0 + 0 + nQ3 + n + 1 = 16n2 13n + 1;


Q1 = Q2 +1 = 16n2 13n + 2.
,
Q(n) = 16n2 13n + 2,
Sortare
T(n) = (16n2 13n + 2) .
, () . ,
, . , .
T(n), , , .
, Sortare ( 4.3) n = 10000 T(n) =
= 28,18 .
(16n2 13n + 2) = 28,18
1,8 10 -8 .
, Turbo PASCAL 7.0 Pentium c 500
MHz,
Sortare.
, Pentium c 150 MHz 6,0 10 -8 .


P149
, .
QI,
:

108

a)

x:=2*a-6*(y+z);

b)

p:=not(a=b)and(c>d);

c)

p:=(a in R)and(b in P);

d)

if a>b then x:=0 else x:=a+b;

e)

case i of
1: x:=0;
2: x:=a+b;
3: x:=a+b+c;
end;

f)

for i:=1 to n do A[i]:=2*A[i];

g)

for i:=1 to n do A[i]:=B[i+1]-C[i-2];

h)

i:=0; while i<n do begin i:=i+1 end;

i) i:=n; repeat i:=i-1 until i=0;


j) begin i:=0; s:=0; r:=0 end;
k) with A do begin x:=0; y:=0 end.
Q(n) :
a)

procedure N2(n : integer);


var i, j, k : integer;
r : real;
begin
for i:=1 to n do
for j:=1 to n do
for k:=1 to 300 do
r:=1.0;
end; { N2 }

b)

procedure N3(n : integer);


var i, j, k : integer;
r : real;
begin
for i:=1 to n do
for j:=1 to n do
for k:=1 to n do
r:=1.0;
end; { N3 }

c)

procedure N4(n : integer);


var i, j, k, m : integer;
r : real;
begin
for i:=1 to n do
for j:=1 to n do
for k:=1 to n do
for m:=1 to n do
r:=1.0;
end; { N4 }

Sortare Pentium f1 = 500 MHz 1 1,8 10 -8 . , f2 =


=150 MHz 2 6,0 10 -8 . , ,

109


, ?
.
. , ,
.
,
. , ,
. , ,
, .
, .
Mips () . , 500800 Mips. . ,
, -
. , , .

4.5.

T(n) Q(n).
108 ... 1010
, n. , ,
Q(n), , .. , n.
4.5.
4.5

n

log2 n

n2

n3

n4

2n

16

16

64

256

16

64

512

4096

256

16

256

4096

65536

65536

32

1024

32768

1048576

4294967296

110

, Sortare :
Q(n) = 16n2 13n + 2.
16n2. ,
n :
:

Q(n) 16n2,
T(n) 16n2.

, :
;
;
- .
,
Cnk,
Q(n) Cnk ; T(n) Cnk,
n , C ,
k .
O(nk),
nk, , nk. , n, n2, n3 ..
, n2. T(n) Sortare,
. 4.3.
, Ckn,
Q(n) Ckn;

T(n) Ckn,

k>1. O(kn).
,
log
n n, .
- .
( 4.5) , n. 4.1, T1(n) O(n3) T2(n) O(2n).
, , . ,
,

111

. ,
, n. n .
, ,
,
T1(n), T2(n):
T1(n) = 1000n2 ;
T2(n) = 2n.
, n = 1, 2, 3, ...,
18 T2(n) < T1(n). , n 18 .
:
;
-
;
.
, ..
nk, , k . ,
. - .


:
a)

12n + 5;

b)

6n2 + 100n + 18;

c)

15n3 + 1000n2 25n + 6000;

d)

2000n3 + 2n + 13;

e)

nlog2n + n5 + 300n2 + 6;

f)

3n + 2n + 14n3 + 21;

g)

n5 + 10n4 + 200n3 + 300n2 + 1000n.

?
:

112

a)

Q(n) = 200n + 15;

b)

Q(n) = 2n + 25n2 + 1000;

c)

Q(n) = n3 + 3n + 6000n2 + 106;

d)

Q(n) = 3n + 2n + n10 + 4000

e)

Q(n) = nlog2n + n3 + n2 + 1500;

f)

Q(n) = 100n2 + 15n3 + 8n + 900.

,
?
N2, N3 N4 . :
QN2(n) = 602n2 + 2n + 2;
QN3(n) = 2n3 + 2n2 + 2n + 2;
QN4(n) = 2n4 + 2n3 + 2n2 + 2n + 2.
, . TN2(n), TN3(n) TN4(n), . 4.4.
, k :
for i1:=1 to n do
for i2:=1 to n do
...
for ik:=1 to n do P
QP , P, . .
:
A, n . ,
B, BA, m. ,
A={3, 1, 5, 9} m=7, , B={3, 1, 9}.
.

113

5.1. ?
, , : , , , .
. , ,
, . , , .
, , :
. ,
,
:
, ;
, , .
, fact: NN,

:
n = 0. fact(0) , fact(0) = 1;
n > 0. fact(n)
, fact(0).
, n = 3 :
fact(3) = 3 fact(2) = 3 2 fact(1) = 3 2 1 fact(0) = 3 2 1 1 = 6.
, fact(n) . , fact(n) PASCAL, :

114

function Fact(n:Natural):Natural;
begin
if n=0 then Fact:=1
else Fact:=nFact(n-1)
end;

Fact(3) . 5.1.

Fact(3)

Fact(2)

Fact(1)

Fact(0)


Fact = 1

Fact = 1

Fact = 2

Fact = 6

. 5.1. Fact(3):
AR ; n ;
*** f , Fact

incons: NN,

n = 0 n>0.
fact(n), n > 0 incons(n) , incons().

115

, n = 3 :
incons(3) = 3 incons(4) = 3 4 incons(5) = 3 4 5 incons(6) = .
,
incons(n) .
, , .
,
,
.
, . .
,
, . , 5.1 , .
5.1

( )

1.

2.

3.

4.

5.


, 2.
,
, , : , , ..
, , , .


.
? .
, ?
?

116

. ? .

a) f : NN,

b) f : NN,
c) f : ZZ,

d) f : NN,
g : NN,
e) f : NN,

div

. , .
Program P150;
{ }
type Natural = 0..Maxint;
function Incons(n:Natural):Natural;
{ }
begin
writeln( n=, n);
if n=0 then Incons:=1
else Incons:=n*Incons(n+1);
end; { Incons }
begin
writeln(Incons(3));
readln;
end.
, . 5.1, Incons(3).
:
a) Arb AfisArb P130;
) Preordine, Inordine Postordine P131;
) InAdincime P133.
S(n) n :

117

5.1, S(n).
:
::= 0123456789
::=
::= +-*/
::= ()
, true, S
<> false .
, :
a) ;
) ;
) ;
) ;
) m.
, true, S <> 9 false . 5.1,
.
,
n. n MaxInt n 10250.
- (. 5.2) B = ||bij||nm, 1 n, m 30. bij
: (bij =1) (bij =0).

a)

. 5.2. :
a ;

118

, (i, j) .
, , . B = ||bij||nm. bij (i, j).
, 13 14.

5.2.
,
si,

S = {s1, s2, , si, , sk},


. , si, si S, - : integer, boolean, char, .
,
(record) . , s1, s2, , sk .
, ,
:
for i:= 1 to k do
if SolutiePosibila(si) then PrelucrareaSolutiei(si)

SolutiePosibila ,
true, si , false
, PrelucrareaSolutiei ,
. si .
,
, .
1. , {0,
1, 2, ..., n}. , , K
m. , n = 100 m = 2,
{0, 1, 2, , 100} 3 , :
2, 11 20. , K = 3.
. , S = {0, 1, 2, , n}.
i, i S, SumaCifrelor.
i i 10.

119

Program P151;
{ }
type Natural=0..MaxInt;
var i, K, m, n : Natural;
function SumaCifrelor(i:Natural):Natural;
var suma : Natural;
begin
suma:=0;
repeat
suma:=suma+(i mod 10);
i:=i div 10;
until i=0;
SumaCifrelor:=suma;
end; { SumaCifrelor }
function SolutiePosibila(i:Natural):boolean;
begin
if SumaCifrelor(i)=m then SolutiePosibila:=true
else SolutiePosibila:=false;
end; { SumaCifrelor }
procedure PrelucrareaSolutiei(i:Natural);
begin
writeln(i=, i);
K:=K+1;
end; { PrelucrareaSolutiei }
begin
write( n=); readln(n);
write( m=); readln(m);
K:=0;
for i:=0 to n do
if SolutiePosibila(i) then PrelucrareaSolutiei(i);
writeln(K=, K);
readln;
end.

P151 , O(n).
2. P = {P1, P2, , Pn}, n
(2 n 30) . Pj xj, yj. ,
Pa, Pb , .
. S = PP. (Pj, Pm)
PP :

120

for j:=1 to n do
for m:=1 to n do
if SolutiePosibil(Pj, Pm) then PrelucrareaSolutiei(Pj, Pm)

Pj, Pm :
.
Program P152;
{ }
const nmax=30;
type Punct = record
x, y : real;
end;
Indice = 1..nmax;
var P : array[Indice] of Punct;
j, m, n : Indice;
dmax : real; { }
PA, PB : Punct;
function Distanta(A, B : Punct) : real;
begin
Distanta:=sqrt(sqr(A.x-B.x)+sqr(A.y-B.y));
end; { Distanta }
function SolutiePosibila(j,m:Indice):boolean;
begin
if j<>m then SolutiePosibila:=true
else SolutiePosibila:=false;
end; { SolutiePosibila }
procedure PrelucrareaSolutiei(A, B : Punct);
begin
if Distanta(A, B)>dmax then
begin
PA:=A; PB:=B;
dmax:=Distanta(A, B);
end;
end; { PrelucrareaSolutiei }
begin
write( n=); readln(n);
writeln( x, y );
for j:=1 to n do
begin
write(P[, j, ]: ); readln(P[j].x, P[j].y);
end;
dmax:=0;

121

for j:=1 to n do
for m:=1 to n do
if SolutiePosibila(j, m) then
PrelucrareaSolutiei(P[j], P[m]);
writeln(: PA=(, PA.x:5:2, ,, PA.y:5:2, ));
writeln(
PB=(, PB.x:5:2, ,, PB.y:5:2, ));
readln;
end.

, P152,
O(n2).
, , ,
, , , S. ( 1) .
( 2)
. , , :
;
;
;
;
;
,
..
, , . k S.
.

, , .


.
while repeat?
P151 P152. P152
, .
, .
, ?

122

P = {P1, P2, , Pn} , n (3 n 30)


. Pj xj, yj.
, P, . .
, ,
n, true, n , false . .
(a)x x , a
, . , , ,
(a)x = b,
a b , x . a
{0, 1, 2, , 9}, b
. ,
(160)x = 122
x = 8,
(5)x = 10
. .

N G .
, .
,

10

25

50

, S,
.
, , R . , R , 1R30. d
(x, y, z)

5.3. Greedy
, :
A={a1, a2, ..., an}, n ;
B, BA, , .

123

,
2n
Ai A. ,
.
Ai, AiA,
Greedy ()
A. , . ,
Greedy .
, Greedy, :
while ExistaElemente do
begin
AlegeUnElement(x);
IncludeElementul(x);
end

ExistaElemente true,
A , .
AlegeUnElement A x, IncludeElementul B.
B .
, Greedy A B. , B A, (greedy
, ).
. A={a1, a2, ..., ai, ..., an},
ai>0. , B, BA, ,
B . ,
A={21,5; 3,4; 0; 12,3; 83,6} B={21,5; 83,6}.
. , B, BA, b0, B\{b} B.
, :
B A.
A B
( ) A B,
n m. B , m=0.
Program P153;
{ Tehnica Greedy }
const nmax=1000;
var A : array [1..nmax] of real;
n : 1..nmax;

124

B
m
x
i

:
:
:
:

array [1..nmax] of real;


0..nmax;
real;
1..nmax;

Function ExistaElemente : boolean;


var i : integer;
begin
ExistaElemente:=false;
for i:=1 to n do
if A[i]>0 then ExistaElemente:=true;
end; { ExistaElemente }
procedure AlegeUnElement(var x : real);
var i : integer;
begin
i:=1;
while A[i]<=0 do i:=i+1;
x:=A[i];
A[i]:=0;
end; { AlegeUnElement }
procedure IncludeElementul(x : real);
begin
m:=m+1;
B[m]:=x;
end; { IncludeElementul }
begin
write( n=); readln(n);
writeln( A:);
for i:=1 to n do read(A[i]);
writeln;
m:=0;
while ExistaElemente do
begin
AlegeUnElement(x);
IncludeElementul(x);
end;
writeln( B:);
for i:=1 to m do writeln(B[i]);
readln;
end.

, AlegeUnElement A,
x, .
, Greedy,
, .

125

, ExistaElemente, AlegeUnElement
IncludeElementul, n. while n . , ,
Greedy, . , , Ai, Ai A,
O(2n), .. . ,
Greedy , , A.


, Greedy.
, Greedy?
P153.
, B . .
. n f1, f2, ..., fn ,
. ,
,
. , (
) , fi (i=1, 2, ..., n) ti .
. n . i
(i=1, 2, ..., n) gi ci, . , , Gmax. ,
, ,
C . ,
.
. *
, , (. 5.3). : ,
, , ,
. ,
, .
,
. ,
.
. . ,
. ,
*

126

.
A m n . A[i, j] : 0 ; 1 ; 2 ; 3
. , A[i, j]=2.

. 5.3.

HRUBE.IN m, n, .
m n A[i, j] , .
. HRUBE.OUT
, , , ,
.
. 5 m, n 100.
3 .
:
HRUBE.IN
HRUBE.OUT
7 9

1 1 1 1 1 1 1 1 1

1 0 0 1 0 0 0 0 1

1 0 1 0 0 1 0 1 1

1 0 0 0 1 0 0 0 1

1 0 1 0 1 0 1 0 1

1 0 0 0 0 0 1 0 1

1 2 1 1 1 1 1 3 1

. {, , ,
} , .

127

5.4.
, , ,
X=(x1, x2 , ..., xk , ... , xn).
xk X , Ak, k = 1, 2, ..., n. , mk
Ak
, .
, ,
X S = A1A2
... An. , . ,
A1, A2, ..., An , , O(2n), .. .
A1A2 ...
An, X , .. xk , x1, x2 , ..., xk-1. ,
xk xk+1 ,
, x1, x2 , ..., xk. ,
xk+1. , xk , Ak
, k ,
xk-1.
, k
. , x1, x2 , ..., xk-1. backtracking (back , track ).
, . 5.4 ,
A1 = {a11, a12}, A2 = {a21, a22} A3 = {a31, a32, a33}.
, A1 A2 (m1 = 2, m2 =
= 2), A3 (m3 = 3). akj .
0 (false) 1 (true).
. 5.4 , a11 A1
, , a12
. X a21
A2, , A3.
a31, a32, a33 , A2, , a22. A3,
a32 .

128

k:=1

A1
k:=k+1

A2
k:=k+1

k:=k+1
0

k:=k1
0

A3

. 5.4.

,
, :
procedure Reluare(k:integer);
begin
if k<=n then
begin
X[k]:=PrimulElement(k);
if Continuare(k) then Reluare(k+1);
while ExistaSuccesor(k) do
begin
X[k]:=Succesor(k);
if Continuare(k) then Reluare(k+1)
end; { while }
end { then }
else PrelucrareaSolutiei;
end; {Reluare}

Reluare ,
X A1, A2, ..., An. :
PrimulElement(k) Ak;
Continuare(k) true, ,
k X,
false ;
ExistaSuccesor(k) true,
Ak , xk , false ;
Succesor(k) , Ak xk;

129

PrelucrareaSolutiei , X , .

Reluare , . 5.4.
Reluare(1) x1 X A1:
X=(a11).
Continuare(1)
false, while.
ExistaSuccesor(1) true , , x1 X a11:
X=(a12).
Continuare(1) true , , Reluare(2). ,
x2 X A2:
X=(a12, a21).
Continuare true, Reluare(3).
Reluare(3) x3 X a31, a32 a33:
X=(a12, a21, a31);
X=(a12, a21, a32);
X=(a12, a21, a33),
.
A3 ExistaSuccesor false , ,
Reluare(2). x2 X
, a21:
X=(a12, a22).
Continuare
true, Reluare(3). ,
,
X=(a12, a22, a32)
,
Reluare(4). k>n , , PrelucrareaSolutiei
X .
. A1, A2, ..., An,
mk .
, q.
. A1, A2, ..., An
() A = ||akj||. mk Ak () M = ||mk||.

130

,
, , k X,
q k<n q k = n. X j aij,
A1, A2, ..., An.
Program P154;
{ , }
const mmax=50; { }
nmax=50; { }
type Natural = 0..MaxInt;
Multime = array [1..nmax] of Natural;
var

A : array[1..nmax] of Multime;
n : 1..nmax;
{ }
M : array[1..nmax] of 1..mmax; { S[k] }
X : array[1..nmax] of 1..mmax; {
}
q : Natural;
k, j : integer;
Indicator : boolean;

function PrimulElement(k : integer) : Natural;


begin
PrimulElement:=1;
end; {PrimulElement }
function Continuare(k : integer) : boolean;
var j : integer;
suma : Natural;
begin
suma:=0;
for j:=1 to k do suma:=suma+A[j, X[j]];
if ((k<n) and (suma<q)) or ((k=n) and (suma=q))
then Continuare:=true
else Continuare:=false;
end; { Continuare }
function ExistaSuccesor(k : integer) : boolean;
begin
ExistaSuccesor:=(X[k]<M[k]);
end; { ExistaSuccesor }
function Succesor(k : integer) : integer;
begin
Succesor:=X[k]+1;
end; { Succesor }

131

procedure PrelucrareaSolutiei;
var k : integer;
begin
write(: );
for k:=1 to n do write(A[k, X[k]], );
writeln;
Indicator:=true;
end; { PrelucrareaSolutiei }
procedure Reluare(k : integer);
{ }
begin
if k<=n then
begin
X[k]:=PrimulElement(k);
if Continuare(k) then Reluare(k+1);
while ExistaSuccesor(k) do
begin
X[k]:=Succesor(k);
if Continuare(k) then Reluare(k+1);
end { while }
end { then }
else PrelucrareaSolutiei;
end; { Reluare }
begin
write( n=); readln(n);
for k:=1 to n do
begin
write( A[, k, ]=); readln(M[k]);
write( A[, k, ]: );
for j:=1 to M[k] do read(A[k, j]);
writeln;
end;
Write( q=); readln(q);
Indicator:=false;
Reluare(1);
if Indicator=false then writeln( );
readln;
end.

Reluare
A1, A2, ..., An (. 5.4) , ,
, X A1A2...An. , , , ,
O(mn), m=max(m1, m2, ..., mn).

132

, , ,
akj
A1, A2, ..., An. . ,
A1, A2, ...,
An , , n.
, ,

, . , , . , , A1, A2, ..., An
, X
, .


, .

Reluare.
Reluare.
, .
, . 5.4, , A1, A2, ..., An P154:
a) A1={1}, A2={2}, A3={3}, q=4;
b) A1={1}, A2 ={2, 3}, A3={4, 5}, q=10;
c) A1={1, 2, 3}, A2={4, 5}, A3={6}, q=14;
d) A1={1, 2}, A2={3, 4}, A3={5, 6}, A4={7, 8}, q=20.
, A1A2...An.
.
. 5.4 A1, A2, A3
.
B={b1, b2, ..., bn}, n . , ,
Bi, BiB, q .
. Bi
X = ||xk||n,

, Bi , x1+x2+...+xn=q.

133

. n , n30.
A=||aij||nm,

i, j ;
.
m, .
,
.
. ,
(. 5.5). , .

. 5.5.


A=||aij||nm, 1n, m30,

(i, j) ;
.
, . , , B C.
n (n30) , 1, 2, 3, ..., n. i, (i = 1, 2, 3, ..., n), mi Vi.
, , , S p , .
, n k . ,
n=9 k=3 1+2+6, 2+3+4 1+3+5.
,
,
8 12.

134

5.5.
( divide et impera)
, :
1)
, ;
2) ,
, ;
3) .
, :
A = (ai, ai+1, ..., aj)
.
,
m = (j i) div 2
A ,
:
A1 = (ai, ai+1, ..., ai+m); A2 = (ai+m+1, ai+m+2, ..., aj).
, A1 A2 A11,
A12 A21, A22 . ,

, .
, . 5.6 A=(a1, a2, ...,
a7) .
A=(a1, a2, a3, a4, a5, a6, a7)

A1=(a1, a2, a3, a4)

A1-1=(a1, a2)

A1-2=(a3, a4)

A2=(a5, a6, a7)

A2-1=(a5, a6)

A2-2=(a7)

. 5.6. A

, , :
procedure DesparteSiStapineste(i, j : integer; var x : tip);
var m : integer;
x1, x2 : tip;

135

procedure DesparteSiStapineste(i, j : integer; var x : tip);


var m : integer;
x1, x2 : tip;
begin
if SolutieDirecta(i, j) then Prelucrare(i, j, x)
else
begin
m:=(j-i) div 2;
DesparteSiStapineste(i, i+m, x1);
DesparteSiStapineste(i+m+1, j, x2);
Combina(x1, x2, x);
end;
end;

i j
(ai, ai+1, ..., aj), . SolutieDirecta true, ,
false . Prelucrare x , .
, (ai, ai+1, ..., am) (ai+m+1, ai+m+2, ..., aj).
Combina x1 x2 x .
1. A={a1, a2, ..., an}, n . ,
.
. A
n (). , ,
(ai, ..., aj) . , x = ai x = max(ai , aj ).
Program P155;
{
}
const nmax=100;
var A : array[1..nmax] of real;
i, n : 1..nmax;
x : real;
function SolutieDirecta(i, j : integer) : boolean;
begin
SolutieDirecta:=false;
if (j-i<2) then SolutieDirecta:=true;
end; { SolutieDirecta }

136

procedure Prelucrare(i, j : integer; var x : real);


begin
x:=A[i];
if A[i]<A[j] then x:=A[j];
end; { Prelucrare }
procedure Combina(x1, x2 : real; var x : real);
begin
x:=x1;
if x1<x2 then x:=x2;
end; { Combina }
procedure DesparteSiStapineste(i, j : integer; var x : real);
var m : integer;
x1, x2 : real;
begin
if SolutieDirecta(i, j) then Prelucrare(i, j, x)
else
begin
m:=(j-i) div 2;
DesparteSiStapineste(i, i+m, x1);
DesparteSiStapineste(i+m+1, j, x2);
Combina(x1, x2, x);
end;
end; { DesparteSiStapineste }
begin
write( n=); readln(n);
writeln( , n, );
for i:=1 to n do read(A[i]);
writeln;
DesparteSiStapineste(1, n, x);
writeln( x=, x);
readln;
readln;
end.

P155 DesparteSiStapineste

. ,
, .
,
.
2. LH. n .
(xi, yi). ,
.

137


, , (. 5.7).

a)

. 5.7. : a ;
;

. P=(a, b, c, d), a
b , c d . , (0, 0, L, H).
:
Smax= 0;
, Smax;
, (xi, yi), , 5.7:
P1=(a, b, xi, d), P2=( xi, b, c, d) P3=(a, yi, c, d), P4=(a, b, c, yi);
, ,
, Smax .
Program P156;
{ }
const nmax=100;
var L, H : real;

138

n : 1..nmax;
X,Y : array[1..nmax] of real;
Smax, amax, bmax, cmax, dmax : real;
i : integer;
function SolutieDirecta(a, b, c, d : real;
var i : integer) : boolean;
label 1;
var j : integer;
begin
SolutieDirecta:=true;
for j:=1 to n do
if (X[j]>a) and (X[j]<c) and (Y[j]>b) and (Y[j]<d) then
begin
SolutieDirecta:=false;
i:=j;
goto 1;
end;
1:end; { SolutieDirecta }
procedure PrelucrareaSolutiei(a, b, c, d : real);
var S : real;
begin
S:=(c-a)*(d-b);
if S>=Smax then
begin
Smax:=S;
amax:=a; bmax:=b; cmax:=c; dmax:=d;
end;
end; { PrelucrareaSolutiei }
procedure DesparteSiStapineste(a, b, c, d : real);
var i : integer;
begin
writeln( (, a:5:1, , b:5:1, ,
c:5:1, , d:5:1, ));
readln;
if SolutieDirecta(a, b, c, d, i)
then PrelucrareaSolutiei(a, b, c, d)
else begin
DesparteSiStapineste(a, b, X[i], d);
DesparteSiStapineste(X[i], b, c, d);
DesparteSiStapineste(a, Y[i], c, d);
DesparteSiStapineste(a, b, c, Y[i]);
end;
end; { DesparteSiStapineste }

139

begin
writeln( L, H); readln(L, H);
write( n=); readln(n);
writeln( X[i], Y[i]);
for i:=1 to n do read(X[i], Y[i]);
writeln;
Smax:=0;
DesparteSiStapineste(0, 0, L, H);
writeln( (,
amax:5:1, , bmax:5:1, ,
cmax:5:1, , dmax:5:1, ));
writeln(Smax=, Smax:5:2);
readln;
end.

SolutieDirecta P156 true,


(a, b, c d) false .
false, i .
a<xi<c b<yi<d. PrelucrareaSolutiei S=(ca)(db) Smax. SSmax,
(amax, bmax, cmax, dmax).
, , k , ,

. ,
n log2 n n2 log2 n, .. .
, , ,
. ,
, , .
, , , .


, .
?
, ,
A={a1, a2, ..., an}, n .
, , . 5.7, P156:
: 3 4;
: 3;
: (1, 1); (1, 2); (2, 2).

140

P155 P156.
,
. . , , , .
. A={a1, a2, ..., an}, . ,
, A p. .
, ,
a1, a2, ..., an.
. , (a1, a2, ..., an) :
;
,
;
, ()
.
,
. , (3, 4, 18) (2, 1, 15), (3, 2, 1, 4, 15, 18).
.* , 1, 2 3,
n (. 5.8).
1,
. , 2 3
:
;
.

. 5.8.
*

, ,
64 , .

141

. i j (i, j), i, j{1, 2, 3}, ij. H(m, i, j) , m (, , ) i j. ,


H(1, 1, 2)=(1, 2);
H(2, 1, 2)=(1, 3), (1, 2), (3, 2);
H(3, 1, 2)=(1, 2), (1, 3), (2, 3), (1, 2), (3, 1), (3, 2), (1, 2).
,

k=6ij. , n
(n1) .

5.6.
, (d1, d2, ..., dp, ..., dq), .
dp , , , , , , , .. , , : (d1, d2, ..., dp, dp+1, ..., dq) ,
(d1, d2, ..., dp) (dp+1, ..., dq) .
,
,
. , .
,
. ,
:
1) dp dp+1, ..., dq.
, . dq, dq-1, ..., d1.
2) dp d1, ..., dp-1. , .
d1, d2, ..., dq.
,
,
, .
, .

142

.
nm , 1 (.
5.9). , - , .
(i, j), 1in, 1jm, aij . , :

. ,
Cmax , .

. 5.9.

.
A = ||aij||nm, aij , (i, j). , :
1
2 3 4 5

A=


C = ||cij||nm, cij , , (1,1)
(i, j). , Cmax = cnm.
, (i, j)
: (i, j1) (i1, j). , ,
(i, j), :
cij = aij + max(ci,j1, ci1,j).
cij C , :

143

1: c11;
2: c21, c12;
3: c31, c22, c13;
...
n+m-1: cnm.
, k cij C,
i+j1=k. ,
cij A, . 5.10.
k=1

k=2

k=3

k=4

k=5

k=6

k=8

k=8

. 5.10. C

, i j,
cij C.
Program P157;
{ }
var A , C : array [1..50, 1..50] of real;
m, n, i, j, k : integer;
function Max(a, b : real) : real;
begin
if a>b then Max:=a else Max:=b;
end; { Max }
begin
write( n, m: ); readln(n, m);
writeln( A);
for i:=1 to n do
for j:=1 to m do read(A[i,j]);
writeln;

144

C[1,1]:=A[1,1];
for i:=2 to n do C[i,1]:=A[i,1]+C[i-1,1];
for j:=2 to m do C[1,j]:=A[1,j]+C[1,j-1];
for k:=2 to n+m-1 do
for i:=2 to n do
for j:=2 to m do
if (i+j-1)=k then
C[i,j]:=A[i,j]+Max(C[i,j-1], C[i-1,j]);
writeln(Cmax=, C[n,m]);
readln;
end.

, ,
. , P157 O(n3).


.
?
.
, .
P157.
, , ?
. ,
, Gmax.
i (i=1, 2, ..., n) gi ci, . , ,
, C . .
. n . . i
j cij . ,
, : cij < cik + ckj. , i j.
. , .

, ,
. , ,
:

145

1) 0;
4) 110;
2) 1;
5) 1101101,
3) 000;
10001101101110 2354.
, , .
. , . ,
P1P2...Pn (xi, yi) Pi, i=1, 2, ..., n. , P1P2...Pn ,
PjPm, jm, j, m{1, 2, ..., n},
. , .

5.7.
( branch and bound) m, .
S = {s1, s2, ..., sn}, . ,
: ,
, - . sisj
, si sj (.
5.11). , ,
- .. ,
,
,
, , ,
, - ..

, :

;
, ;
, .
,
, f :
SR, .
, f(si) -

146

, , si
si.
, , . , . ,
.

. 5.11.

, , :
1. : .
.
2. :
.
3. : . , ( , , ), . .
2 3 , , .
, .
, 5.2 ,
, . 5.11. . ,
, 9, .

147

T 5.2

1.

(s1)

2.

s1

s2, s3, s4

3.

(s3, s4)

4.

(s3)

s4

s8, s9

5.

(s3, s8)

6.

(s8)

s3

s5, s6, s7

7.

(s8, s5, s6, s7)

8.

(s8, s6, s7)

s5

s10, s11

9.

(s8, s6, s7, s10, s11)

10.

(s8, s6, s7, s11)

s10

, . 5.11,
,
s10. , , , (s1, s3, s5, s10).
:
IniializareaListei;
repeat
Ramifica;
Margineste;
until Gasit or ListaEsteVida

, ,
, f : S R. ,
,
. ,
,
. ,
:
f(si) = niv(si) + h(si),
niv(si) si, h(si) (), , si si.
, h(si) ,
, ,
.

148

, ,
f(si) si. ,
,
. ,
, , .


?
?
?
?
? ?
.
-, . , ,
. .
, . 5.11,
:
a) ;
) ;
) .
K , ? .
,
( ).
.
,
.
0, 1 2 ,
, , , , ..
, ,
?

5.8.
, , . (),

149

, : ,
, .. ,
, .

:
, , () , ;
(), ;
;
, , ;
,
, , ..

Perspico, 15. 15
, 1 15 (. 5.12).

a)

. 5.12. Perspico: a ;

44,
. , ,
. , ,
.
Perspico 15
16 . 0,
, :
type Stare = array [1..4, 1..4] of 0..15;

150

,
16 16 :
,
108 .
, , (, , ).
Stare.
,
, . 5.13.

. 5.13. Perspico


, . ,

151

, . , , 1015 . ,
, ,
.
f : S R :
f(si) = niv(si) + h(si),
h(si) , si .
si,
:
h(si) , .
, . 17 :
f(s1) = niv(s1) + h(s1) = 0 + 4 = 4;
f(s2) = niv(s2) + h(s2) = 1 + 5 = 6;
f(s3) = niv(s3) + h(s3) = 1 + 3 = 4;
f(s6) = niv(s6) + h(s6) = 2 + 2 = 4;
f(s7) = niv(s7) + h(s7) = 3 + 0 = 3.
:
type AdresaNod = ^Nod;
Nod = record
S : Stare;
Nivel, Cost : integer;
Drum : boolen;
D : array [1..4] of AdresaNod;
Tata : AdresaNod;
end;
AdresaCelula = ^Celula;
Celula = record
ReferintaNod : AdresaNod;
Urm : AdresaCelula;
end;
var Radacina : AdresaNod;
BazaListei : AdresaCelula;

S, Nivel,
Cost, Drum D, Tata. Drum , , , , . Tata , -.

152


ReferintaNod, , .
Urm . ,
, .
,
Perspico, P158.
Program P158;
{ Perspico }
const NivelMaxim=15;
type Stare=array[1..4, 1..4] of 0..15;
AdresaNod=^Nod;
Nod=record
S : Stare;
Nivel, Cost : integer;
Drum : boolean;
D : array [1..4] of AdresaNod; { }
Tata : AdresaNod;
end;
AdresaCelula=^Celula;
Celula=record
ReferintaNod : AdresaNod;
Urm : AdresaCelula;
end;
var Radacina : AdresaNod;
BazaListei : AdresaCelula;
StareaInitiala, StareaFinala : Stare;
Gasit : boolean; { true }
Finput, Foutput : text; { }
Str : array[1..4] of Stare; { }
m : 0..4;
{ }
i, j : integer;
procedure CalculareaCostului(Adresa : AdresaNod);
var i, j, C : integer;
begin
C:=0;
for i:=1 to 4 do
for j:=1 to 4 do
if Adresa^.S[i,j] <> StareaFinala[i,j] then C:=C+1;
Adresa^.Cost:=Adresa^.Nivel+C;
end; { CalculareaCostului }
procedure Initializare;
{ }
{ }
var i : integer;

153

begin
Gasit:=false;
new(Radacina);
Radacina^.S:=StareaInitiala;
Radacina^.Nivel:=0;
CalculareaCostului(Radacina);
Radacina^.Drum:=false;
for i:=1 to 4 do Radacina^.D[i]:=nil;
Radacina^.Tata:=nil;
new(BazaListei);
BazaListei^.ReferintaNod:=Radacina;
BazaListei^.Urm:=nil;
end; { Initializare }
procedure Ramifica(Adresa : AdresaNod);
{ Str , }
{ }
label 1;
var
St : Stare;
i, j, k : integer;
begin
{ 0 }
for i:=1 to 4 do
for j:=1 to 4 do
if Adresa^.S[i,j]=0 then goto 1;
1: m:=0;
{ }
if i<>1 then
begin
St:=Adresa^.S;
St[i,j]:=St[i-1, j];
St[i-1, j]:=0;
m:=m+1;
Str[m]:=St;
end;
{ }
if i<>4 then
begin
St:=Adresa^.S;
St[i,j]:=St[i+1, j];
St[i+1, j]:=0;
m:=m+1;
Str[m]:=St;
end;
{ }
if j<>1 then
begin
St:=Adresa^.S;

154

St[i,j]:=St[i, j-1];
St[i, j-1]:=0;
m:=m+1;
Str[m]:=St;
end;
{ }
if j<>4 then
begin
St:=Adresa^.S;
St[i,j]:=St[i, j+1];
St[i, j+1]:=0;
m:=m+1;
Str[m]:=St;
end;
end; { Desparte }
procedure IncludeInLista(Adresa : AdresaNod);
{ }
var R : AdresaCelula;
begin
new(R);
R^.ReferintaNod:=Adresa;
R^.Urm:=BazaListei;
BazaListei:=R;
end; { IncludeInLista }
procedure ExtrageDinLista(var Adresa : AdresaNod);
{ }
label 1;
var P, R : AdresaCelula;
C : integer; { costul curent }
begin
if BazaListei=nil then goto 1;
{ }
C:=MaxInt;
R:=BazaListei;
while R<>nil do
begin
if R^.ReferintaNod^.Cost < C then
begin
C:=R^.ReferintaNod^.Cost;
Adresa:=R^.ReferintaNod;
P:=R;
end; { then }
R:=R^.Urm;
end; { while }
{ }

155

if P=BazaListei then BazaListei:=P^.Urm


else
begin
R:=BazaListei;
while P<>R^.Urm do R:=R^.Urm;
R^.Urm:=P^.Urm;
end;
dispose(P);
1:end; { ExtrageDinLista }
function StariEgale(S1, S2 : Stare) : boolean;
{ TRUE, S1 S2 }
var i, j : integer;
Coincid : boolean;
begin
Coincid:=true;
for i:=1 to 4 do
for j:=1 to 4 do
if S1[i,j]<>S2[i,j] then Coincid:=false;
StariEgale:=Coincid;
end; { StariEgale }
function StareDejaExaminata(St : Stare) : boolean;
{ TRUE,
}
var EsteInArbore : boolean;
procedure InAdincime(R : AdresaNod);
{ }
label 1;
var i : integer;
begin
if R<>nil then
begin
if StariEgale(St, R^.S) then
begin
EsteInArbore:=true;
goto 1;
end;
for i:=1 to 4 do InAdincime(R^.D[i]);
end;
1:end; { InAdincime }
begin
EsteInArbore:=false;
InAdincime(Radacina);

156

StareDejaExaminata:=EsteInArbore;
end; { StareDejaExaminata }
procedure Margineste(Adresa : AdresaNod);
{ }
label 1;
var i, k : integer;
R : AdresaNod;
begin
k:=0;
if (Adresa^.Nivel+1) > NivelMaxim then goto 1;
for i:=1 to m do
if not StareDejaExaminata(Str[i]) then
begin
k:=k+1;
new(R);
R^.S:=Str[i];
R^.Nivel:=Adresa^.Nivel+1;
CalculareaCostului(R);
for j:=1 to 4 do R^.D[j]:=nil;
Adresa^.D[i]:=R;
R^.Tata:=Adresa;
R^.Drum:=false;
if StariEgale(R^.S, StareaFinala) then
begin
R^.Drum:=true;
Gasit:=true;
end;
IncludeInLista(R);
end;
writeln(Foutput);
writeln(Foutput, , k,
);
writeln(Foutput);
1:end; { Margineste }
procedure AfisareaNodului(R : AdresaNod);
var i, j : integer;
begin
writeln(Foutput, =, R^.Drum, =, R^.Nivel,
=, R^.Cost);
for i:=1 to 4 do
begin
for j:=1 to 4 do write(Foutput, R^.S[i, j] : 3);
writeln(Foutput);
end;

157

for i:=1 to 4 do
if R^.D[i]<>nil then write(Foutput, *** )
else write(Foutput, nil );
writeln(Foutput); writeln(Foutput);
end; { AfisareaNodului }
procedure RamificaSiMargineste;
var NodulCurent : AdresaNod;
begin
Initializare;
repeat
ExtrageDinLista(NodulCurent);
writeln(Foutput,
);
writeln(Foutput,
======================);
AfisareaNodului(NodulCurent);
Ramifica(NodulCurent);
Margineste(NodulCurent);
until Gasit or (BazaListei=nil);
end; { RamificaSiMargineste }
procedure AfisareaDrumului;
label 1;
var R : AdresaCelula;
P, Q : AdresaNod;
begin
if not Gasit then
begin
writeln(Foutput, );
goto 1;
end;
writeln(Foutput,
:);
writeln(Foutput,
=============);
{ }
R:=BazaListei;
while (R<>nil) and (not R^.ReferintaNod^.Drum) do R:=R^.Urm;
{ , }
P:=R^.ReferintaNod;
while P<>nil do
begin
P^.Drum:=true;
P:=P^.Tata;
end;
{ }
P:=Radacina;
while P<>nil do
begin
AfisareaNodului(P);

158

Q:=nil;
for i:=1 to 4 do
if (P^.D[i]<>nil) and P^.D[i]^.Drum then Q:=P^.D[i];
P:=Q;
end;
writeln(Foutput, );
1:end; { AfisareaDrumului }
begin
{ }
assign(Finput, FINPUT.TXT);
reset(Finput);
for i:=1 to 4 do
for j:=1 to 4 do read(Finput, StareaInitiala[i, j]);
{ }
for i:=1 to 4 do
for j:=1 to 4 do read(Finput, StareaFinala[i, j]);
close(Finput);
{ }
assign(Foutput, FOUTPUT.TXT);
rewrite(Foutput);
RamificaSiMargineste;
AfisareaDrumului;
close(Foutput);
writeln(=, Gasit);
readln;
end.

P158
INPUT.TXT, , , FOUTPUT.TXT.
.


, . 5.13 . 5.2, .
P158.
P158 ,
. 5.12. , .
P158
Perspico:

159

a)

c)

11

13

b)

12

10

11

10

14

15

13

14

15

12

10

11

10

11

13

14

15

12

13

14

15

12

d)

,
FOUTPUT.TXT.
, :
a)
b)
1
2
3
4
1
3
4
4

13

10

11

10

11

14

15

12

13

14

15

12

P158 .
const NivelMaxim = 5;
(a) . , ?
Perspico, 9 (. 5.14).
P158 , , ?

a)

. 5.14. Perspico:
a ;

P158, :

160

F(si) = niv(si) + g(si),


g(si)
, . , . 5.12 :
g(si) = 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 1 + 3 = 6.
, 4 5. ,
, h(si), g(si) .
P158 ,
.
, .
, , ,
, (, ) (, ). , , {A, B, ..., H}, , {1, 2, ..., 8}.
*. n , . i , n1
, . ,
dij ,
. , , , , .
, . , .
: ,
(1, i, j, ..., k, 1), i, j, ..., k {2, 3, ..., n}. {2, 3, ..., n}.
, , , .

5.9.
, ,
: .
,
, . ,
.
, , . , . ,
, ,
.
*

, ,
.

161

, , , ,

.
,
. ,
Greedy, , .., ,
. , , , . .
. n ,
(. 5.15). dij ,
a b.

. 5.15.


( ) D = ||dij||nn n n , . dij
i, j , , 0
. , dii = 0, i = 1, 2, ..., n.
, . 5.15 :

D=

162

, a = 1, b = 6, 7 1, 3, 5, 6.
, ,

q 0 n2. , ,
, , O(n!) .
,
.
x1 = a ,
x2, x2 ..
, : , .
:
X = (a, x2, ..., xk-1, xk, ..., b)
xk xk-1. i
Ai , i = 1, 2, ..., n. , ,
, Ai dij, j Ai.
, . 5.15 :
A1 = (2, 3, 4);
A2 = (1);
A3 = (1, 4, 5);
A4 = (3, 5, 6);
A5 = (6, 3, 4);
A6 = (5).
: xk
(a, x2, ..., xk-1) , :
1) xk xk-1, .. xk Ak-1;
2) xk , .. xk a, xk
x2, ..., xk xk-1.
, . 5.15 () X :
X = (1);
X = (1, 2);
X = (1);
X = (1, 3);
X = (1, 3, 4);

163

X = (1, 3, 4, 5);
X = (1, 3, 4, 5, 6).
(1, 3, 4, 5, 6), ,
11 , , . (1, 4,
5, 6), 13.
A1, A2, ..., An
A[1], A[2], ..., A[n] A, .
Program P159;
{ }
const nmax=50;
var n : integer; { }
D : array[1..nmax, 1..nmax] of real; { }
a, b : 1..nmax;
X : array [1..nmax] of integer;
{ }
V : array[1..nmax, 1..nmax] of integer; { }
Finput : text;
procedure InitializareVecini;
{ V[k] k }
var k, i, j, p, q, r : integer;
begin
for k:=1 to n do
begin
{ V[k] }
for i:=1 to n do V[k,i]:=0;
{ V[k] }
i:=0;
for j:=1 to n do
if D[k,j]<>0 then
begin
i:=i+1;
V[k,i]:=j;
end; { then }
{ V[k] }
for j:=1 to i do
for p:=1 to i-1 do
if D[k, V[k,p]]>D[k, V[k, p+1]] then
begin
q:=V[k,p];
V[k,p]:=V[k, p+1];
V[k, p+1]:=q;
end; { then }
end; { for }
end; { InitializareVecini }

164

procedure Initializare;
var i, j : integer;
begin
assign(Finput, DRUM.IN);
reset(Finput);
readln(Finput, n);
readln(Finput, a, b);
writeln(n=, n,
a=, a, b=, b);
for i:=1 to n do
for j:=1 to n do read(Finput, D[i,j]);
close(Finput);
InitializareVecini;
end; { Initializare }
function MultimeVida(k : integer) : boolean;
begin
MultimeVida:=(V[X[k-1], 1]=0);
end; { MultimeVida }
function PrimulElement(k : integer) : integer;
var i : integer;
begin
PrimulElement:=V[X[k-1], 1];
for i:=1 to n-1 do V[X[k-1],i]:=V[X[k-1], i+1];
end; { PrimulElement }
function ExistaSuccesor(k : integer) : boolean;
begin
ExistaSuccesor:=(V[X[k-1], 1]<>0);
end; { ExistaSuccesor }
function Succesor(k : Integer) : integer;
var i : integer;
begin
Succesor:=V[X[k-1], 1];
for i:=1 to n-1 do V[X[k-1], i]:=V[X[k-1], i+1];
end; { Succesor }
function Continuare(k : integer) : boolean;
var i : integer;
Indicator : boolean;
begin
Continuare:=true;
for i:=1 to k-1 do
if X[i]=X[k] then Continuare:=false;
end; { Continuare }

165

procedure PrelucrareaSolutiei(k : integer);


var i : integer;
s : real;
begin
writeln( :);
for i:=1 to k-1 do write(X[i] : 3);
writeln;
s:=0;
for i:=1 to k-1 do s:=s+D[X[i], X[i+1]];
writeln( , s : 10:2);
readln;
halt;
end; { PrelucrareaSolutiei }
procedure Reluare(k : integer);
label 1;
var i : integer;
begin
if MultimeVida(k) then goto 1;
if X[k-1]<>b then
begin
X[k]:=PrimulElement(k);
if Continuare(k) then Reluare(k+1);
while ExistaSuccesor(k) do
begin
X[k]:=Succesor(k);
if Continuare(k) then Reluare(k+1);
end; { while }
end { then}
else PrelucrareaSolutiei(k);
1:end; { Reluare }
begin
Initializare;
X[1]:=a;
Reluare(2);
end.

P159 , , , O(mn), .. . m .
, ,
P159, . ,
, , , ,
.

166

.
.
,
, , .
,
C = ||cij||nn, cij
i j. cij :
1) , :
i = j;
i, j ;
;
2) i, j, k:
cij = min(cik, cik+cjk), i, j {1, 2, ..., n}, i j, i k, j k;
3) 2 k = 1, 2, ..., n.
. 5.15 . 5.16.
C , , , a, b Greedy. ,
X = (x1, ..., xk-1, xk, ...) x1 = a. k xk, xk Ak-1, :
C[a, xk] + C[xk, b] = C[a, b].
, a = 1, b = 6 . 5.15 :
X = (1);
X = (1, 3);
X = (1, 3, 5);
X = (1, 3, 5, 6).
, ,
-. ,
, .

RoyFloyd.
Program P160;
{
}
const nmax=50;
Infinit=1.0E+35;

167

k=1

k=2

k=3

k=4

k=5

k=6

. 5.16.

var n : integer; { }
D : array[1..nmax, 1..nmax] of real; { }
a, b : 1..nmAx;
X : array [0..nmax+1] of integer;
{ }
V : array[1..nmax, 1..nmax] of integer; { }
C : array[1..nmax, 1..nmax] of real; { }
Finput : text;
procedure InitializareVecini;
{ V[k] k }
var k, i, j, p, q, r : integer;

168

begin
for k:=1 to n do
begin
{ V[k] }
for i:=1 to n do V[k,i]:=0;
{ V[k] }
i:=0;
for j:=1 to n do
if D[k,j]<>0 then
begin
i:=i+1;
V[k,i]:=j;
end; { then }
end; { for }
end; { InitializareVecini }
procedure Initializare;
var i, j : integer;
begin
assign(Finput, DRUM.IN);
reset(Finput);
readln(Finput, n);
readln(Finput, a, b);
writeln(n=, n,
a=, a, b=, b);
for i:=1 to n do
for j:=1 to n do read(Finput, D[i,j]);
close(Finput);
InitializareVecini;
end; { Initializare }
procedure AfisareaDrumului;
var k : integer;
begin
write( t: );
k:=1;
repeat
write(X[k] : 3);
k:=k+1;
until X[k]=0;
writeln;
writeln( , C[a, b] : 5);
readln;
end; { PrelucrareaSolutiei }
function Min(p, q : real) : real;
{ p q }
var s : real;

169

begin
if p<q then s:=p else s:=q;
if s>Infinit then s:=Infinit;
Min:=s;
end; { Min }
procedure RoyFloyd;
var i, j, k : integer;
s : real;
ors : integer; { -
}
cnd : boolean; { }
begin
{ }
for i:=1 to n do
for j:=1 to n do
if (D[i,j]=0) and (i<>j) then C[i,j]:=Infinit
else C[i,j]:=D[i,j];
{ }
for k:=1 to n do
for i:=1 to n do
if i<>k then
for j:=1 to n do
if j<>k then C[i,j]:=Min(C[i,j], C[i,k]+C[j,k]);
{ Greedy }
for k:=1 to n do X[k]:=0;
k:=1; X[1]:=a;
while X[k]<>b do
begin
i:=1;
while V[X[k], i]<>0 do
begin
ors:=V[X[k], i];
cnd:=true;
for j:=1 to k do if ors=X[j] then cnd:=false;
if cnd and (C[A, ors]+C[ors, B]=C[a,b])
then X[k+1]:=ors;
i:=i+1;
end; { while }
k:=k+1
end; { while }
end; { RoyFloyd }
begin
Initializare;
RoyFloyd;
AfisareaDrumului;
end.

170

RoyFloyd , O(n3), .. .
. , . ,
. :
1) , , ;
2) , ,
, , .
, X = (a, x2, ..., xk-1, xk, ..., b), xk xk-1, ,
. , , :
xk xk-1. ,
, , .


?
, ?
, . .
,
.
, .
,
.
,
?
.

, ( 5.6).
, :
a) ( 5, 5.3);
) ( 6, 5.3);
) ( 9, 5.4);
) ( 7, 5.6);
) ( 9, 5.6);
) ( 10, 5.6);
) Perspico ( 5.8);
) ( 10, 5.8).

171

6


6.1.
.
A, n :
A = {a1, a2, , aj, , an}.

, set,
A
n: A=(a1, a2, , aj, , an). , A.
Ai A. , :
Bi = (b1, b2, , bj, , bn),

Ai A
Bi :
A1 =
A2 = {a1}
A3 = {a2}
A4 = {a1, a2}
...
Ak = {a1, a2, , an}

B1 = (0, 0, , 0);
B2 = (1, 0, , 0);
B3 = (0, 1, , 0);
B3 = (1, 1, , 0);
...
Bk = (1, 1, , 1);

, A k = 2n.

, : , , \ ,

172

.
.
, , , . .
1. A={a1, a2, , an}, n .
, Ai, Ai A, m.
. , {a1}, {a2}, {a1, a2} .. B1, B2, ..., Bk.
Program P161;
{ }
const nmax=50;
type Multime = array [1..nmax] of integer;
CifraBinara = 0..1;
VectorCaracteristic = array[1..nmax] of CifraBinara;
var A : Multime;
B : VectorCaracteristic;
n, m, j : integer;
function SolutiePosibila : boolean;
var j, suma : integer;
begin
suma:=0;
for j:=1 to n do
if B[j]=1 then suma:=suma+A[j];
if suma=m then SolutiePosibila:=true
else SolutiePosibila:=false;
end; { SolutiePosibila }
procedure PrelucrareaSolutiei;
var j : integer;
begin
write(: );
for j:=1 to n do
if B[j]=1 then write(A[j], );
writeln;
end; { PrelucrareaSolutiei }
procedure GenerareSubmultimi(var t:CifraBinara);
var j : integer;
begin
t:=1; { transportul }
for j:=1 to n do
if t=1 then

173

if B[j]=1 then B[j]:=0


else begin B[j]:=1; t:=0 end;
end; { GenerareSubmultimi }
procedure CautareSubmultimi;
var i : integer;
t : CifraBinara;
begin
for j:=1 to n do B[j]:=0;
{ B=(0, 0, ..., 0) }
repeat
if SolutiePosibila then PrelucrareaSolutiei;
GenerareSubmultimi(t);
until t=1;
end; { CautareSubmultimi }
begin
write( n=); readln(n);
writeln( , n, :);
for j:=1 to n do read(A[j]);
write( m=); readln(m);
CautareSubmultimi;
writeln();
readln;
end.

P161
CautareSubmultimi repeat...
until. GenerareSubmultimi.
B ,
.
if GenerareSubmultimi , bj t, t . t = 1 n ,
Bk = (1, 1, , 1)
B1 = (0, 0, , 0).
,
, O(2n).
S
.
2. n A1, A2, , An,
Aj mj . Aj
aj , a1 a2 ... an .
. S=A1A2...An.
, k = m1 m2 mn. -

174

si A1A2...An
:
Ci = (c1, c2, , cj, , cn),
cj Aj. ,



A1 = (-6, 2, 1); A2 = (4, 9); A3 = (-8, 3, 5),
:
s1 = (-6, 4, -8) C1 = (, , );
s2 = ( 2, 4, -8) C2 = (, , );
s3 = ( 1, 4, -8) C3 = (, , );
s4 = (-6, 9, -8) C4 = (, , );
s5 = ( 2, 9, -8) C5 = (, , );

s18= ( 1, 9, 5) C18 = (, , ),
, .
C1, C2, , Ck , C1=(1, 1, , 1).
Program P162;
{ }
const nmax=50; { }
mmax=50; { }
type Multime = array [1..mmax] of integer;
VectorIndicii = array[1..nmax] of 1..mmax;
var A : array[1..nmax] of Multime;
n : 1..nmax;
{ }
M : array[1..nmax] of 1..mmax; { A[i] }
Pmax : integer;
{ maximal }
C, Cmax : VectorIndicii;
i, j : integer;
procedure PrelucrareaSolutieiPosibile;
var j, p : integer;
begin
p:=1;
for j:=1 to n do p:=p*A[j, C[j]];
if p > Pmax then begin Pmax:=p; Cmax:=C end;
end; { PrelucrareaSolutieiPosibile }
procedure GenerareProdusCartezian(var t:integer);
var j : integer;
begin
t:=1; { }

175

for j:=1 to n do
begin
C[j]:=C[j]+t;
if C[j]<=M[j] then t:=0 else C[j]:=1;
end; { for }
end; { GenerareProdusCartezian }
procedure CautareaProdusuluiMaximal;
var j : integer;
t : integer;
begin
Pmax:=-MaxInt;
writeln(Pmax=, Pmax);
for j:=1 to n do C[j]:=1;
{ ncepem cu vectorul indiciilor C=(1, 1, ..., 1) }
repeat
PrelucrareaSolutieiPosibile;
write( : );
for j:=1 to n do write(A[j, C[j]], ); writeln;
GenerareProdusCartezian(t);
until t=1;
end; { CautareaProdusuluiMaximal }
begin
write( n=); readln(n);
for i:=1 to n do
begin
write( M[, i, ]=); readln(M[i]);
write( A[, i, ]: );
for j:=1 to M[i] do read(A[i, j]);
writeln;
end;
CautareaProdusuluiMaximal;
writeln(Pmax=, Pmax);
write( : );
for j:=1 to n do write(A[j, Cmax[j]], );
writeln;
readln;
readln;
end.

GenerareProdusCartezian C
, .
c1 m1, c2 m2, c3
m3 .. GenerareProdusCartezian

176

, C, . t =
= 1 n , Ck = (m1,
m2, , mn) C1 = (1, 1, , 1).
, n , ,
A1A2An , n :
for j1:=1 to m1 do
for j2:=1 to m2 do
...

for jn:=1 to mn do
if SolutiePosibila(aj1, aj2 ,..., ajn)
then PrelucrareaSoluiei(aj1, aj2, ..., ajn)

, , O(mn), m = max(m1, m2, , mn).

Ai, Aj A .
, : Ai Aj, Ai Aj, Ai \ Aj, Ai.

Ai, Ai A, n

(n- ), Ai
, , A.
, ,
: , , \ , . , :
, ?
CautareSubmultimi
P161. n.
A, n .
, .
n = 32*35*17*, 9 .
, ,
*, , m.
CautareaProdusuluiMaximal P162. n m1, m2, , mn.
m p .
f , k .
, .
A = (a1, a2, , aj, , an) ,
. p p
A. , p.

177

6.2.
, , , .
. , A={a1, a2, ..., an} n Pn = n!.
factorial,
Pn = 1 2 3 ... n
:

, A={a1, a2} P2 = 2! = 2, (a1, a2) (a2, a1). A={a1, a2, a3} P3 = 3! = 6, :


(a1, a2, a3);
(a2, a3, a1);

(a1, a3, a2);


(a3, a1, a2);

(a2, a1, a3);


(a3, a2, a1).

A={a1, a2, ..., an} I={1, 2, ..., n} ,


,
A n {1, 2, ...,
n}, n.
,
.
, (1, 2, ..., n).
p = (p1, ..., pi1, pi, pi+1, ..., pn),
p,
i, :
pi < pi+1; pi+1 > pi+2 > ... > pn.
, pi pi+1, ..., pn , pi, pk:
(p1, ..., pi-1, pk, pi+1, ..., pk-1, pi, pk+1, ..., pn).
p (n1) , .
i, , ,
, .. (n, (n1), ..., 1)
.
, n=3 :
(1, 2, 3);
(2, 3, 1);

178

(1, 3, 2);
(3, 1, 2);

(2, 1, 3);
(3, 2, 1).


GenerarePermutari.
Program P163;
{ }
const nmax=100;
type Permutare=array[1..nmax] of 1..nmax;
var P : Permutare;
n : 2..nmax;
Indicator : boolean;
i : integer;
procedure GenerarePermutari(var Indicator : boolean);
label 1;
var i, j, k, aux : integer;
begin
{ }
if not Indicator then
begin
for i:=1 to n do P[i]:=i;
Indicator:=true;
goto 1;
end;
{ i }
i:=n-1;
while P[i]>P[i+1] do
begin
i:=i-1;
if i=0 then
begin
{ }
Indicator:=false;
goto 1;
end; { then }
end; {while }
{ k }
k:=n;
while P[i]>P[k] do k:=k-1;
{ interschimbarea P[i] - P[k] }
aux:=P[i]; P[i]:=P[k]; P[k]:=aux;
{ (n-i) }
for j:=1 to (n-i) div 2 do

179

begin
aux:=P[i+j];
P[i+j]:=P[n-j+1];
P[n-j+1]:=aux;
end; { for }
Indicator:=true;
1:end; { GenerarePermutari }
begin
write( n=); readln(n);
Indicator:=false;
repeat
GenerarePermutari(Indicator);
if Indicator then
for i:=1 to n do write(P[i] : 3);
writeln;
until not Indicator;
readln;
end.


GenerarePermutari, Indicator false. P , Indicator
true. GenerarePermutari Indicator
false.
, , ,
O(n!).
, , n.
. m A={a1, a2, ..., an} n :

, :
Amn = n (n 1) (n 2) ... (n m + 2) (n m + 1).
, A I={1, 2, ...,
n}. , I={1, 2, 3} m=2 :
(1, 2);
(3, 1);

(2, 1);
(2, 3);

(1, 3);
(3, 2).

, a=(1, 2, ..., m).

180

a=(a1, a2, ..., ai, ..., am) . a a, i, ai


. ai ,
ai+1, ai+2, ..., n, ai. D=(d1, d2, ..., di, ..., dn) , di
0 1 ,
i a. i, ai,
ai+1, ..., am .
i , ,
(nm+1, nm+2, ..., n) . ,
Indicator.
Program P164;
{ }
const nmax=100;
mmax=100;
type Aranjament=array[1..mmax] of 1..nmax;
var A : Aranjament;
D : array[1..nmax] of 0..1;
n : 1..nmax;
m : 1..nmax;
i : integer;
Indicator : boolean;
procedure GenerareAranjamente(var Indicator : boolean);
label 1;
var i, j, k, l : integer;
begin
{ }
if not Indicator then
begin
for i:=1 to m do
begin
A[i]:=i; D[i]:=1;
end;
for i:=m+1 to n do D[i]:=0;
Indicator:=true;
goto 1;
end;
{ }
for i:=m downto 1 do
begin
D[A[i]]:=0;

181

for j:=A[i]+1 to n do
if D[j]=0 then
begin
A[i]:=j; D[j]:=1; k:=0;
for l:=i+1 to m do
begin
repeat k:=k+1 until D[k]=0;
A[l]:=k; D[k]:=1;
end; { for }
goto 1;
end; { if }
end; { for }
Indicator:=false;
1:end; { GenerareAranjamente }
begin
write( n=); readln(n);
write( m=); readln(m);
Indicator:=false;
repeat
GenerareAranjamente(Indicator);
if Indicator then
for i:=1 to m do write(A[i] : 3);
writeln;
until not Indicator;
readln;
end.

, , , O(n!).
. n m
(mn) :

, I={1, 2, 3} m=2 :
{1, 2};

{1, 3};

{2, 3}.


, {1, 2, ..., m}.
c={c1, c2, ..., ci, ..., cm}.
:
i, ci<nm+1, ci+1=nm+i+1,
cm-1=n1, cm=n;
c={c1, ..., ci-1, ci+1, ci+2, ..., ci+ni+1}.
i, , , , .

182

I={1, 2, ..., n} ( ) C.
Program P165;
{ }
const nmax=100;
mmax=100;
type Combinare=array[1..mmax] of 1..nmax;
var C : Combinare;
n : 1..nmax;
m : 1..mmax;
i : integer;
Indicator : boolean;
procedure GenerareCombinari(var Indicator : boolean);
label 1;
var i, j : integer;
begin
{ }
if not Indicator then
begin
for i:=1 to m do C[i]:=i;
Indicator:=true;
goto 1;
end;
{ }
for i:=m downto 1 do
if C[i]<(n-m+i) then
begin
C[i]:=C[i]+1;
for j:=i+1 to m do C[j]:=C[j-1]+1;
goto 1;
end; { then }
Indicator:=false;
1:end; { GenerareCombinari }
begin
write( n=); readln(n);
write( m=); readln(m);
Indicator:=false;
repeat
GenerareCombinari(Indicator);
if Indicator then
for i:=1 to m do write(C[i] :3);
writeln;

183

until not Indicator;


readln;
end.

, , , O(nk), k=min(m, nm+1), .. .


, Pn, . n m .
, I={1, 2, ..., n}.
, , , n
.
T[1..n, 1..n] . , , .
, , A, b, C, d, E.
.
n m ,
. , m .
A={a1, a2, ..., an}. , , m A ,
.
, ,
, ?
,
, ?
A={a1, a2, ..., an}. ,
A B C, B C. ,
A={4, 1, 0, 1, 2, 3, 9} B={4, 0, 9} C={1, 1, 2, 3}.

184

7

, , . , .
1. . a n (n 100).
i (xi, yi) ri. , t ,
n .
2. . n , n 100. .
3. . n , m . ,
i, j ,
gij . Gab,
a b.
4. . n ,
. i ri .
m . , .
, ..
. ,
, .
5. . n .
i xi, yi. , , .
6. . n , n 100,
. i
xi, yi. , ,
.
. , i, j, .
, ,
.
7. . ,
, (,) +, -, *, mod, div.
, .

185

8. . n , n 100, m . k , k m = n.
i, j rij , : 0 ( ), 1, 2, ... , 10 ( ).
C rij (i, j)
. Cmax,
.
9. . n . i xi, yi. , ,
,
.
10. . n (n 10), ,
{0, 1, 2, ..., 20}. ,
,
.
11. . n (n 10), ,
. , , n .
12. . , 4 , (, , )
,
.
13. . P1P2
... PnP1, n 20. A . ,
. , , .
14. . . .
, ,
. , , .
15. . mn, m n . mn
11 . , dxy, x, y
.
.
:
;
;

186

,
( );
.
c = dmax,
dmax 11,
. , T
, 11. , T.
16. . (. 7.1).
, (xi, yi). ,
1, 2, 3, ..., n, .
.

. 7.1.

, :
n (xi, yi) ;
m, .
, d
, .
17. . n , 1 n.
i hi xi, yi. , ,
.
(. 7.2):

187

,
;

;
(, );
.

. 7.2. ,

. TURNURI.IN n. n xi,
yi, hi, .
. TURNURI.OUT
.
.
TURNURI.IN
5
3 4 2
3 4 3
4 3 2
1 5 4
2 2 1

TURNURI.OUT
8

: n 1000; xi, yi, hi < 1000.


10 .
18. . , .
. n, n 100, (. 7.3). , 10 ( ), .
INTRARE, IESIRE. , .

188

. 7.3.

. ,
, ,
. ,
.
, . , INTRARE,
STALAGMITE, LILIECI, IZVOARE, IESIRE , 5.
, INTRARE IESIRE.
. .
UndeMaAflu string. ,
, , , . ,
LILIECI, :
LILIECI: STALAGMITE IZVOARE LILIECI LILIECI

, c, TreciCoridorul(c),
c string. ,
. ,
:
uses LABIRINT;

. SPEOLOG.OUT
.
. .

189

, FUNDAC.
. . 7.3 :
SPEOLOG.OUT
4
INTRARE
STALACTITE
IZVOARE
IESIRE

20 .
19. . nm , 1.
.
, . , ,
k .
.
20. . 1 n.
, i, ,
. ,
j, , .
, p, . m {j, k} j, k . , 3 n 1000 2 m 30000.
21. . n . .
M = ||mi||, mi = 1, i , mi = 0 . ,
M C = ||ci||n,
:

M . , M, C.
, 3 n 10000.
22. . n .
xi, yi, zi.
, , ,
, . i
j ,

190

, i
j. , , (.7.4). ,
, ,
i1, i2, i3, ..., ik , i1 i2; i2 i3 .. ,
kmax, .

. 7.4.

. CUTII.IN n. n xi, yi, zi, .


. CUTII.OUT kmax.
.

CUTII.IN
5
4 4 4
1 3 5
2 2 3
1 1 1
1 1 2

CUTII.OUT
3

. 2 n 500; 1 xi, yi, zi 30000.


2 .

191

Acest manual este proprietatea Ministerului Educaiei al Republicii Moldova.


Liceul/gimnaziul __________________________________________________________
Manualul nr. _____________________________________________________________
Nr. Numele de familie i prenumele
crt. elevului

Anul colar

Aspectul manualului
la primire

la restituire

1.
2.
3.
4.
5.
Dirigintele verific dac numele elevului este scris corect.
Elevul nu trebuie s fac niciun fel de nsemnri n manual.
Aspectul manualului (la primire i la restituire) se va aprecia folosind termenii: nou, bun,
satisfctor, nesatisfctor.

Imprimare la Tipografia BALACRON SRL,


str. Calea Ieilor, 10; MD-2069
Chiinu, Republica Moldova
Comanda nr. 561

Das könnte Ihnen auch gefallen