Sunteți pe pagina 1din 51

Probleme rezolvate de programare Subiecte propuse la ATESTAT - 2006

2006
ATESTAT - 2006 - 1 - Se citete de la tastatur un numr natural n (1 < n < 100) i un ir X de n
valori ntregi. S se nlocuiasc fiecare element din ir cu media aritmetic a celorlalte n 1
elemente.

Rezolvare:
Calculm suma S a tuturor elementelor vectorului X.
Pentru fiecare poziie i din vector, calculm media aritmetic a elementelor S x[i].
Program ATESTAT_2006_1_MEDIE_ARITMETICA ;
const
nmax = 100;
type
vector = array [1..nmax] of integer;
vectorreal = array [1..nmax] of real;
var
n, i, k : integer;
s: integer;
x : vector;
y : vectorreal;
media : real;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln ('Dati cele n elemente intregi');
writeln;
for i := 1 to n do
begin
write ('Dati X [', i, '] = ');
readln (x[i])
end;
s:= 0;
for i := 1 to n do
begin
s:= s + x[i]
end;
for i := 1 to n do
begin
media := (s - x[i] ) / (n-1);
y[i] := media ;
writeln ('y[i] = ', y[i] )
end;
readln
end.

ATESTAT - 2006 - 2 Se citete de la tastatur un numr natural n (1 < n < 100) i un ir X de n


valori ntregi. S se construiasc un vector Y care s conin numai elementele pare din vectorul X.

Rezolvare: Notm cu pare o variabil cu dublu rol: de contor al numerelor pare gsite n vectorul
X, precum i de index al elementelor vectorului Y.
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Program ATESTAT_2006_2_ELEMENTE_PARE ;
const
nmax = 100;
type
vector = array [1..nmax] of integer;
var
x, y : vector;
n, i, k, pare : integer;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati vectorul X');
writeln;
for i:= 1 to n do
begin
write ('Dati X[', i, '] = ');
readln (x[i])
end;
pare := 0; {initializare contor si index}
for i := 1 to n do
begin
if x [i] MOD 2 = 0 then
begin
pare := pare + 1;
y [pare] := x[i];
writeln ('y [', pare, '] = ', y [pare]);
end;
end;
readln
end.

ATESTAT - 2006 - 3 Se citete de la tastatur un numr natural n (1 < n < 100) i un ir X de n


valori ntregi. Verificai dac o valoare a, citit de la tastatur se gsete n vector i dac da,
afiai poziiile pe care apare i numrul de apariii.

Rezolvare: Aplicm un algoritm de cutare secvenial a unui element ntr-un vector.


Program ATESTAT_2006_3_CAUTARE ;
const
nmax = 100;
type
vector = array [1..nmax] of integer;
var
x : vector;
n, i, pozitia : integer;
elem : integer;
gasit : BOOLEAN;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati vectorul X');
writeln;

4
Probleme rezolvate de programare Subiecte propuse la ATESTAT
for i:= 1 to n do
begin
write ('Dati X[', i, '] = ');
readln (x[i])
end;
gasit := false;
writeln ;
writeln ('Dati elementul cautat, elem = ');
readln (elem);
writeln;
for i:= 1 to n do
begin
if x [i] = elem then
begin
gasit := true;
pozitia := i;
writeln (elem, ' apare in pozitia ', pozitia);
end
end;
if gasit = true then
writeln (elem, ' apartine vectorului')
else
writeln (elem, ' nu apartine vectorului');
readln
end.

ATESTAT - 2006 - 4 Se citete de la tastatur un numr natural n (1 < n < 100) i un ir X de n


valori ntregi. S se numere elementele din vectorul X care se gsesc n intervalul [a, b], cu a i b
numere ntregi citite de la tastatur, i s se construiasc un alt vector cu cele care nu aparin
intervalului.

Rezolvare: Vom folosi 2 contori:


k = contor care numr elementele din intervalul [a, b] ;
j = contor care numr elementele ce nu aparin [a, b] .
Program ATESTAT_2006_4_NUMARARE_ELEMENTE_DIN_INTERVALUL_A_B ;
const
nmax = 100;
type
vector = array [1..nmax] of integer;
var
x, y : vector;
a, b, n, i, k, j : integer;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati vectorul X');
writeln;
for i:= 1 to n do
begin
write ('Dati X[', i, '] = ');
readln (x[i])
end;
writeln;
write ('Dati a = '); readln (a);
write ('Dati b = '); readln (b);

5
Probleme rezolvate de programare Subiecte propuse la ATESTAT
writeln;
k := 0; {contor care numra elementele din intervalul [a, b] }
j := 0; {contor care numra elementele ce nu apartin [a, b] }
for i := 1 to n do
begin
if (x [i] >= a) and (x[i] <= b) then
begin
k:= k + 1;
end
else
begin
if (x[i] <a) AND (x[i] > b) then
begin
j := j + 1;
y [j] := x [i];
writeln (' y[', j,'] = ', y [j]);
end;
end;
end;
writeln;
writeln ('In intervalul [', a,', ', b,'] exista ', k, ' elemente');
readln
end.

ATESTAT - 2006 - 5 Se citete de la tastatur un numr natural n (1 < n < 100) i un ir X de n


valori ntregi. S se ordoneze cresctor pn la o poziie k citit de la tastatur i descresctor de
la poziia k la n.

Rezolvare: Definim dou proceduri: ORDONEAZACRESCATOR, pentru ordonarea pn la


poziia k i ORDONEAZADESCRESCATOR., pentru ordonarea de la poziia k la n.
Ambele proceduri se bazeaz pe algoritmul bubble sort.
Program ATESTAT_2006_5_ORDONARI ;
const
nmax = 100;
type
vector = array [1..nmax] of integer;
var
x, y, z : vector;
a, b, n, i, k, aux : integer;
procedure ORDONEAZACRESCATOR (VAR v : vector; m : integer);
var
j, aux, test : integer;
begin
repeat
test := 0;
j := 1;
repeat
if v [j] > v [j + 1] then
begin
aux := v[j];
v [j] := v [j + 1];
v [j + 1] := aux;
test := 1;
end;
j := j + 1
until j > m - 1
until test = 0
end;

6
Probleme rezolvate de programare Subiecte propuse la ATESTAT
procedure ORDONEAZADESCRESCATOR (VAR v : vector; m : integer);
var
j, aux, test : integer;
begin
repeat
test := 0;
j := 1;
repeat
if v [j] < v [j + 1] then
begin
aux := v[j];
v [j] := v [j + 1];
v [j + 1] := aux;
test := 1;
end;
j := j + 1
until j > m - 1
until test = 0
end;

Begin { PROGRAM PRINCIPAL }


repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati vectorul X');
writeln;
for i:= 1 to n do
begin
write ('Dati X[', i, '] = ');
readln (x[i])
end;
writeln;
write ('Dati pozitia k = ');
readln (k);
for i:= 1 to k do
begin
y [i] := x [i];
end;
for i:= k + 1 to n do
begin
z [i-k] := x [i]
end;

ORDONEAZACRESCATOR (y, k);


ORDONEAZADESCRESCATOR (z, n-k);

for i:= 1 to k do
begin
writeln ('y [',i,'] = ', y[i]);
end;

writeln;
for i:= k + 1 to n do
begin
writeln ('z [',i-k,'] = ', z[i-k]);
end;
readln
end.

7
Probleme rezolvate de programare Subiecte propuse la ATESTAT
ATESTAT - 2006 - 6 Se citete de la tastatur un numr natural n (1 < n < 100) i un ir X de n
valori ntregi. S se afieze subirul de valori cresctoare (aezate pe poziii consecutive) de
lungime maxim.

Rezolvare:
Reinem cele n valori ntregi n vectorul X.
Construim un alt vector subsir cu elemente din X n ordine cresctoare.
Construim nc un vector LUNGIME n care scriem, pe fiecare poziie, lungimile subirurilor
cresctoare gsite.
Program ATESTAT_2006_6_SUBSIR_DE_VALORI_CRESCATOARE_DE_LUNGIME_MAXIMA ;
const
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
var
max, poz, t, n, i, lung, j : integer;
subsir, lungime, x, y : vector;
k : BOOLEAN;

Begin { PROGRAM PRINCIPAL }


write ('Dati n = '); readln (n);
writeln;
writeln ('Dati vectorul X');
for i:= 1 to n do
begin
write ('X[',i,'] = '); readln (x [i] )
end;
writeln;
lung := 0; {contor pentru numrul de subsiruri crescatoare}
t:= 1; {t = pozitia in subsirul construit cu elemente in ordine
crescatoare}
subsir [t] := x [1]; {initializarea unui prim subsir, presupunand ca vom
gasi un astfel de subsir chiar din pozitia 1}
max := 1; {initializare lungime maxima presupusa}
for i:= 1 to n-1 do
begin
if x [i] < x [i + 1] then
begin
t := t + 1;
subsir [t] := x [i + 1]; {depun in subsir urmatorul element}
end
else
begin
lung := lung + 1;
lungime [lung] := t; {t = nr. de elemente din subsir}
{ writeln ('lungime subsir = ', lungime [lung]); }
if max < lungime [lung] then
begin
max := lungime [lung];
writeln;
poz := i - max + 1; {pt. ca i = 1..n-1}
end;
for j := 1 to t do
write (subsir [j],' '); {tiparesc subsir curent}
writeln;
t := 1; {reinitializ. pozitie nou subsir}
subsir [t] := x [i + 1];
end; {sf. IF}
end; {sf. for}
writeln;

8
Probleme rezolvate de programare Subiecte propuse la ATESTAT
writeln ('Ultimul subsir este:');
lung := lung + 1;
lungime [lung] := t; {t = nr. de elemente din subsir}

for j := 1 to t do
write (subsir [j],' ');
writeln;
if max < lungime [lung] then
begin
max := lungime [lung];
writeln;
writeln;
poz := i + 1 - max + 1; {pt. ca i = 1..n-1}

if max < lungime [lung] then


begin
max := lungime [lung];
writeln;
poz := i - max;
writeln;
end;
writeln;
end;
writeln;
writeln ('Subsirul este:');
writeln;
for i:= 1 to n do
begin
write (X[i],' ');
end;
writeln;
writeln;
writeln ('S-au gasit subsiruri de lungimile');
writeln;

for j:= 1 to lung do


begin
writeln ('Subsirul ', j,' are lungimea ', lungime [j]);
end;
writeln;
writeln ('Subsirul de lungime maxima are ', max, ' elemente:');
writeln ('si incepe in pozitia ', poz);
writeln;
for j := poz to (poz + max-1) do
begin
write (x[j],' ');
end;
readln
end.

ATESTAT - 2006 - 7 Se citete de la tastatur un numr natural n (1 < n < 100) i un ir X de n


valori ntregi. S se afieze toate perechile de elemente ale irului (nu neaprat consecutive ca
poziie) cu proprietatea c ambele elemente ale perechii au aceeai sum a cifrelor.

Rezolvare: Definim dou funcii:


NRCIFRE pentru a numra cifrele unui element ;
SUMACIFRE pentru a face suma cifrelor unui element.
Scriem cifrele unui element ntr-un vector auxiliar y.

9
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Program ATESTAT_2006_7_SUMA_CIFRE ;
const
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
var
S1, x1, n, i, j : integer;
x, suma, y : vector;
k : BOOLEAN;
function NRCIFRE (numar : integer) : integer;
var
m : integer;
begin
if numar = 0 then
m := 1
else
begin
m := 0;
while numar <> 0 do
begin
numar := numar DIV 10;
m := m + 1
end
end;
NRCIFRE := m
end;
function SUMACIFRE (numar : integer) : integer;
var
t, s : integer;
y : vector;
begin
s := 0;
for t := 1 to NRCIFRE (numar) do
begin
y [ t ] := numar MOD 10;
writeln ('y [', t, '] = ', y [t]);

numar := numar DIV 10;


end;
for t := 1 to NRCIFRE (numar) do
begin
s := s + y [t]
end;
SUMACIFRE := s;
end;
Begin { PROGRAM PRINCIPAL }
write ('Dati n = ');
readln (n);
writeln;
writeln ('Dati vectorul X');
for i:= 1 to n do
begin
write ('X[',i,'] = ');
readln (x [i] )
end;
writeln;
for i := 1 to n do
begin
suma [i] := SUMACIFRE (x [i] )
end;
writeln;
writeln ('Perechile de numere cu sumele cifrelor egale sunt:');
writeln;

10
Probleme rezolvate de programare Subiecte propuse la ATESTAT
for i:= 1 to n - 1 do
begin
for j := i + 1 to n do
begin
if suma [i] = suma [j] then
begin
writeln ('x[',i,'] = ', x[i],' x[',j,'] = ', x[j] );
end
end
end;
readln
end.

ATESTAT - 2006 - 8 Se citete un numr ntreg strict pozitiv cu cel mult nou cifre. S se
afieze cifra cea mai mare i de cte ori apare n numr.

Rezolvare: Definim funcia NRCIFRE, pentru a numra cifrele numrului. Reinem ntr-un vector
y cifrele numrului.
Program ATESTAT_2006_8_CIFRA_MAXIMA;
const
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
var
cifmax, cif, max, c, x, x1, n, i : integer;
y : vector;
k : BOOLEAN;
cifra : array [0..9] of integer;
{vectorul "cifra" contorizeaza aparitiile cifrelor in numarul x}

function NRCIFRE ( numar : integer) : integer;


var
m : integer;
begin
if numar = 0 then
m := 1
else
begin
m := 0;
while numar <> 0 do
begin
numar := numar DIV 10;
m := m + 1
end
end;
NRCIFRE := m
end;

Begin { PROGRAM PRINCIPAL }


write ('Introduceti un numar natural x = ');
readln (x);
n := NRCIFRE (x);
writeln ('Numarul ',x,' are ',n,' cifre');

writeln;
x1 := x; {conservam valoarea lui x, folosind un auxiliar x1}
writeln ('Cifrele numarului ', x,' sunt:');

11
Probleme rezolvate de programare Subiecte propuse la ATESTAT
for i := n downto 1 do
begin
y [ i ] := x1 MOD 10;
writeln ('y [',i,'] = ', y [i]);
x1 := x1 DIV 10;
end;

for c:= 0 to 9 do
cifra [c] := 0; {initializam cu "zero" toti contorii cifrelor}
for c:= 0 to 9 do
begin
for i:= 1 to NRCIFRE (x) do
begin
if c = y [i] then
cifra [c] := cifra [c] + 1 {majoram contor cifra}
end
end;
writeln;
writeln ('Frecventa de aparitie a cifrelor este:');

for c:= 0 to 9 do
begin
writeln ('Cifra ', c,' apare de ', cifra [c], ' ori');
end;

cifmax := 0; {initializare variabila "cifmax" }

for i := 1 to NRCIFRE (x) do


begin
if cifmax < y [i] then
cifmax := y [i]
end;
writeln;
writeln ('Cifra maxima ', cifmax,' apare de ', cifra [cifmax], ' ori');
readln
end.

ATESTAT - 2006 - 9 Se citete un numr ntreg strict pozitiv cu cel mult nou cifre. S se
afieze cifrele distincte precum i de cte ori apar n numr.

Rezolvare: Folosim un vector CIFRA n care memorm, pe fiecare poziie, frecvenele de


apariie ale cifrelor de la 0 la 9. Reinem ntr-un vector y cifrele numrului x.
Program ATESTAT_2006_9_NUMARARE_CIFRE_DISTINCTE ;
const
nmax = 100;
TYPE
vector = array [1..nmax] of integer;

var
c, x, x1, n, i : integer;
y : vector;
k : BOOLEAN;
cifra : array [0..9] of integer;
{vectorul "cifra" contorizeaza aparitiile cifrelor in numarul x}

12
Probleme rezolvate de programare Subiecte propuse la ATESTAT
function NRCIFRE ( numar : integer) : integer;
var
m : integer;
begin
if numar = 0 then
m := 1
else
begin
m := 0;
while numar <> 0 do
begin
numar := numar DIV 10;
m := m + 1
end
end;
NRCIFRE := m
end;

Begin { PROGRAM PRINCIPAL }


write ('Introduceti un numar natural x = ');
readln (x);
n := NRCIFRE (x);
writeln ('Numarul ',x,' are ',n,' cifre');
writeln;
x1 := x; {conservam valoarea lui x, folosind un auxiliar x1}
writeln ('Cifrele numarului ', x,' sunt:');
for i := n downto 1 do
begin
y [ i ] := x1 MOD 10;
writeln ('y [',i,'] = ', y [i]);
x1 := x1 DIV 10;
end;

for c:= 0 to 9 do
cifra [c] := 0; {initializam cu "zero" toti contorii cifrelor}

for c:= 0 to 9 do
begin
for i:= 1 to NRCIFRE (x) do
begin
if c = y [i] then
cifra [c] := cifra [c] + 1 {majoram contor cifra}
end
end;
writeln;
writeln ('Frecventa de aparitie a cifrelor este:');
for c:= 0 to 9 do
begin
writeln ('Cifra ', c,' apare de ', cifra [c], ' ori');
end;
readln
end.

ATESTAT - 2006 - 10 Se citete un numr ntreg strict pozitiv cu cel mult nou cifre. S se
verifice dac cifrele lui sunt n ordine strict cresctoare.

Rezolvare: Definim funcia NRCIFRE, pentru a numra cifrele numrului. Reinem ntr-un vector
y cifrele numrului.

13
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Program ATESTAT_2006_10_CIFRE_IN_ORDINE_CRESCATOARE ;
const
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
var
x, x1, n, i : integer;
y : vector;
k : BOOLEAN;

function NRCIFRE ( numar : integer) : integer;


var
m : integer;
begin
if numar = 0 then
m := 1
else
begin
m := 0;
while numar <> 0 do
begin
numar := numar DIV 10;
m := m + 1
end
end;
NRCIFRE := m
end;
Begin { PROGRAM PRINCIPAL }
write ('Introduceti un numar natural x = ');
readln (x);
n := NRCIFRE (x);
writeln ('Numarul ',x,' are ',n,' cifre');
writeln;
x1 := x; {conservam valoarea lui x, folosind un auxiliar x1}
for i := n downto 1 do
begin
y [ i ] := x1 MOD 10;
writeln ('y [',i,'] = ', y [i]);
x1 := x1 DIV 10;
end;

K := TRUE;
for i := 3 to n do
begin
if NOT ((y[i] > y [i - 1] ) AND (y[i] > y [i - 1])) then
k := FALSE
end;
if K then
writeln (x ,' are cifrele in ordine crescatoare')
else
writeln (x ,' are NU cifrele in ordine crescatoare');
readln
end.

ATESTAT - 2006 - 11 Se citesc trei numere ntregi strict pozitive. S se afieze n ordine
cresctoare. S se verifice dac ele formeaz sau nu o progresie geometric.

Rezolvare: n general, un ir de numere se afl ntr-o progresie geometric dac i Xn + 1 / Xn =


raia i Xn / Xn1 = raia ... i X2 / X1 = raia.

14
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Program ATESTAT_2006_11_PROGRESIE_GEOMETRICA ;
const
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
var
ratia, n, i, j, k: integer ;
x : vector;
OK : boolean;
procedure ORDONEAZA (VAR v : vector; m : integer);
var
j, aux, test : integer;
begin
repeat
test := 0;
j := 1;
repeat
if v [j] > v [j + 1] then
begin
aux := v[j];
v [j] := v [j + 1];
v [j + 1] := aux;
test := 1;
end;
j := j + 1
until j > m - 1
until test = 0
end;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = '); readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati vectorul X');
writeln;
for i := 1 to n do
begin
write ('Dati X [', i, '] = '); readln (X [i])
end;
writeln;
ORDONEAZA (X, n);
writeln;
writeln ('Vectorul X dupa ordonare este');
for i := 1 to n do
begin
writeln ('X [',i,'] = ', X[i] );
end;
writeln;

ratia := x [2] DIV x [1];

OK := TRUE ; {presupun ca formeaza progresie aritmetica}


for i:= 2 to n do
begin
if x [i] / x [i-1] <> ratia then
OK := FALSE
end;
if OK = TRUE then
writeln ('Elementele formeaza o progresie geometrica')
else
writeln ('Elementele NU formeaza o progresie geometrica');
readln
end.

15
Probleme rezolvate de programare Subiecte propuse la ATESTAT
ATESTAT - 2006 - 12 Se citesc dou numere ntregi strict pozitive. S se calculeze i s se
afieze c.m.m.d.c. al celor dou numere.

Rezolvare: Aplicm algoritmul lui Euclid.


Program ATESTAT_2006_12_CMMDC ;
VAR
A, B, aux, R, cmmdc : integer;
Begin { PROGRAM PRINCIPAL }
write ('Dati A = ');
readln (A);
write ('Dati B = ');
readln (B);
if A< b then
begin
aux := a;
a:= B;
b := aux;
end;
R := A MOD B;
While R <> 0 do
begin
A := B;
B := R;
R := A MOD B
end;
cmmdc := b;
writeln ('CMMDC = ', CMMDC);
readln
end.

ATESTAT - 2006 - 13 Se citesc dou numere ntregi strict pozitive cu cel mult nou cifre. S se
afieze c.m.m.d.c. al lor, apoi s se verifice rezultatul.

Rezolvare: Aplicm algoritmul lui Euclid.


Program ATESTAT_2006_13_CMMDC;
VAR
A, B, aux, R, cmmdc : LONGINT;
Begin { PROGRAM PRINCIPAL }
write ('Dati A = ');
readln (A);
write ('Dati B = ');
readln (B);
if A< b then
begin
aux := a;
a:= B;
b := aux;
end;
R := A MOD B;
While R <> 0 do
begin
A := B;
B := R;
R := A MOD B
end;
cmmdc := b;

16
Probleme rezolvate de programare Subiecte propuse la ATESTAT
writeln ('CMMDC = ', CMMDC);
writeln;
if (A MOD cmmdc = 0) AND (B MOD cmmdc = 0) then
writeln ('Rezultat CORECT')
else
writeln ('Rezultat incorect');
readln
end.

ATESTAT - 2006 - 14 Dou numere ntregi strict pozitive sunt prietene dac suma divizorilor
proprii ai unuia este mai mic cu o unitate dect cellalt numr i invers. Date dou numere, s se
verifice dac sunt prietene.

Rezolvare: Reinem n SA, respectiv n SB, sumele divizorilor celor dou numere.
Program ATESTAT_2006_14_NUMERE_PRIETENE ;
const
nmax = 30;
type
vector = array [1..nmax] of integer;
var
divA, divB : vector;
SA, SB, A, B, divizor, nrdiv: integer;
Begin { PROGRAM PRINCIPAL }
write ('Dati A = '); readln (A);
write ('Dati B = '); readln (B);
SA := 0;
SB := 0;
for divizor := 1 to A do
begin
if A MOD divizor = 0 then
SA := SA + divizor;
end;
for divizor := 1 to B do
begin
if B MOD divizor = 0 then
SB := SB + divizor;
end;
if (SA = B-1) AND (SB = A - 1) then
writeln (A, ' si ', B, ' sunt prietene')
else
writeln (A, ' si ', B, ' NU sunt prietene');
readln
end.

ATESTAT - 2006 - 15 Fie A (m, n) o matrice cu m linii i n coloane. S se genereze i s se


afieze matricea B (n, m) cu n linii i m coloane, ale crei elemente sunt definite astfel:
B[i, j] = A[j, i], cu i = 1 . . n, j = 1 . . m.

Rezolvare: Se construiete matricea B, n condiiile din enun.


Program ATESTAT_2006_15_MATRICI ;
const
nmax = 30;
type
matrice = array [1..nmax, 1..nmax] of integer;

17
Probleme rezolvate de programare Subiecte propuse la ATESTAT
var
A, B : matrice;
m,n,i,j : integer;

Begin { PROGRAM PRINCIPAL }


repeat
write ('Dati m = ');
readln (m);
until (m >= 1) and (m <= nmax);
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati matricea A');
for i := 1 to m do
begin
for j := 1 to n do
begin
write ('Dati A [', i,',',j,'] = ');
readln (A [i,j])
end
end;
for i:= 1 to n do
begin
for j := 1 to m do
begin
B [i, j] := A [j, i]
end
end;
writeln;

writeln ('Matricea A este:');


for i:= 1 to m do
begin
for j := 1 to n do
begin
write (A [i, j], ' ')
end;
writeln
end;
writeln;
writeln ('Matricea B este:');
for i:= 1 to n do
begin
for j := 1 to m do
write (B [i, j], ' ');
writeln
end;
readln
end.

ATESTAT - 2006 - 16 Fie A (m, n) o matrice cu m linii i n coloane, cu elemente numere reale i
dou linii distincte L1 i L2 cu indicii din intervalul [1 . . m]. S se schimbe elementele liniei L1 cu
elementele liniei L2 i s se afieze matricea rezultat.

Rezolvare: Folosim o matrice auxiliar MATAUX i aplicm algoritmul folosit schimbarea ntre
ele a dou valori reale.

18
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Program ATESTAT_2006_16_INTERSCHIMBARE_LINII_MATRICE ;
const
nmax = 30;
type
vector = array [1..nmax] of integer;
matrice = array [1..nmax, 1..nmax] of integer;
var
A, B, MATAUX : matrice;
m,n,i,j, L1, L2 : integer;
v1, v2, aux : vector;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati m = '); readln (m);
until (m >= 1) and (m <= nmax);
repeat
write ('Dati n = '); readln (n);
until (n >= 1) and (n <= nmax);

writeln;
writeln ('Dati matricea A');
writeln;
for i := 1 to m do
begin
for j := 1 to n do
begin
write ('Dati A[', i,',',j,'] = ');
readln (A[i,j])
end
end;
writeln;
writeln;
for i := 1 to m do
begin
for j := 1 to n do
MATAUX [i, j] := A [i, j] ;
end;
writeln;
writeln ('Dati linia L1 =');
readln (L1);
writeln ('Dati linia L2 = ');
readln (L2);
for j := 1 to n do
begin
aux [j] := MATAUX [L1, j];
MATAUX [L1, j] := MATAUX [L2, j];
MATAUX [L2, j] := aux [j]
end;
writeln;
for i := 1 to m do
begin
for j := 1 to n do
B [i, j] := MATAUX [i, j];
end;
writeln;
writeln ('Matricea A este:');
for i := 1 to m do
begin
for j := 1 to n do
begin
write (A[i,j],' ');
end;
WRITELN
end;

19
Probleme rezolvate de programare Subiecte propuse la ATESTAT
writeln;
writeln;
writeln ('Matricea B este:');
for i := 1 to m do
begin
for j := 1 to n do
begin
write (B[i,j],' ');
end;
WRITELN
end;
readln
end.

ATESTAT - 2006 - 17 Se d o matrice ptratic A de ordinul n. S se ordoneze cresctor


elementele de pe diagonala principal.
Indicaie: dou elemente consecutive de pe diagonala principal sunt:
A [i, i] i A [i + 1, i + 1].

Rezolvare: Definim procedura ORDONEAZA, bazat pe algoritmul bubble sort. Folosim un


vector auxiliar n care prelum elementele de pe diagonala principal.
Program ATESTAT_2006_17_ORDONARE_ELEMENTE_DIAGONALA_PRINCIPALA ;
const
nmax = 100;
type
vector = array [1..nmax] of integer;
matrice = array [1..nmax, 1..nmax] of integer;
var
x : vector;
m, n, i, j : integer;
A : matrice;
procedure ORDONEAZA (VAR v : vector; m : integer);
var
j, aux, test : integer;
begin
repeat
test := 0;
j := 1;
repeat
if v [j] > v [j + 1] then
begin
aux := v[j];
v [j] := v [j + 1];
v [j + 1] := aux;
test := 1;
end;
j := j + 1
until j > m - 1
until test = 0
end;
Begin { PROGRAM PRINCIPAL }
writeln ('Matricea A este patratica, deci m = n');
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);

20
Probleme rezolvate de programare Subiecte propuse la ATESTAT
writeln;
writeln ('Dati matricea A');
writeln;
for i := 1 to n do
begin
for j:= 1 to n do
begin
write ('Dati A[', i,',',j, '] = ');
readln (A [i,j])
end
end;
writeln;
for i:= 1 to n do
begin
X[i] := A [i,i] {preluam elementele diagonalei principale in X }
end;
writeln;
ORDONEAZA (X, n);
for i:= 1 to n do
begin
A[i,i] := X [i]
end;
writeln;
writeln ('Matricea A dupa ordonarea diagonalei principale este');

for i := 1 to n do
begin
for j:= 1 to n do
begin
write (A[i,j], ' ');
end;
writeln
end;
readln
end.

ATESTAT - 2006 - 18 Fie A (m, n) o matrice cu m linii i n coloane cu elemente ntregi i un


numr ntreg nenul ad <> 0. S se calculeze i s se afieze:
a). numrul d de elemente ale matricii date divizibile cu ad.
b). suma S a elementelor strict negative de pe fiecare coloan.
Exemplu: Pentru m = 3, n = 4, ad = 2
i matricea:
1 2 3 4
5 6 7 8
9 10 11 12
==================
se obin: d = 6
S= 1 16 3 20

Rezolvare: Testul de divizibilitate se face verificnd condiia: A [i, j] MOD ad = 0.


Program ATESTAT_2006_18_DETERMINARI_IN_MATRICE ;
const
nmax = 30;
type
matrice = array [1..nmax, 1..nmax] of integer;
vector = array [1..nmax] of integer;

21
Probleme rezolvate de programare Subiecte propuse la ATESTAT
var
A, B : matrice;
ad, d, m,n,i,j : integer;
s : vector; {vector suma}
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati m = ');
readln (m);
until (m >= 1) and (m <= nmax);
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati matricea A');
for i := 1 to m do
begin
for j := 1 to n do
begin
write ('Dati A [', i,',',j,'] = ');
readln (A [i,j])
end
end;
writeln;
write ('Dati ad = ');
readln (ad);
d:=0;
for i := 1 to m do
begin
for j := 1 to n do
begin
if A [i,j] MOD ad = 0 then
d := d + 1
end
end;
writeln ('Matricea are ', d,' elemente divizibile cu ', ad);
writeln;
writeln;
for j := 1 to n do
begin
s [j] := 0;
for i := 1 to m do
begin
if A [i,j] < 0 then
s [j] := s [j] + A [i,j]
end;
writeln ('S [', j,' = ', s [j] )
end;
readln
end.

ATESTAT - 2006 - 19 Fie A o matrice ptratic de ordinul n, cu elemente numere ntregi. S se


afieze pe ecran rama exterioar a matricii.
Exemplu: Pentru n = 3
i matricea: 1 2 3
5 6 7
9 10 11
se va afia succesiunea de elemente: 1 2 3 7 11 10 9 5.

22
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Rezolvare: Afim elementele de pe prima linie, ultima coloan, ultima linie (de la sfrit ctre
nceput) i, n sfrit, prima coloan (de asemenea, de la sfrit ctre nceput).
Program ATESTAT_2006_19_RAMA_EXTERIOARA_A_MATRICII ;
const
nmax = 100;
type
vector = array [1..nmax] of integer;
matrice = array [1..nmax, 1..nmax] of integer;
var
n, i, j : integer;
A : matrice;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = '); readln (n)
until (n >= 1) and (n <= nmax);
writeln ('Dati matricea A');
writeln;
for i := 1 to n do
begin
for j:= 1 to n do
begin
write ('Dati A[', i,',',j, '] = ');
readln (A [i,j])
end
end;
writeln;
for j := 1 to n do
write ( A[1,j],' '); {elementele de pe prima linie}
for i:= 2 to n do
write ( A [i, n], ' '); {elementele de pe ultima coloana}
for j:= n-1 downto 1 do
write ( A[n, j], ' '); {elementele de pe ultima linie}
for i := n-1 downto 2 do
write ( A[i, 1], ' '); {elementele de pe prima coloana}
readln
end.

ATESTAT - 2006 - 20 Fie A o matrice ptratic de ordinul n, cu elemente numere ntregi. S se


genereze i s se afieze vectorul format din numerele prime de deasupra diagonalei principale.
Exemplu: Pentru n = 5
i matricea:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7 ====> (2 3 5 19 7)
14 23 22 21 8
13 12 11 10 9
Rezolvare: Construim un vector X cu elementele de deasupra diagonalei principale.
Program ATESTAT_2006_20_NUMERE_PRIME_DE_DEASUPRA_DIAGONALEI_PRINCIPALE ;
const
nmax = 100;
type
vector = array [1..nmax] of integer;
matrice = array [1..nmax, 1..nmax] of integer;

23
Probleme rezolvate de programare Subiecte propuse la ATESTAT
var
prim, x : vector;
t, p, k, m, n, i, j, nrdiv, divizor: integer;
A : matrice;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = '); readln (n)
until (n >= 1) and (n <= nmax);
writeln ('Dati matricea A');
for i := 1 to n do
begin
for j:= 1 to n do
begin
write ('Dati A[', i,',',j, '] = ');
readln (A [i,j])
end
end;
writeln;
k := 0; {index vector construit cu elemente de deasupra diag. principale}
for i := 1 to n do
begin
for j := 1 to n do
begin
if j > i then
begin
k := k + 1;
x [k] := A [i,j]
end;
end
end;
p := 0; {contor care numara primele gasite in vector}
for t := 1 to k do
begin
nrdiv := 0;
for divizor := 1 to x [t] do
begin
if x [t] MOD divizor = 0 then
nrdiv := nrdiv + 1
end;
if nrdiv <= 2 then
begin
p := p + 1;
prim [p] := x [t]
end;
end;
for i := 1 to p do
begin
writeln ('Prim [', i, '] = ', prim [i] );
end;
readln
end.

ATESTAT - 2006 - 21 Fie A o matrice ptratic de ordinul n, cu elemente numere ntregi. S se


genereze i s se afieze vectorul format din numerele palindroame de sub diagonala secundar.

Rezolvare: Construim un vector cu elementele de sub diagonala secundar.


n vector avem elemente ncepnd cu linia i = 2. Pe aceast linie avem doar ultimul element (j = n)

24
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Observm c: pe linia i = 2, indexul de coloan j ia o singur valoare j = n, deci jmin = n
pe linia i = 3, indexul de coloan j ia valori de la jmin = n-1 la n;
pe linia i = 4, indexul de coloan j ia valori de la jmin = n-2 la n;
pe o linie oarecare i, indexul de coloan j ia valori de la jmin = n (i 2) la n.
n concluzie, jmin = n + 2 i, pentru orice linie i ntre 2 i n.
Deci, cel mai mic index de coloan, pe o linie i de sub diagonala secundar, este jmin = n + 2 - i.
Altfel spus, pe o linie i vom avea doar elementele cu indexul de coloan jde la n + 2 - i la n.
Program ATESTAT_2006_21_PALINDROAME_DE_SUB_DIAGONALA_SECUNDARA ;
uses CRT;
const
nmax = 100;
TYPE
matrice = array [1..nmax, 1..nmax] of integer;
vector = array [1..nmax] of integer;
var
p, numar, m, n, i, j, t, q : integer;
A : matrice;
x, v : vector;
k : BOOLEAN;
function NRCIFRE (numar : integer) : integer;
var
nr : integer; {retine nr. cifre}
begin
if numar = 0 then
nr := 1
else
begin
nr := 0;
while numar <> 0 do
begin
numar := numar DIV 10;
nr := nr + 1
end
end;
NRCIFRE := nr
end;
function SIMETRIC (numar : integer): boolean;
var
z1, nr : integer;
cif : vector;
begin
nr := NRCIFRE (numar);
z1 := numar;
writeln;
for i := 1 to NRCIFRE (numar) do
begin
cif [ i ] := z1 MOD 10;
{ writeln ('cif [',i,'] = ', cif [i]); }
z1 := z1 DIV 10;
end;

K := TRUE; {presupun "numar" = SIMETRIC}

for i := 1 to nr DIV 2 do
begin
if cif [i] <> cif [nr - i + 1] then
k := FALSE
end;
SIMETRIC := k
end; {sfarsit functie SIMETRIC}

25
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Begin { PROGRAM PRINCIPAL }
clrscr;
repeat
write ('Dati n = '); readln (n)
until (n >= 1) AND (n <= nmax);

writeln ('Dati matricea A');


for i := 1 to n do
begin
for j := 1 to n do
begin
write ('Dati A [',i,',',j,'] = ');
readln (A [i,j])
end
end;
writeln;
writeln ('Matricea A este:');
for i := 1 to n do
begin
for j := 1 to n do
begin
write (a[i,j],' ')
end;
writeln;
end;
writeln;
{construire vector V cu elemente de SUB diagonala secundara}
writeln ('Elementele de SUB diagonala SECUNDARA sunt:');
writeln;
t := 0; {index in vectorul V}
for i:= 2 to n do {pe linia i = 1 nu exista elemente SUB diag sec}
begin
for j := n + 2 - i to n do
begin
t := t + 1;
V [t] := a [i,j];
write ( V [t], ' ' );
end;
end;
writeln ('Numarul Total de elemente SUB diagonala SECUNDARA, t = ', t);
q := 0; {contor pt. elemente simetrice}
writeln;
writeln ('Elemente SIMETRICE SUB diagonala SECUNDARA:');
for p := 1 to t do
{caut in vectorul cu elemente de SUB diag sec}
begin
if SIMETRIC ( v [p] ) = TRUE then
begin
writeln ('V [', p,'] = ', V [p] );
q := q + 1; {am gasit un numar SIMETRIC, il numaram}
x [q] := v [p]; {retin elementul gasit in vectorul X}
{ writeln ( x [q] ); }
end;
end;
writeln ('TOTAL elemente SIMETRICE SUB diagonala SECUNDARA = ', q);
readln
end.

26
Probleme rezolvate de programare Subiecte propuse la ATESTAT
ATESTAT - 2006 - 22 Se dau matricile A i B de tip (m, n) cu elemente numere reale. S se
genereze i s se afieze matricile C i D de tip (m, n), astfel nct C = A + B, D = A B, ale cror
elemente sunt definite astfel:
C [i, j] = A [i, j] + B [i, j]
D [i, j] = A [i, j] B [i, j] pentru orice i = 1 . . m i j = 1 . . n.
Rezolvare: Matricile C i D se construiesc foarte simplu n dou bucle FOR
Program ATESTAT_2006_22_MATRICI_SUMA_SI_DIFERENTA ;
const
nmax = 30;
type
matrice = array [1..nmax, 1..nmax] of integer;
var
C, D, A, B : matrice;
m, n, i, j : integer;

Begin { PROGRAM PRINCIPAL }


repeat
write ('Dati m = ');
readln (m);
until (m >= 1) and (m <= nmax);

repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);

writeln;
writeln ('Dati matricea A');
for i := 1 to m do
begin
for j := 1 to n do
begin
write ('Dati A [', i,',',j,'] = ');
readln (A [i,j])
end
end;
writeln;

writeln ('Dati matricea B');


for i := 1 to m do
begin
for j := 1 to n do
begin
write ('Dati B [', i,',',j,'] = ');
readln (B [i,j])
end
end;
writeln;

for i := 1 to m do
begin
for j := 1 to n do
begin
C [i, j] := A [i,j] + B [i,j];
end
end;
writeln;

27
Probleme rezolvate de programare Subiecte propuse la ATESTAT
for i := 1 to m do
begin
for j := 1 to n do
begin
D [i, j] := A [i,j] - B [i,j];
end
end;
writeln;

writeln ('Matricea C este');


for i := 1 to m do
begin
for j := 1 to n do
begin
write ( C [i,j], ' ');
end;
writeln
end;
writeln;

writeln ('Matricea D este');


for i := 1 to m do
begin
for j := 1 to n do
begin
write ( D [i,j], ' ');
end;
writeln;
end;
writeln;
readln
end.

ATESTAT - 2006 - 23 Pentru un ir de n numere naturale, s se calculeze recursiv cel mai


mare divizor comun al lor.

Rezolvare: Prezentm dou variante:

VARIANTA 1

Definim o funcie recursiv CMMDC (A, B), n care:


- Dac B = 0, CMMDC = A (B = 0 este i condiia de oprire a funciei recursive CMMDC);
- Dac B <> 0, funcia CMMDC se va apela cu parametrii B, respectiv R = A MOD B.
Desigur, aceast funcie se poate aplica doar celor dou numere A i B. De aceea, vom folosi un
vector auxiliar Caux, n care vom reine CMMDC temporari. De exemplu:
- Dup introducerea lui x [ 1 ] , Caux [ 1 ] = x [ 1 ];
- Dup introducerea lui x [ 2 ], Caux [ 2 ] = CMMDC ( x [ 2 ], Caux [ 1 ] );
- ..
- Dup introducerea lui x [ i ], Caux [ i ] = CMMDC ( x [ i ], Caux [ i - 1 ] );
- ..
- Dup introducerea lui x [ n ], Caux [ n ] = CMMDC ( x [ n ], Caux [ n - 1 ] ).
n final CMMDC = Caux [ n ].

28
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Program ATESTAT_2006_23_CMMDC_RECURSIV_ VARIANTA_1 ;
uses CRT;
CONST
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
VAR
x, Caux : vector; {Caux = vector auxiliar in care se retin CMMDC temporari}
C, n, i : integer;

function CMMDC (A, B : integer) : integer;


var
aux, R : integer;
begin
if A < B then
begin
aux := A;
A := B;
B := aux
end;
IF B = 0 then
CMMDC := A
Else
Begin
R := A MOD B;
CMMDC := CMMDC (B, R) sau CMMDC := CMMDC (B, A MOD B)
End;
End; {sfrsit functie CMMDC}

Begin { PROGRAM PRINCIPAL }


CLRSCR;
write ('Dati nr. de elemente ale vectorului, n = ');
readln (n);
writeln;
write ('Dati primul element, x [ 1 ] = ');
readln (x [ 1] );
Caux [ 1 ] := x [ 1 ];
C := Caux [ 1 ];
writeln ('Pana in acest moment, CMMDC = ', C);
writeln;
for i:= 2 to n do
begin
write ('Dati x [ ', i,' ] = ');
readln (x [i]);
Caux [ i ] := CMMDC ( x [i], Caux [i - 1] );
C := Caux [i];
writeln ('Pana in acest moment, CMMDC = ', C);
writeln;
end;
writeln ('C = ', C);
readln
end.

VARIANTA 2

Pentru un ir de n numere, se poate defini o funcie recursiv, notat DIVCOM, care calculeaz
CMMDC prin metoda DEI. Se calculeaz CMMDC al primei jumti a irului de numere i se
reine rezultatul n variabila C1; apoi se calculeaz CMMDC al celei de-a doua jumti i se reine
rezultatul n variabila C2. n final, se determin CMMDC al C1 i C2.

29
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Program ATESTAT_2006_23_CMMDC_RECURSIV_VARIANTA_2_DEI ;
CONST
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
VAR
x : vector;
C, n, i : integer;

function DIVCOM (incep, sf : integer): integer;


{ determina CMMDC prin DEI }
VAR
mij, C1, C2 : integer;
begin
if incep = sf then
DIVCOM := x [incep]
else
begin
if sf - incep = 1 then
DIVCOM := CMMDC ( x [incep], x [sf] )
else
begin
mij := (incep + sf) DIV 2;
C1 := DIVCOM (incep, mij);
C2 := DIVCOM (mij + 1, sf);
DIVCOM := CMMDC (C1, C2)
end
end
end; { sf. fct. DIVCOM }

Begin { PROGRAM PRINCIPAL }


write ('Dati nr. de elemente ale vectorului, n = ');
readln (n);

for i:= 1 to n do
begin
write ('Dati x [ ', i,' ] = ');
readln (x [i])
end;
C := DIVCOM (1, n);
writeln ('C = ', C);
readln
end.

ATESTAT - 2006 - 24 Se citesc pe rnd n numere naturale. Folosind o funcie recursiv,


verificai care sunt termeni ai irului lui Fibonacci i afiai aceti termeni cte unul pe un rnd.

Rezolvare: Definim o funcie recursiv FIBO, care calculeaz termenii irului lui Fibonacci.
Definim nc o funcie numit VERIFICA, pentru a testa dac un numr este sau nu termen al
irului lui Fibonacci.
Program ATESTAT_2006_24_FIBONACCI_RECURSIV ;
VAR
n, i : integer;
x : integer;

30
Probleme rezolvate de programare Subiecte propuse la ATESTAT
function FIBO (m : integer) : integer;
begin
if (m = 1) or (m=2) then
FIBO := 1
else
FIBO := FIBO (m-1) + FIBO (m-2)
end;
function VERIFICA (y : integer) : boolean;
label
10;
var
j : integer;
k : boolean;
begin
k := FALSE; {presupun ca y nu este element al sirului Fibo}
j := 1;
while FIBO (j) <= y do
begin
if FIBO (j) = y then
begin
k := TRUE;
GOTO 10;
end;
j := j + 1;
end;
10: VERIFICA := k
end;
Begin { PROGRAM PRINCIPAL }
write ('Dati n = ');
readln (n);
writeln ;
writeln ('Dati numerele ');
for i := 1 to n do
begin
write ('x = ');
readln (x);
if VERIFICA (x) = TRUE then
writeln (x,' apartine sirului lui Fibonacci');
end;
readln
end.

ATESTAT - 2006 - 25 Scriei o funcie recursiv care ntoarce oglinditul unui numr natural.
Exemplu, pentru n = 123 se afieaz 321

Rezolvare: Notm cu INVERS funcia recursiv cerut, n care facem apel la funcii specifice
datelor de tip STRING: LENGTH i COPY.
Program ATESTAT_2006_25_INVERSAREA_ELEMENTELOR_UNUI_SIR_CU_FUNCTIE_RECURSIVA ;
uses CRT;
VAR
sir, inv : string;
function INVERS ( s : string ) : string;
begin
if s = '' then
INVERS := ''
else
INVERS := s [ LENGTH (s) ] + INVERS ( COPY (s, 1, LENGTH (s) - 1 ));
end; { sfarsit functie }

31
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Begin { PROGRAM PRINCIPAL }
write ('Introduceti sirul : ');
readln (sir);
writeln;
inv := INVERS ( sir );
writeln ('Sirul invers este : ', inv );
readln
end.

ATESTAT - 2006 - 26 S se determine i s se afieze reprezentarea unui numr natural dat


ntr-o baz b, b < 10, folosind o funcie recursiv (s se transforme un numr natural din baza 10
ntr-o alt baz de numeraie b, b < 10, folosind o funcie recursiv).

Rezolvare: A se vedea i alte variante de rezolvare prezentate la pag. 140 (ATESTAT - 2005 28)
Program ATESTAT_2006_26_TRANSFORMARE_BAZA_10_IN_BAZA_b_VARIANTA_RECURSIVA;
var
n: integer;
b: byte;
procedure TRANSFORMA (x: integer; b:byte);
var r: byte;
begin
if x > 0 then
begin
r := x MOD b;
TRANSFORMA (x DIV b, b);
write (r);
end;
end;
Begin { PROGRAM PRINCIPAL }
write ('Dati n = ');
readln (n);
write ('Dati noua baza de numeratie, b = ');
readln (b);
writeln ('Reprezentarea numarului ',n,' in baza ',b,' este:');
TRANSFORMA (n, b);
readln;
end.

ATESTAT - 2006 - 27 S se descompun recursiv un numr n N, transmis ca parametru,


ntr-o sum de puteri ale lui 2, puterile fiind n ordine strict descresctoare.
Exemplu: 14 = 8 + 4 + 2 = 23 + 22 + 21.

Rezolvare: Definim funciile PUTERE (pentru ridicare la putere), TRANSFORMA (pentru a


converti un numr zecimal n numr binar) i NRCIF (pentru numrarea cifrelor unui numr).
Program ATESTAT_2006_27_DESCOMPUNERE_IN_SUMA_DE_PUTERI_ALE_LUI_2;
uses CRT;
const
nmax = 30;
type
vector = array [1..nmax] of integer; {vector cifre numar}
var
c : vector; {vector cifre numar sau vector coeficienti ai
puterilor lui 2}

32
Probleme rezolvate de programare Subiecte propuse la ATESTAT
zecim, n: integer;
b: byte;
x1, x : integer;
i, nrcifre : integer;
ZECIMAL, BINAR, nr10 : integer; {numarul zecimal}
nr2inversat, nr2 : integer; {numar binar inversat respectiv nr. binar
final}
R : vector; {vector de resturi}
j, k, rest : integer;
function PUTERE (z : integer; m : integer): integer;
var
p, j : integer;
begin
p := 1;
for j := 1 to m do
p := p * z;
PUTERE := p
end;
procedure TRANSFORMA (x:integer; b:byte);
var
r:byte; {r = REST}
begin
i := 0;
if x > 0 then
begin
i := i + 1;
r := x mod b; {b = baza de numeratie}
TRANSFORMA (x DIV b, b);
write (r);
end;
end;
function NRCIF (y : integer) : integer;
var
nr : integer;
begin
if y = 0 then
nrcif := 0
else
NRCIF := 1 + NRCIF (y DIV 10)
end;
Begin { PROGRAM PRINCIPAL }
clrscr;
write ('Dati numarul zecimal = '); readln (nr10);
zecimal := nr10; {conservam valoarea initiala a numarului zecimal}
n := nr10;
write ('Dati baza de numeratie, b = '); readln (b);
writeln ('Reprezentarea numarului ',n,' in baza ',b,' este:');
writeln;
TRANSFORMA (n, b);
writeln;
nrcifre := NRCIF (n);
writeln ('Numarul ', n,' are ', nrcifre,' cifre');
if nr10 = 0 then
begin
ZECIMAL := 0;
i :=1;
R [i] := 0
end
else
begin
i := 0;
ZECIMAL := nr10;

33
Probleme rezolvate de programare Subiecte propuse la ATESTAT
while nr10 <> 1 do
begin
rest := nr10 MOD 2;
i := i + 1;
R [i] := rest;
nr10 := nr10 DIV 2;
end;
i := i + 1;
r [i] := nr10;
end;
n := i;
writeln; writeln;
write ('Numarul binar echivalent lui ', ZECIMAL,' este ');
for i := n downto 1 do
begin
write ( R[i] );
end;
writeln; writeln;
writeln ('Numarul zecimal ', ZECIMAL,' se poate scrie ca suma puterilor lui
2 astfel:');
writeln;
for i := n downto 2 do
begin
write (r[i] * putere (2, i-1), ' + ');
end;
write (r[i] * putere (2, 0));
readln;
end.

ATESTAT - 2006 - 28 Scriei o funcie (iterativ sau recursiv) care afieaz valoarea unui
polinom n punctul a. Coeficienii polinomului sunt dai ntr-un vector.
Astfel, pentru v = (1, 2, 3), avem polinomului P = x2 + 2x + 3.

Rezolvare:
Plecm de la observaia c vectorul conine coeficienii polinomului n ordine descresctoare a
puterilor lui x, inclusiv puterea zero. Definim funcii i proceduri ale cror roluri rezult din nume.

Program ATESTAT_2006_28_CALCUL_VALOARE_POLINOM_INTR-UN_PUNCT;
uses Crt;
const
GMaxPol = 20; {Gradul MAXim al unui POLinom}
TYPE
polinom = record
GRAD : 0..GMaxPol;
COEF : array [ 0..GMaxPol ] of real;
end;
VAR
POL1, POL2, POLREZ, PREST : polinom;
NUMAR, A, X, VALOARE : real;
OK, EROARE : boolean;
OPTIUNE, CC : char;
function ValPolinom (var POL : polinom; VALX : real) : real;
var {calcularea Valoarea unui Polinom}
i : integer;
VALPOL : real;
begin
i := POL.GRAD;
VALPOL := POL.COEF [i]; {coeficientul monomului de grad maxim}

34
Probleme rezolvate de programare Subiecte propuse la ATESTAT
while i > 0 do
begin
i := i - 1;
VALPOL := VALPOL * VALX + POL.COEF [i];
end;{while}
ValPolinom := VALPOL;
end;{function}

function RoundR (NUMAR:real; ZEC:integer) : real;


var
P10 : real;
i : integer;
begin
P10 := 1;
for i := 1 to ZEC do
P10 := P10 * 10; {end for}
RoundR := Round (NUMAR * P10) / P10;
end;{function}

procedure CitestePol (MESAJ:string; var POL:polinom);


var
i:integer;
begin
repeat
WriteLn;
WriteLn (MESAJ,' :');
Write (' - introduceti gradul: ');
ReadLn (POL.GRAD);
if POL.GRAD > GMaxPol then
begin
WriteLn('Atentie! Gradul nu poate fi mai mare de ',GMaxPol,' !');
Sound(1200); {Beep}
Delay(200);
NoSound;
end;{if}
if POL.GRAD<0 then
begin
WriteLn('Atentie ! Gradul nu poate fi mai mic decat 0 !');
Sound(1200); Delay(200); NoSound; {Beep}
end;{if}
until (0 <= POL.GRAD) and (POL.GRAD <= GMaxPol);
for i := POL.GRAD downto 0 do
begin
Write (' - Coeficientul lui X^', i:2,': ');
ReadLn ( POL.COEF [i] );
POL.COEF[i] := RoundR (POL.COEF [i], 5); {cel mult 5 zecimale}
end;{for}
end;{procedure}

function NrRealSir (NUMAR : real; SEMN : boolean) : string;


var L, R : integer;
S : string;
begin
Str (NUMAR:20:5,S);
L:=1;
while S [L] = ' ' do
L := L + 1; {sterge spatiile nesemnificative}
R := 20;
while S [R] = '0' do
R := R - 1; {sterge zerourile nesemnificative}
if R = 15 then
R := 14; {sterge punctul situat dupa numerele intregi}
S := Copy (S, L, R-L + 1);

35
Probleme rezolvate de programare Subiecte propuse la ATESTAT
if SEMN then
begin
if NUMAR >= 0.0 then
S := ' + ' + S;
S := copy (S, 1, 1) + ' ' + copy (S, 2, Length (S) - 1);
end;{if}
NrRealSir:=S;
end;{function}

procedure Titlu (MESAJ: string);


var i:integer;
begin
ClrScr;
WriteLn ( MESAJ,' :');
for i:=1 to Length (MESAJ) + 2 do
Write ('='); {end for}
Window (1, 4, 80, 25);
end;{procedure}

procedure AfiseazaPol (MESAJ:string; var POL:polinom);


var i, L, R : integer;
S : string;
begin
WriteLn;
WriteLn (MESAJ,' (de gradul ', POL.GRAD:2,') :');
for i := POL.GRAD downto 0 do
begin
if (i>0) and ( Abs (POL.COEF [i]) = 1.0) then
S := '' {nu afiseaza coeficientul + /- 1, dar va afisa 0}
else
S := NrRealSir ( Abs (POL.COEF [i]), False); {end if}
if POL.COEF[I]<0.0 then
S := ' - ' + S
else
if i < POL.GRAD then
S := ' + ' + S
else
S := ' ' + S; {end if}
Write (S);
if i > 9 then
Write ('x^', i:2)
else
if i > 1 then
Write('x^', i:1)
else
if i = 1 then
Write ('x')
else {i=0}
WriteLn; {end if}
end;{for}
end;{procedure}

Begin { PROGRAM PRINCIPAL }


repeat
CLRSCR; {sterge ecranul si bufferul tastaturii}

while KeyPressed do
begin
CC := ReadKey;
if Ord (CC) = 0 then
CC := ReadKey;
end;{while}
{afiseaza meniul principal}

36
Probleme rezolvate de programare Subiecte propuse la ATESTAT
writeln;
writeln (' Alegeti una dintre optiunile de mai jos:');
writeln;
WriteLn(' 1 - Pentru a calcula valoarea unui polinom intr-un punct X dat');
WriteLn;
WriteLn(' <Esc> - Pentru Terminarea programului ');
writeln;
Write (' Introduceti optiunea dvs : ');
{citeste OPTIUNEA solicitata}

repeat
until KeyPressed;

OPTIUNE := ReadKey;
if Ord (OPTIUNE)=0 then
CC := ReadKey;
{anuleaza bufferul tastaturii}

while KeyPressed do
begin
CC := ReadKey;
if Ord (CC) = 0 then
CC := ReadKey;
end;{while}
{executa operatia solicitata}
OK:=True;

CASE OPTIUNE of
'1' : begin Titlu ('1. Valoarea unui polinom pentru un X dat');
CitestePol ('Se calculeaza valoarea polinomului',POL1);
WriteLn;
Write ('pentru X = : ');
ReadLn(X);
X := RoundR (X,5); {cel mult 5 zecimale}
VALOARE := ValPolinom (POL1,X);
ClrScr;
AfiseazaPol ('Se calculeaza valoarea polinomului',POL1);
WriteLn;
WriteLn ('pentru X = ', NrRealSir (X,False),' ;');
WriteLn;
WriteLn('Se obtine valoarea ',NrRealSir(VALOARE,False),' !');
end;
else OK:=False;
end;{case}

if OK then
begin
WriteLn;
WriteLn;
Write ('Apasati o tasta oarecare ... ');
repeat
until KeyPressed;
Window (1,1,80,25);
end;{if}

until Ord (OPTIUNE) = 27; {Pana se apasa ESC}


end. {sfarsit program}

37
Probleme rezolvate de programare Subiecte propuse la ATESTAT
ATESTAT - 2006 - 29 Se citete o valoare n. S se scrie un subprogram recursiv care s
calculeze valoarea expresiei: E = 2 * 4 * 6 * 8* * (2 * n)

Rezolvare: Definim funcia EXPRESIE al crei algoritm recursiv se aseamn cu algoritmul de


calculare a lui xn.
Program ATESTAT_2006_29_CALCUL_RECURSIV_EXPRESIE ;
var
E, n : integer;

function EXPRESIE (m : integer) : integer;


begin
IF m = 0 then
EXPRESIE := 1
else
EXPRESIE := 2 * m * EXPRESIE (m - 1)
end;
Begin { PROGRAM PRINCIPAL }
write ('Dati n = ');
readln (n);

E := EXPRESIE (n);
writeln ('E = ', E);
readln
end.

ATESTAT - 2006 - 30 Se citete un numr ntreg i pozitiv. S se scrie un subprogram recursiv


care s determine suma cifrelor impare ale numrului.

Rezolvare: Definim funciile NRCIF (pentru numrarea cifrelor unui ntreg) i SUMAIMPARE
(pentru determinarea sumei cifrelor impare ale unui numr).
Program ATESTAT_2006_30_SUMA_CIFRE_IMPARE ;
const
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
var
S1, x, x1, n, i : integer;
y : vector;
k : BOOLEAN;
function NRCIFRE (z : integer) : integer;
var
m : integer;
begin
if z = 0 then
m := 1
else
begin
m := 0;
while z <> 0 do
begin
z := z DIV 10;
m := m + 1
end
end;
NRCIFRE := m
end;

38
Probleme rezolvate de programare Subiecte propuse la ATESTAT
function SUMAIMPARE (z : integer) : integer;
var
s, j : integer;
y : vector;
begin
s := 0;
for i := 1 to n do
begin
y [ i ] := x1 MOD 10;
writeln ('y [',i,'] = ', y [i]);
x1 := x1 DIV 10;
end;
for j := 1 to NRCIFRE (z) do
begin
if y [j] MOD 2 <> 0 then
s := s + y [j]
end;
SUMAIMPARE := s;
end;

Begin { PROGRAM PRINCIPAL }


write ('Introduceti un numar natural x = ');
readln (x);
n := NRCIFRE (x);
writeln ('Numarul ',x,' are ',n,' cifre');
writeln;
x1 := x;
s1 := SUMAIMPARE (x);
writeln ('Suma cifrelor impare = ', S1);
readln
end.

ATESTAT - 2006 - 31 Se citete o valoare natural n. S se scrie cte un subprogram recursiv


care s calculeze valoarea expresiilor :

E1 = 1 * 4 * 7 * (3n 2)
E2 = 2 4 + 6 + + (1)n + 1 (2 n)

Rezolvare: Funciile PUTERE, EXPRESIE1 i EXPRESIE2 au algoritmi asemntori.


Program ATESTAT_2006_31_CALCUL_RECURSIV_EXPRESII ;
var
E1, E2, n : integer;

function PUTERE (x : integer; m : integer) : integer;


var
p, i : integer;
begin
p := 1;
for i := 1 to m do
begin
p := p * x
end;
PUTERE := p
end;

39
Probleme rezolvate de programare Subiecte propuse la ATESTAT
function EXPRESIE1 (m : integer) : integer;
begin
if m = 0 then
EXPRESIE1 := 1
else
EXPRESIE1 := (3*m -2) * EXPRESIE1 (m-1)
end;
function EXPRESIE2 (m : integer) : integer;
begin
if m = 0 then
EXPRESIE2 := 0
else
EXPRESIE2 := PUTERE (-1, m + 1) * (2 * m) + EXPRESIE2 (m-1)
end;
Begin { PROGRAM PRINCIPAL }
write ('Datin n = ');
readln (n);
E1 := EXPRESIE1 (n);
E2 := EXPRESIE2 (n);
writeln ('E1 = ', E1);
writeln;
writeln ('E2 = ', E2);
readln
end.

ATESTAT - 2006 - 32 Se citesc dou numere naturale diferite a i b. Cerine:


Definii o funcie recursiv care calculeaz valoarea ab.
Definii o funcie recursiv care calculeaz suma valorilor din intervalul [a, b].

Rezolvare: Funciile recursive PUTERE i SUMA se bazeaz pe algoritmi cunoscui.


Program ATESTAT_2006_32_FUNCTII_RECURSIVE ;
var
a, b, p, s : integer;
function PUTERE (y: integer; m : integer) : integer;
begin
if m = 0 then
PUTERE := 1
else
PUTERE := y * PUTERE (y, m-1)
end;
function SUMA (a1, b1 : integer) : integer;
var
s1, i : integer;
begin
s1 := 0;
for i := a1 to b1 do
s1 := s1 + i;
SUMA := s1
end;
Begin { PROGRAM PRINCIPAL }
write ('Dati a = '); readln (a);
write ('Dati b = '); readln (b);
p := PUTERE (a, b);
writeln ('P = ', P);
s := SUMA (a, b);
writeln ('S = ', s);
readln
end.

40
Probleme rezolvate de programare Subiecte propuse la ATESTAT
ATESTAT - 2006 - 33 Se citete de la tastatur un vector cu n elemente numere ntregi.
Cerine:
Scriei o funcie recursiv care calculeaz produsul elementelor impare din vector.
Scriei o funcie recursiv sau iterativ care determin dac elementele situate simetric fa de
mijlocul vectorului sunt egale.

Rezolvare: Funcia PRODUS este recursiv; funcia VERIFIC este iterativ.


Program ATESTAT_2006_33_FUNCTII_RECURSIVE ;
const
nmax = 30;
type
vector = array [1..nmax] of integer;
var
x, y : vector;
mij, P, n, i, j, impare : integer;
b : boolean;
function PRODUS (m : integer): integer;
begin
if m = 0 then
PRODUS := 1
else
PRODUS := y [m] * PRODUS (m-1)
end;
function VERIFICA (v : vector) : boolean;
begin
b := TRUE;
for i := 1 to n DIV 2 do
begin
if v [i] <> v [n - i + 1] then
b := FALSE
end;
VERIFICA := b;
end;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = '); readln (n)
until (n >= 1) and (n <= nmax);
writeln ('Dati vectorul de intregi');
for i := 1 to n do
begin
write ('Dati X [ ', i,' ] = '); readln (x[i])
end;
impare := 0; {contor pentru numere impare}
for i:= 1 to n do
begin
if x [i] MOD 2 <> 0 then
begin
impare := impare + 1;
y [impare] := x [i]; {construiesc vectorul de impare}
end;
end;
p := PRODUS (impare);
writeln ('P = ', P);
writeln;
if VERIFICA (x) then
writeln ('Vectorul este SIMETRIC')
else
writeln ('Vectorul este NESIMETRIC');
readln
end.

41
Probleme rezolvate de programare Subiecte propuse la ATESTAT
ATESTAT - 2006 - 34 Se citete de la tastatur un vector cu n elemente numere ntregi.
Cerine:
Scriei o funcie recursiv care calculeaz suma elementelor vectorului.
Scriei o funcie recursiv sau iterativ care verific dac exist cel puin o valoare pozitiv n
vector.

Rezolvare: Funcia SUMA este recursiv; funcia VERIFIC este iterativ.


Program ATESTAT_2006_34_FUNCTII_RECURSIVE ;
const
nmax = 30;
type
vector = array [1..nmax] of integer;
var
x, y : vector;
mij, s, n, i, j, k : integer;
b : boolean;

function SUMA (m : integer): integer;


begin
if m = 0 then
SUMA := 0
else
SUMA := x [m] + SUMA (m-1)
end;
function VERIFICA (v : vector) : boolean;
begin
b := FALSE;
for i := 1 to n do
begin
if v [i] > 0 then
b := TRUE
end;
VERIFICA := b;
end;

Begin { PROGRAM PRINCIPAL }


repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati vectorul de intregi');
for i := 1 to n do
begin
write ('Dati X [ ', i,' ] = ');
readln (x[i])
end;
writeln;
s := SUMA (n);
writeln ('S = ', S);
writeln;

if VERIFICA (x) then


writeln ('Vectorul are cel putin un element pozitiv')
else
writeln ('Vectorul nu are elemnte pozitive');
readln
end.

42
Probleme rezolvate de programare Subiecte propuse la ATESTAT
ATESTAT - 2006 - 35 Fie X un ir de n numere ntregi i k N, 1 < k < n. S se scrie o funcie
care s afieze irul astfel nct primele k numere s fie n ordine cresctoare i restul n ordine
descresctoare.

Rezolvare: Reinem numerele introduse n vectorul x. Procedurile ORDONEAZACRESCATOR


i ORDONEAZADESCRESCATOR vor sorta k, respectiv n - k elemente ale vectorului x.
Program ATESTAT_2006_35_ORDONARI_ELEMENTE_VECTOR ;
const
nmax = 100;
type
vector = array [1..nmax] of integer;
var
x, y, z : vector;
n, i, k, aux : integer;
a, b : integer;
procedure ORDONEAZACRESCATOR (VAR v : vector; m : integer);
var
j, aux, test : integer;
begin
repeat
test := 0;
j := 1;
repeat
if v [j] > v [j + 1] then
begin
aux := v[j];
v [j] := v [j + 1];
v [j + 1] := aux;
test := 1;
end;
j := j + 1
until j > m - 1
until test = 0
end;
procedure ORDONEAZADESCRESCATOR (VAR v : vector; m : integer);
var
j, aux, test : integer;
begin
repeat
test := 0;
j := 1;
repeat
if v [j] < v [j + 1] then
begin
aux := v[j];
v [j] := v [j + 1];
v [j + 1] := aux;
test := 1;
end;
j := j + 1
until j > m - 1
until test = 0
end;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = '); readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati vectorul X');
writeln;

43
Probleme rezolvate de programare Subiecte propuse la ATESTAT
for i:= 1 to n do
begin
write ('Dati X[', i, '] = ');
readln (x[i])
end;
writeln;
write ('Dati pozitia k = ');
readln (k);
for i:= 1 to k do
y [i] := x [i];
for i:= k + 1 to n do
z [i-k] := x [i]
ORDONEAZACRESCATOR (y, k);
ORDONEAZADESCRESCATOR (z, n-k);
for i:= 1 to k do
writeln ('y [',i,'] = ', y[i]);
writeln;
for i:= k + 1 to n do
writeln ('z [',i-k,'] = ', z[i-k]);
readln
end.

ATESTAT - 2006 - 36 Fie X un ir de n numere ntregi. S se scrie o funcie care s afieze irul
astfel nct numerele s se gseasc n ordinea cresctoare a sumei cifrelor (primul numr este cel
care are suma cifrelor cea mai mic).

Rezolvare:
Varianta 1: Definim funcia NRCIFRE care determin numrul cifrelor unui ntreg. Dup
introducerea irului de numere, preluate ntr-un vector X, vom determina, pentru fiecare numr,
numrul de cifre i vom face suma cifrelor fiecrui numr. Toate cele n sume vor fi reinute ntr-un
vector S al sumelor cifrelor numerelor introduse. Ordonm vectorul S (al sumelor) i, n funcie de
ordinea elementelor din S, ordonm i vectorul X.
Program ATESTAT_2006_36_ORDONARI_SUMA_CIFRE_VARIANTA_1 ;
const
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
var
aux, x1, n, nr, i, test, j, s1 : integer;
x, v, S : vector;
k : BOOLEAN;
function NRCIFRE (y : integer) : integer;
var
m : integer;
begin
if y = 0 then
m := 1
else
begin
m := 0;
while y <> 0 do
begin
y := y DIV 10;
m := m + 1
end
end;
NRCIFRE := m
end;

44
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati vectorul');
for i:= 1 to n do
begin
write ('Dati X [', i, '] = ');
readln (x [i])
end;
for i := 1 to n do
begin
nr := NRCIFRE (x [i] );
x1 := x [i];
S1 := 0;
for j := 1 to nr do
begin
v [ j ] := x1 MOD 10;
s1 := S1 + v [j];
x1 := x1 DIV 10;
end;
s [i] := s1;
end;
repeat
test := 0;
i := 1;
repeat
if S [i] > S [i + 1] then
begin
aux := x [i];
x [i] := x [i + 1];
x [i + 1] := aux;
aux := s [i];
S [i] := S [i + 1];
S [i + 1] := aux;
test := 1
end;
i := i + 1;
until (i > n-1)
until (test = 0);
writeln ('Vectorul X ordonat dupa suma cifrelor');
for i:= 1 to n do
writeln ('X [', i,'] = ', X [i] );
readln
end.

Varianta 2: Observnd c la ieire trebuie s avem un vector ordonat (X) n condiiile problemei,
vom defini o procedur AFISEAZA, care va avea ca parametru vectorul X. n afar de aceast
procedur se mai definesc i procedurile CONSTRUIESTEVECTORSUMECIFRE,
CITESTEVECTOR, ORDONEAZA,.

Program ATESTAT_2006_36_ORDONARI_SUMA_CIFRE_VARIANTA_2;
const
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
var
n : integer;
x, S : vector;

45
Probleme rezolvate de programare Subiecte propuse la ATESTAT
function NRCIFRE (y : integer) : integer;
var
m : integer;
begin
if y = 0 then
m := 1
else
begin
m := 0;
while y <> 0 do
begin
y := y DIV 10;
m := m + 1
end
end;
NRCIFRE := m
end;

procedure CITESTEVECTOR (VAR V : vector; n : integer);


VAR
i : integer;
begin
for i := 1 to n do
begin
write ('Dati X [', i, '] = ');
readln ( V [i])
end;
end;

procedure CONSTRUIESTEVECTORSUMECIFRE (VAR S : vector; n : integer);


VAR
i, j : integer; {indici}
nr : integer;
x1 : integer;
S1 : integer;
V : vector;
begin
for i := 1 to n do
begin
nr := NRCIFRE (x [i] );
x1 := x [i];
S1 := 0;
for j := 1 to nr do
begin
v [ j ] := x1 MOD 10;
s1 := S1 + v [j];
x1 := x1 DIV 10;
end;
s [i] := s1;
end;
end;

procedure AFISEAZA (VAR V : vector; n : integer);


var
i : integer;
begin
writeln ('Vectorul X ordonat dupa suma cifrelor');
for i := 1 to n do
writeln (' X [', i,'] = ', V [i] );
end;

46
Probleme rezolvate de programare Subiecte propuse la ATESTAT
procedure ORDONEAZAVECTORSUME ( VAR S : vector; n : integer);
var
test : integer ;
i : integer;
aux : integer;
begin
repeat
test := 0;
i := 1;
repeat
if S [i] > S [i+1] then
begin
aux := x [i];
x [i] := x [i+1];
x [i+1] := aux;
aux := s [i];
S [i] := S [i+1];
S [i+1] := aux;
test := 1
end;
i := i + 1;
until (i > n-1)
until (test = 0);
end;
begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n<= nmax);
writeln;
writeln ('Dati vectorul');
CITESTEVECTOR (X, n);
CONSTRUIESTEVECTORSUMECIFRE ( S, n );
ORDONEAZAVECTORSUME ( S, n);
writeln;
AFISEAZA ( X, n );
readln
end.

ATESTAT - 2006 - 37 Fie X un ir de n numere ntregi. S se scrie o funcie care s afieze irul
obinut din irul dat nlocuind fiecare numr cu oglinditul su.

Rezolvare: Observnd c la ieire trebuie s avem un vector n condiiile problemei, vom defini o
procedur AFISEAZA, care va avea ca parametru acest vector. Se mai definete, de asemenea,
funcia INVERS, care inverseaz un numr.
Program ATESTAT_2006_37_NUMERE_INVERSATE ;
uses CRT;
const
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
var
n, i : integer;
numar, eroare : integer;
x, xinv : vector;
sir, inv : string;

47
Probleme rezolvate de programare Subiecte propuse la ATESTAT
function INVERS ( s : string ) : string;
begin
if s = '' then
INVERS := ''
else
INVERS := s [ LENGTH (s) ] + INVERS ( Copy (s, 1, LENGTH (s) - 1 ));
end; { sfarsit functie }

procedure AFISEAZA (VAR V : vector; n : integer);


var
i : integer;
begin
for i := 1 to n do
writeln (' Xinv [', i,'] = ', V [i] );
end;

Begin { PROGRAM PRINCIPAL }


repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati vectorul');
for i:= 1 to n do
begin
write ('Dati X [', i, '] = ');
readln (x [i]);
STR (x[i], sir); { transforma numarul x[i] intr-un sir}
inv := INVERS (sir);
VAL (inv, numar, eroare); { transforma sirul inv intr-un numar }
xinv [i] := numar;
end;
writeln;
writeln ('Vectorul rezultat este:');
AFISEAZA (xinv, n);
readln
end.

ATESTAT - 2006 - 38 Fie X un ir de n numere ntregi. S se scrie o funcie care s afieze irul
obinut din irul dat nlocuind fiecare numr cu suma divizorilor si.

Rezolvare: Pentru c la ieire trebuie s avem un vector n condiiile problemei, vom defini o
procedur AFISEAZA, care va avea ca parametru acest vector. Mai definim, de asemenea, funcia
SUMADIVIZORI, care determin suma divizorilor unui ntreg.
Program ATESTAT_2006_38_SUMA_DIVIZORI ;
uses CRT;
const
nmax = 100;
TYPE
vector = array [1..nmax] of integer;
var
n, i: integer;
x, Sum : vector;

48
Probleme rezolvate de programare Subiecte propuse la ATESTAT
procedure AFISEAZA (VAR V : vector; n : integer);
var
i : integer;
begin
for i := 1 to n do
writeln (' SUM [', i,'] = ', V [i] );
end;
function SUMADIVIZORI (numar : integer) : integer;
var
S, nrdiv, divizor : integer;
begin
nrdiv := 0;
divizor := 1;
S := 0;
for divizor := 1 to numar do
begin
if numar MOD divizor = 0 then
s := s + divizor
end;
SUMADIVIZORI := S
end;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln;
writeln ('Dati vectorul');
for i:= 1 to n do
begin
write ('Dati X [', i, '] = ');
readln (x [i]);
SUM [i] := SUMADIVIZORI ( x[i] );
end;
writeln;
writeln ('Vectorul rezultat este:');
AFISEAZA (SUM, n);
readln
end.

ATESTAT - 2006 - 39 Fie A o matrice ptratic cu n x n numere ntregi. S se scrie o funcie


care s determine liniile pentru care suma elementelor este un numr prim, afind i valoarea
sumei respective.

Rezolvare: Definim funcia VERIFICANUMAR, care face verificrile cerute de problem pentru
fiecare element al matricei. Funcia are la baz algoritmul de testare a primalitii unui ntreg.
Program ATESTAT_2006_39_SUMA_ELEMENTE_LINIE_MATRICE ;
const
nmax = 30;
type
matrice = array [1..nmax, 1..nmax] of integer;
vector = array [1..nmax] of integer;
var
A : matrice;
n, i, j : integer;
s : vector; {vector suma}

49
Probleme rezolvate de programare Subiecte propuse la ATESTAT
function VERIFICANUMAR (numar : integer) : boolean;
var
nrdiv, divizor : integer;
begin
nrdiv := 0;
divizor := 1;
for divizor := 1 to numar do
begin
if numar mod divizor = 0 then
nrdiv := nrdiv + 1
end;
if nrdiv <= 2 then
VERIFICANUMAR := TRUE
else
VERIFICANUMAR := FALSE
end; {sfarsit functie}
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln ('Dati matricea A');
for i := 1 to n do
begin
for j := 1 to n do
begin
write ('Dati A [', i,',',j,'] = ');
readln (A [i,j])
end
end;
writeln ('Matricea A este');
for i := 1 to n do
begin
for j := 1 to n do
begin
write (A [ i,j] ,' ');
end;
writeln;
end;
writeln;
for i := 1 to n do
begin
s [i] := 0;
for j := 1 to n do
begin
s [i] := s [i] + A [i,j]
end;
if VERIFICANUMAR ( s [i] ) = true then
writeln ('Suma elem. liniei ', i,' este nr. prim ', S [i] )
end;
readln
end.

ATESTAT - 2006 - 40 Fie A o matrice ptratic cu n x n numere ntregi. S se scrie o funcie


care s afieze matricea obinut prin interschimbarea unor elemente, astfel nct suma
elementelor de pe diagonala principal s fie maxim.

Rezolvare: Pentru c la ieire trebuie s avem o matrice n condiiile problemei, vom defini o
procedur AFISEAZA, care va avea ca parametru aceast matrice.

50
Probleme rezolvate de programare Subiecte propuse la ATESTAT
Se mai definete i funcia MAXLINIE, care determin numrul maxim dintr-o linie a matricii.
Program ATESTAT_2006_40_SUMA_ELEMENTE_DIAGONALA_PRINCIPALA ;
const
nmax = 30;
type
matrice = array [1..nmax, 1..nmax] of integer;
vector = array [1..nmax] of integer;
var
A : matrice;
nrlinie, s, n, i, j : integer;
function MAXLINIE ( nrlinie : integer) : integer;
var
max : integer;
begin
max := a [nrlinie, 1];
for j := 2 to n do
begin
if max < a [nrlinie, j] then
max := a [nrlinie, j]
end;
if A [nrlinie, nrlinie] < max then
A [nrlinie, nrlinie] := max;
MAXLINIE := max
end;
procedure AFISEAZA (VAR W : matrice; n : integer);
begin
for i := 1 to n do
begin
for j := 1 to n do
begin
write ( W [ i,j] ,' ');
end;
writeln;
end;
writeln;
end;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = ');
readln (n)
until (n >= 1) and (n <= nmax);
writeln ('Dati matricea A');
for i := 1 to n do
begin
for j := 1 to n do
begin
write ('Dati A [', i,',',j,'] = ');
readln (A [i,j])
end
end;
writeln ('Matricea A, in forma initiala, este');
AFISEAZA (A, n );
for i:= 1 to n do
a [i, i] := MAXLINIE (i)
writeln ('Matricea A DUPA schimbare este');
AFISEAZA (A, n );
s := 0;
for i := 1 to n do
s := s + a [i,i];
writeln ('Suma elementelor diagonalei principale este = ', s);
readln
end.

51
Probleme rezolvate de programare Subiecte propuse la ATESTAT
ATESTAT - 2006 - 41 Fie A o matrice ptratic cu n x n numere ntregi. S se scrie o funcie
care s afieze elementele matricei care au proprietatea c sunt formate, fiecare n parte, din cifre
distincte. Stabilii apoi dac elementele de pe diagonala principal formeaz o secven
palindromic: a 11 = a n n, a 2 2 = a n - 1 n - 1,

Rezolvare:
Vom defini o procedur AFISEAZAELEMENTECUCIFREDISTINCTE, n interiorul creia se
fac testele, n condiiile problemei i care afieaz elementele matricii care au proprietatea cerut. Se
mai definete i funcia VERIFICA, pentru testarea fiecrui element al matricii. Funcia VERIFICA
este apelat n interiorul procedurii AFISEAZA. La rndul ei, funcia VERIFICA face apel la
funcia NRCIFRE, care numr cifrele unui ntreg.
Program ATESTAT_2006_41_SECVENTA_PALINDROMICA_PE_DIAGONALA_PRINCIPALA ;
type
matrice = array [1..100, 1..100] of integer;
vector = array [1..100] of integer;
var
p, q, t, x, x1, n, i, j : integer;
z, diagonala, y : vector;
k : BOOLEAN;
A : matrice;

function NRCIFRE (y : integer) : integer;


var
m : integer;
begin
if y = 0 then
m := 1
else
begin
m := 0;
while y <> 0 do
begin
y := y DIV 10;
m := m + 1
end
end;
NRCIFRE := m
end;

function VERIFICA (numar : integer) : boolean;


begin
x1 := numar;
for p := 1 to NRCIFRE (numar) do
begin
y [ p ] := x1 MOD 10; {vector cu cifrele lui "numar"}
x1 := x1 DIV 10;
end;
k := FALSE; {FALSE - presupun ca "numar" nu are elemente egale}
for p := 1 to NRCIFRE (numar) - 1 do
for q:= p + 1 to NRCIFRE (numar) do
begin
if y [p] = y [q] then
k := TRUE {2 elemente egale}
end;
VERIFICA := k;
end;

52
Probleme rezolvate de programare Subiecte propuse la ATESTAT
procedure AFISEAZAELEMENTECUCIFREDISTINCTE ;
begin
t := 0; {contor pt. distincte}
for i := 1 to n do
begin
for j := 1 to n do
begin
writeln ('a[',i,',',j,'] = ', a[i,j]);
{ writeln ('nrcifre = ', NRCIFRE ( a[i,j])); }
writeln ('VERIFICA = ', VERIFICA (a[i,j]) );
if VERIFICA ( a[i,j]) = FALSE then
begin
t := t + 1;
writeln ('t = ',t);
z [t] := a [i,j]
end;
end;
end;
for i := 1 to t do
begin
writeln ('z [', i,'] = ', z[i] );
end;
end;
Begin { PROGRAM PRINCIPAL }
repeat
write ('Dati n = '); readln (n)
until (n >= 1) and (n <= nmax);
writeln ('Dati matricea A');
for i := 1 to n do
for j := 1 to n do
begin
write ('Dati A [', i,',',j,'] = ');
readln (A [i,j])
end;
AFISEAZAELEMENTECUCIFREDISTINCTE ;

{****************************************************************************}
{ partea a 2-a a problemei }
for i := 1 to n do {transfer diagonala matricei in vectorul "diagonala" }
diagonala [i] := a [i,i];
K := TRUE; {presupun ca exista SIMETRIE pe diagonala principala }
for i := 1 to n DIV 2 do
begin
if diagonala [i] <> diagonala [n - i + 1] then
k := FALSE
end;
if K = TRUE then
writeln ('Elementele diagonalei formeaza o secventa PALINDROMICA')
else
writeln ('Elementele diagonalei NU formeaza o secventa PALINDROMICA');
writeln ('Matricea A este');
for i := 1 to n do
begin
for j := 1 to n do
begin
write (A [ i,j] ,' ');
end;
writeln;
end;
writeln;
readln
end.

53

S-ar putea să vă placă și