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.
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.
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;
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.
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);
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.
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;
10
{ 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;
11
12
{ 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
13
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;
14
begin
write (x[j],'
');
end;
readln
end.
16
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 }
17
18
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
19
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:');
20
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.
21
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}
22
{ 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
23
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.
24
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
begin
do
25
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.
26
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
27
28
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 = ');
29
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;
30
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.
31
32
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
33
34
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]
35
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;
36
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.
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);
38
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.
1
2 3
4
5
6
7 8
9
10 11 12
==================
se obin: d = 6
S=
16
20
40
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]
41
end;
writeln ('S [', j,' = ', s [j] )
end;
readln
end.
42
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.
43
44
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;
45
for i := 1 to p do
begin
writeln ('Prim [', i, '] = ', prim [i] );
end;
readln
end.
46
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
47
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}
48
Begin
{ PROGRAM PRINCIPAL }
clrscr;
repeat
write ('Dati n = '); readln (n)
until (n >= 1) AND (n <= nmax);
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.
50
{ 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])
51
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;
52
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.
54
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.
56
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;
57
58
59
60
Begin
{ PROGRAM PRINCIPAL }
write ('Introduceti sirul : ');
readln (sir);
writeln;
inv := INVERS ( sir );
writeln ('Sirul invers este : ', inv );
readln
end.
62
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;
63
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;
64
66
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
67
Write ('
');
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);
68
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
69
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}
70
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;
71
72
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;
74
{ 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.
76
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.
78
readln
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.
80
82
84
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.
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;
86
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
87
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;
88
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;
90
92
{ 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.
94
96
end;
if VERIFICANUMAR ( s [i] ) = true then
writeln ('Suma elem. liniei ', i,' este nr.
prim ', S [i] )
end;
readln
end.
98
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
99
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.
100
102
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];
103
104