Sunteți pe pagina 1din 98

Probleme rezolvate de programare Subiecte date la ATESTAT

1
ATESTAT - 2008 - 1 Scriei un program care citete de la tastatur dou iruri de
caractere formate din litere mici. S se creeze fiierul BAC.TXT, n care s se scrie unul din
cele dou iruri care se dovedete a fi o subsecven a celuilalt ir. Dac cele dou iruri nu au
aceast proprietate, atunci n fiierul BAC.TXT se va scrie textul FR SOLUIE.
Exemplu:
Se citesc irurile: Se afieaz n BAC.TXT :
calculator lat
lat sau lat este subir al irului calculator
Rezolvare :
Program ATESTAT_2008_1_SUBSECVENTA_SIRURI;
uses CRT;
VAR
vect or si r ur i : ar r ay [ 1. . 2] of STRI NG;
n, i , j , k : i nt eger ;
si r 1, si r 2, si r , subsi r , l i ni e : STRI NG;
GASI T : BOOLEAN; {GASI T = TRUE, daca est e sat i sf acut a cer i nt a
pr obl emei }
BAC : t ext ;
Begin { PROGRAM PRINCIPAL }
cl r scr ;
wr i t el n;
wr i t el n;
ASSI GN ( BAC, ' C: \ BAC. TXT' ) ;
REWRI TE ( BAC) ;
wr i t e ( ' Dat i si r ul 1 = ' ) ;
r eadl n ( si r 1) ;
wr i t e ( ' Dat i si r ul 2 = ' ) ;
r eadl n ( si r 2) ;
wr i t el n;
wr i t el n;
GASIT := FALSE ; {pr esupunem ca si r ur i l e i nt r oduse NU
sat i sf ac cer i nt el e}
i f POS ( sir1, sir2) <> 0 t hen
begi n
GASIT := TRUE; {si r ur i l e SATI SFAC cer i nt el e
pr obl emei }
subsi r : = si r 1;
Probleme rezolvate de programare Subiecte date la ATESTAT
2
si r : = si r 2;
wr i t e ( BAC, subsi r , ' est e subsi r al si r ul ui
' , si r ) ;
wr i t el n ( BAC) ;
end;
i f POS ( sir2, sir1) <> 0 t hen
begi n
GASIT := TRUE; {si r ur i l e SATI SFAC cer i nt el e
pr obl emei }
subsi r : = si r 2;
si r : = si r 1;
wr i t e ( BAC, subsi r , ' est e subsi r al si r ul ui
' , si r ) ;
wr i t el n ( BAC) ;
end;
RESET (BAC);
I F GASI T = FALSE t hen
begi n
wr i t el n;
wr i t el n ( ' FARA SOLUTI E' )
end
ELSE
begi n
whi l e not EOF ( BAC) DO
begi n
r eadl n ( BAC, l i ni e) ;
wr i t el n ( l i ni e)
end;
end;
r eadl n
end.
ATESTAT - 2008 - 2 REPETA 2007 2 Scriei un program care citete de la
tastatur un numr natural n (2 < n < 21) i apoi n linii cu cte n numere ntregi de cel mult 7
cifre ce formeaz un tablou bidimensional A.
S se afieze pe ecran diferena dintre suma elementelor de pe diagonala principal i
suma elementelor de pe diagonala secundar a matricei A.
Rezolvare:
Elementele de pe diagonala principal sunt: A [i, i], pentru i =1 . . n;
Elementele de pe diagonala secundar sunt: A [i, n i + 1] , pentru i =1 . . n.
Probleme rezolvate de programare Subiecte date la ATESTAT
3
Program ATESTAT_2008_2_DIAGONALE_MATRICE;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of i nt eger ;
mat r i ce = ar r ay [ 1. . nmax, 1. . nmax] of i nt eger ;
VAR
x : vect or ;
m, n, i , j : i nt eger ;
A : mat r i ce;
Di f , Spr i nci pal a, Ssecundar a : i nt eger ;
Begin { PROGRAM PRINCIPAL }
r epeat
wr i t e ( ' Dat i n = ' ) ;
r eadl n ( n)
unt i l ( n>=1) and ( n <=nmax) ;
wr i t el n;
wr i t el n ( ' Dat i el ement el e mat r i ci i A' ) ;
wr i t el n;
f or i : = 1 t o n do
begi n
f or j : = 1 t o n do
begi n
wr i t e ( ' Dat i A[ ' , i , ' , ' , j , ' ] = ' ) ;
r eadl n ( A [ i , j ] )
end
end;
wr i t el n;
Spr i nci pal a : = 0;
Ssecundar a : = 0;
f or i : = 1 t o n do
begi n
Spr i nci pal a : = Spr i nci pal a + A [i, i] ;
Ssecundar a : = Ssecundar a + A [i, n i + 1]
end;
Di f : = Spr i nci pal a - Ssecundar a;
wr i t el n ( ' Di f er ent a di nt r e sumel e cel or doua di agonal e est e
: ' , Di f ) ;
r eadl n
end.
ATESTAT - 2008 3 (REPETA 2007 3) Fiierele text X.TXT i Y.TXT conin
fiecare numele a 7 persoane, cte un nume pe fiecare linie, fiecare nume avnd cel mult 15
litere. tiind c n fiecare fiier numele sunt memorate n ordine alfabetic, scriei un program
care s citeasc din cele dou fiiere i s afieze pe ecran toate numele din cele dou fiiere n
ordine alfabetic, separate printr-un spaiu.
Exemplu :
Probleme rezolvate de programare Subiecte date la ATESTAT
4
se vor afia pe ecran urmtoarele nume:
Ana Angie Cora Dana Daniel Dora Ene Horia Mara Nae Oana Paul Paul Tibi.
Rezolvare:
Program ATESTAT_2008_3_INTERCLASARE ;
uses CRT;
CONST
nmax = 50;
TYPE
vect or = ar r ay [ 1. . nmax] of STRI NG;
vect or r ezul t ant = ar r ay [ 1. . 2*nmax] of STRI NG;
VAR
m, n, i , j , k : i nt eger ;
A, B : vect or ;
C : vect or r ezul t ant ;
X, Y, Z : TEXT;
l i ni e : STRI NG;
Begin { PROGRAM PRINCIPAL }
cl r scr ;
n : = 7;
ASSI GN ( X, ' C: \ X. t xt ' ) ;
ASSI GN ( Y, ' C: \ Y. t xt ' ) ;
ASSI GN ( Z, ' C: \ Z. t xt ' ) ; {Z = f i si er ul car e va r ezul t a pr i n
I NTERCLASARE}
r ewr i t e ( X) ;
r ewr i t e ( y) ;
A [ 1] : = ' Ana' ;
A [ 2] : = ' Dana' ;
A [ 3] : = ' Dani el ' ;
A [ 4] : = ' Ene' ;
A [ 5] : = ' Mar a' ;
A [ 6] : = ' Nae' ;
A [ 7] : = ' Paul ' ;
Dac fiierul X.TXT are coninutul:
Ana
Dana
Daniel
Ene
Mara
Nae
Paul
iar fiierul Y.TXT are coninutul:
Angie
Cora
Dora
Horia
Oana
Paul
Tibi
Probleme rezolvate de programare Subiecte date la ATESTAT
5
B [ 1] : = ' Angi e' ;
B [ 2] : = ' Cor a' ;
B [ 3] : = ' Dor a' ;
B [ 4] : = ' Hor i a' ;
B [ 5] : = ' Oana' ;
B [ 6] : = ' Paul ' ;
B [ 7] : = ' Ti bi ' ;
f or i : = 1 t o n do
begi n
wr i t e ( X, A[ i ] ) ;
wr i t el n ( X) ;
wr i t e ( Y, B [ i ] ) ;
wr i t el n ( Y) ;
end;
wr i t el n;
{OPTI ONAL, af i samcont i nut ur i l e cel or doua f i si er e}
wr i t el n ( ' Fi si er ul X est e: ' ) ;
RESET ( X) ;
whi l e not EOF ( X) do
begi n
READLN ( X, l i ni e) ;
wr i t el n ( l i ni e) ;
end;
wr i t el n;
wr i t el n ( ' Fi si er ul Y est e: ' ) ;
RESET ( Y) ;
whi l e not EOF ( Y) do
begi n
READLN ( Y, l i ni e) ;
wr i t el n ( l i ni e) ;
end;
{CI TI REA DI N FI SI ERELE TEXT }
RESET ( X) ;
i : =1;
Whi l e NOT EOF ( X) do
begi n
READLN ( X, l i ni e) ;
A [ i ] : = l i ni e;
i : = i + 1
end;
RESET ( Y) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
6
j : =1;
Whi l e NOT EOF ( Y) do
begi n
READLN ( Y, l i ni e) ;
B [ j ] : = l i ni e;
j : = j + 1
end;
k : = 0; {i ndex i n vect or ul r ezul t ant C}
i : = 1;
j : = 1;
WHI LE ( i <= n) AND ( j <= n) do
begi n
i f A [ i ] <= B [ j ] t hen
begi n
k : = k + 1;
C [ k] : = A [ i ] ;
i : = i + 1;
end
el se
begi n
k : = k + 1;
C [ k] : = B [ j ] ;
j : = j + 1;
end;
end;
WHI LE i <= mdo
begi n
k : = k + 1;
C [ k] : = A [ i ] ;
i : = i + 1
end;
WHI LE j <= n do
begi n
k : = k + 1;
C [ k] : = B [ j ] ;
j : = j + 1
end;
wr i t el n;
wr i t el n ( ' Vect or ul r ezul t at i n ur ma i nt er cl asar i i est e: ' ) ;
wr i t el n;
f or k : = 1 t o n + n do
begi n
wr i t el n ( ' C [ ' , k, ' ] = ' , C [ k] ) ;
end;
Probleme rezolvate de programare Subiecte date la ATESTAT
7
REWRITE (Z); {Gener ar e f i si er r ezul t ant }
f or k : = 1 t o n + n DO
begi n
WRI TE ( Z, C[ k] , ' ' ) ;
end;
wr i t el n;
wr i t el n ( ' Cont i nut ul f i si er ul r ezul t ant est e: ' ) ;
wr i t el n;
RESET (Z); {Pr egat i r e f i si er pent r u ci t i r e}
Whi l e not EOF ( Z) do
begi n
READ ( Z, l i ni e) ;
wr i t e ( l i ni e) ;
end;
r eadl n
end.
ATESTAT - 2008 - 4 (REPETA 2007 4) Scriei programul care citete de la
tastatur un numr natural n (n < 100) i un ir cu n numere ntregi din intervalul [100, 999].
Programul construiete un ir de numere rezultat prin nlocuirea fiecrui numr din irul citit
cu numrul obinut prin interschimbarea cifrei unitilor cu cifra sutelor. Numerele din noul
ir se vor afia pe ecran separate printr-un singur spaiu.
Exemplu: pentru n = 3 i irul 123, 904, 500, se afieaz 321, 409, 5.
Rezolvare:
Vom trata numerele introduse ca pe iruri de caractere.
Fiind necesar inversarea cifrei sutelor cu cifra unitilor, iar numrul avnd doar 3 cifre,
vom defini o funcie recursiv INVERS pentru inversarea numrului.
OBSERVAIE:
Problema se poate rezolva i cu ajutorul operatorilor DIV i MOD. Mai simplu este cu
STRING.
Program ATESTAT_2007_4_INVERSAREA_ELEMENTELOR_UNUI_SIR ;
VAR
n, i : i nt eger ;
si r , i nv : st r i ng;
Y : ar r ay [ 1. . 100] of st r i ng;
function INVERS ( s : st r i ng ) : st r i ng;
begi n
i f s = ' ' t hen
I NVERS : = ' '
Probleme rezolvate de programare Subiecte date la ATESTAT
8
el se
I NVERS : = s [ LENGTH ( s) ] + INVERS ( Copy ( s, 1,
LENGTH ( s) - 1 ) ) ;
end; { sf ar si t f unct i e }
Begin { PROGRAM PRINCIPAL }
wr i t e ( ' Dat i numar ul de numer e, n = ' ) ;
r eadl n ( n) ;
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i un nou numar cupr i ns i nt r e 100 si
999, x = ' ) ;
r eadl n ( si r ) ;
Y [ i ] : = I NVERS ( si r ) ;
wr i t el n ( si r , ' ' , I NVERS ( si r ) ) ;
end;
wr i t el n;
f or i : = 1 t o n do
wr i t e ( Y[ i ] , ' ' )
r eadl n
end.
ATESTAT - 2008 - 5 (SIMILAR 2007 5) Dou tablouri unidimensionale A i
B, cu elementele A1, A2, , An, respctiv B1, B2, , Bn sunt n relaia A < = B dac A1 <
= B1, A2 < = B2, , An < = Bn. Scriei un program care citete dou tablouri
unidimensionale A i B cu acelai numr de elemente de tip ntreg i verific dac A < = B
sau B < = A, afind un mesaj adecvat.
Rezolvare:
Program ATESTAT_2008_5_COMPARARE_VECTORI;
{ I n ur ma execut i ei subpr ogr amul ui VERI FI CA, vom avea val or i l e:
}
{ k1 = TRUE daca A <= B }
{ k2 = TRUE daca B <= A }
{ k1 = k2 = FALSE daca ni ci una di n cel e doua condi t i i nu est e
sat i sf acut a }
program COMPARARE_VECTORI;
uses CRT;
const
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of i nt eger ;
Probleme rezolvate de programare Subiecte date la ATESTAT
9
var
n, i : i nt eger ;
A, B : vect or ;
k1, k2, k : BOOLEAN ;
procedure CITESTEVECTOR (VAR X:vector; m : integer);
begi n
f or i : = 1 t o mdo
begi n
wr i t e ( ' Dat i el ement ul [ ' , i , ' ] = ' ) ;
r eadl n ( X [ i ] )
end;
end;
procedure VERIFICA (A, B: vector; n : integer; VAR K : BOOLEAN);
begi n
k : = TRUE ; {pr esupun i ndepl i ni t a cer i nt a pr obl emei }
f or i : = 1 t o n do
begi n
I F A [ i ] > B[ i ] t hen
k : = FALSE;
end;
end;
begi n
CLRSCR;
wr i t el n;
wr i t el n;
r epeat
wr i t e ( ' Dat i n = ' ) ;
r eadl n ( n)
unt i l ( n >= 1) and ( n <= nmax) ;
wr i t el n;
wr i t el n ( ' Dat i vect or ul A ' ) ;
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i A [ ' , i , ' ] = ' ) ;
r eadl n ( A [ i ] ) ;
end;
wr i t el n;
wr i t el n ( ' Dat i vect or ul B ' ) ;
f or i : = 1 t o n do
begi n
Probleme rezolvate de programare Subiecte date la ATESTAT
10
wr i t e ( ' Dat i B [ ' , i , ' ] = ' ) ;
r eadl n ( B [ i ] ) ;
end;
wr i t el n;
wr i t el n;
VERIFICA (A, B, n, K);
k1 : = k;
VERIFICA (B, A, n, K);
k2 : = k;
i f k1 = TRUE t hen
wr i t el n ( ' A <= B' )
el se
begi n
i f k2 = TRUE t hen
wr i t el n ( ' B < A' )
el se
wr i t el n ( ' Cer i nt el e pr obl emei nu sunt i ndepl i ni t e
de cel put i n o per eche de
el ement e' ) ;
end;
wr i t el n;
r eadl n
end.
ATESTAT - 2008 - 6 (REPETA 2007 06) Scriei subprogramul SORTARE
care primete, prin intermediul parametrului A, un tablou unidimensional cu 1000 de
componente reale i prin intermediul parametrilor i i j dou numere ntregi, 1 < = i < j < =
1000. Subprogramul realizeaz ordonarea cresctoare a elementelor A
i
, A
i+1,
, A
j
.
Rezolvare: Aplicm algoritmul bubble sort n procedura de ordonare.
Program ATESTAT_2008_6_SORTARE_PARTIALA_VECTOR ;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of i nt eger ;
VAR
A : vect or ;
n, i , j , k, aux : i nt eger ;
Probleme rezolvate de programare Subiecte date la ATESTAT
11
procedure ORDONEAZACRESCATOR ( VAR A : vect or ; pozi , pozj :
i nt eger ) ;
{pozi = pozi t i a i ni t i al a; pozj = pozi t i a f i nal a; }
{or donar ea se apl i ca vect or ul ui i nt r e pozi t i i l e pozi si pozj
}
VAR
k, aux, t est : i nt eger ;
begi n
r epeat
t est : = 0;
k : = pozi ;
r epeat
i f A [ k] > A [ k + 1] t hen
begi n
aux : = A[ k] ;
A [ k] : = A [ k+1] ;
A [ k + 1] : = aux;
t est : = 1;
end;
k : = k + 1
unt i l k > pozj - 1
unt i l t est = 0
end;
Begin { PROGRAM PRINCIPAL }
Wr i t el n ( ' Dat i numar ul de el ement e' ) ;
wr i t el n;
r epeat
wr i t e ( ' Dat i n = ' ) ;
r eadl n ( n)
unt i l ( n>=1) and ( n <=nmax) ;
wr i t el n;
wr i t el n ( ' Dat i vect or ul A' ) ;
wr i t el n;
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i a[ ' , i , ' ] = ' ) ;
r eadl n ( A [ i ] )
end;
wr i t el n;
wr i t e ( ' Dat i pozi t i a i = ' ) ;
r eadl n ( i ) ;
wr i t e ( ' Dat i pozi t i a j = ' ) ;
r eadl n ( j ) ;
ORDONEAZACRESCATOR ( A, i, j);
Probleme rezolvate de programare Subiecte date la ATESTAT
12
wr i t el n;
wr i t el n ( ' Par t ea sor t at a di n vect or ul A, i nt r e pozi t i i l e i
si j , est e: ' ) ;
f or k: = i t o j do
begi n
wr i t el n ( ' A [ ' , k, ' ] = ' , A [ k] ) ;
end;
r eadl n
end.
ATESTAT - 2008 - 7 REPETA 2007 7 Scriei programul care citete de la
tastatur elementele unui vector X cu 1000 de numere reale i care ordoneaz cresctor
termenii irului. Programul va afia pe ecran noile valori ale vectorului X separate ntre ele prin
spaiu.
Rezolvare: Aplicm algoritmul bubble sort n procedura de ordonare.
Program ATESTAT_2008_7_ORDONARE_VECTOR ;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of i nt eger ;
VAR
x : vect or ;
n, i : i nt eger ;
procedure ORDONEAZACRESCATOR ( VAR v : vect or ; m: i nt eger ) ;
VAR
j , aux, t est : i nt eger ;
begi n
r epeat
t est : = 0;
j : = 1;
r epeat
i f v [ j ] > v [ j + 1] t hen
begi n
aux : = v[ j ] ;
v [ j ] : = v [ j +1] ;
v [ j + 1] : = aux;
t est : = 1;
end;
j : = j + 1
unt i l j > m- 1
unt i l t est = 0
Probleme rezolvate de programare Subiecte date la ATESTAT
13
end;
Begin { PROGRAM PRINCIPAL }
r epeat
wr i t e ( ' Dat i n = ' ) ;
r eadl n ( n)
unt i l ( n>=1) and ( n <=nmax) ;
wr i t el n;
wr i t el n ( ' Dat i vect or ul X' ) ;
wr i t el n;
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i X[ ' , i , ' ] = ' ) ;
r eadl n ( x[ i ] )
end;
wr i t el n;
ORDONEAZACRESCATOR (X, n);
wr i t el n ( Rezul t at ul sor t ar i i est e: ) ;
f or i : = 1 t o n do
begi n
wr i t e ( X[ i ] , ' ' )
end;
r eadl n
end.
ATESTAT - 2008 - 8 SIMILAR 2007 8 ntr-un sistem de coordonate carteziene,
se consider punctele A (x1, y1) i B (x2, y2), unde coordonatele ntregi x1, y1, x2, y2
sunt, n aceast ordine, urmtorii termeni consecutivi ai irului Fibonacci : fn, fn+1, fn+2, fn+3
(n natural). Scriei un program care citete de la tastatur un numr natural n (1 < = n < =
20), determin i afieaz pe ecran distana dintre cele dou puncte A i B astfel determinate.
Distana dintre 2 puncte A (x1, y1) i B (x2, y2) se noteaz cu d .
Rezolvare:
Program ATESTAT_2007_8_DISTANTA_DINTRE_DOUA_PUNCTE ;
CONST
nmax = 24;
TYPE
vect or = ar r ay [ 1. . nmax] of LONGI NT;
VAR
n, i : i nt eger ;
x1, x2, y1, y2 : LONGI NT;
Probleme rezolvate de programare Subiecte date la ATESTAT
14
d : r eal ;
f : vect or ;
procedure FIBO (VAR f : vector; m : integer) ;
{ pr ocedur a FI BO gener eaza t er meni i si r ul ui l ui Fi bonacci }
{ si - i depune i n vect or ul f }
begi n
f [ 1] : = 1;
f [ 2] : = 1;
f or i : = 3 t o mdo
begi n
f [ i ] : = f [ i - 1] + f [ i - 2]
end;
end;
Begin { PROGRAM PRINCIPAL }
wr i t e ( ' Dat i n = ' ) ;
r eadl n ( n) ;
wr i t el n ;
FIBO (f, n + 3);
x1 : = f [ n] ;
x2 : = f [ n+1] ;
y1 : = f [ n+2] ;
y2 : = f [ n+3] ;
{Di st ant a di nt r e punct el e A si B se cal cul eaza cu r el at i a}
d := SQRT ( SQR (ABS (x1 - x2)) + SQR ( ABS (y1 - y2))) ;
wr i t el n ( ' Di st ant a di nt r e A si B est e = ' , d ) ;
r eadl n
end.
ATESTAT - 2008 - 9 REPETA 2007 9 ntr-o list simplu nlnuit alocat dinamic,
fiecare nod (element al listei) reine n cmpul DATA un numr natural cu cel mult nou
cifre i n cmpul URM, adresa elementului urmtor din list. Scriei subprogramul
DIVIZOR care, prin parametrul p, primete adresa primului element al listei descrise mai
sus, prin parametrul x, primete un numr natural cu cel mult nou cifre i care afieaz pe
ecran, cte unul pe linie, numerele din list care au exact un divizor comun cu x. Dac n list
nu exist un astfel de element atunci se va afia mesajul problema nu are soluie.
Rezolvare:
1 - Se determin numrul de elemente din list, prin TRAVERSAREA listei.
Probleme rezolvate de programare Subiecte date la ATESTAT
15
2 - Construim un tablou A, n care memorm toi divizorii lui X.
3 - Construim un tablou B, n care memorm toi divizorii unui numr Y din list.
4 - Numrm divizorii comuni din tablourile A i B.
5 - Numrm i numerele din list care au doar un divizor comun cu X, n afar de 1.
5 - Dac exist doar un singur divizor comun, n afar de 1, afim numrul curent Y din list.
6 - Dac nu gsim nici un numr n list, n conditiile problemei, afim mesajul: NU
EXIST SOLUIE.
Program
ATESTAT_2007_9_DIVIZORI_IN_LISTA_SIMPLU_INLANTUITA_ALOCATA_DINAM
IC ;
uses CRT;
l abel
10, 500;
TYPE
l i st a = ^nod;
nod = r ecor d
chei e: i nt eger ;
dat a: LONGI NT;
ur m: l i st a
end;
VECTOR = ar r ay [ 1. . 1000] of LONGI NT;
VAR
q, r , baza, p : l i st a;
nr di vi zor i , nr nodur i , n, i , j , k : i nt eger ;
X, Y, Z : LONGI NT;
dat a: LONGI NT;
c : char ;
A, B, V : VECTOR; {vect or i n car e se r et i n di vi zor i i gasi t i }
procedure CITDATA; { i nt r oducer e dat e ut i l e }
begi n
r eadl n ( dat a) ;
end;
procedure TRAVLISTA ( VAR nr nodur i : i nt eger ) ;
{ t r aver sar e l i st a i n or di nea i nt r oducer i i }
begi n
nr nodur i : = 0;
p: =baza;
whi l e p<>ni l do
begi n
wr i t el n( ' Nodul ' , p^. chei e, ' cont i ne nr . =
' , p^. dat a) ;
p: =p^. ur m;
nr nodur i : = nr nodur i + 1;
Probleme rezolvate de programare Subiecte date la ATESTAT
16
wr i t el n
end;
end;
procedure DETDIVIZORI ( Z : LONGI NT; VAR nr di vi zor i : i nt eger ; VAR
V : vect or ) ;
{ det er mi na di vi zor i i unui nr Z di n l i st a si - i scr i e i n
vect or ul V }
VAR
nr di v, di vi z : LONGI NT;
begi n
k : = 0; {i ndex i n vect or ul V - vect or cu di vi zor i i nr . Z
}
nr di v : = 0;
f or di vi z : = 2 t o Z do
begi n
i f Z MOD di vi z = 0 t hen
begi n
nr di v : = nr di v + 1;
k : = k + 1;
V [ k ] : = di vi z;
end;
end;
wr i t el n;
nrdivizori := nrdiv;
wr i t el n ( ' Di vi zor i i l ui ' , Z, ' , i n af ar a de 1, sunt : ' ) ;
f or i : = 1 t o k do
begi n
wr i t el n ( ' Di vi zor ul [ ' , i , ' ] = ' , V [ i ] )
end;
wr i t el n;
end;
procedure INSEREAZAPRIMUL ; {i nt r oduce pr i mul el ement - l a
i ncep. l i st ei }
begi n
baza: =ni l ;
new( p) ;
wr i t el n;
p^. chei e : = 1;
p^. ur m: = baza;
baza: =p;
wr i t e ( ' Dat i pr i mul numar al l i st ei , nr . = ' ) ;
CITDATA;
p^. dat a: =dat a
Probleme rezolvate de programare Subiecte date la ATESTAT
17
end;
procedure INSEREAZAREST ; { i nt r oduce cel el al t e el em. - l a
sf i r s. l i st ei }
begi n
new ( q) ;
q^. ur m: =ni l ;
p^. ur m: =q;
i f j <=n t hen
begi n
q^. chei e : = j ;
wr i t e ( ' Dat i al ' , j , ' - l ea numar al l i st ei , nr = ' ) ;
CITDATA;
q^. dat a: =dat a;
p: =q
end
el se
begi n
q^. chei e: =N+1;
{ q^. dat a: =' ACEST NOD NU SE VA STERGE SI NU SE ADAUGA
NI MI C DUPA EL' ; }
p: =q
end
end;
procedure CREARELISTA ;
begi n
INSEREAZAPRIMUL;
wr i t el n;
i f n > 1 t hen
begi n
f or j : =2 t o n do
begi n
INSEREAZAREST;
wr i t el n
end
end;
end;
procedure DIVIZOR (P : LISTA; X : LONGINT; VAR V : vector );
VAR
di vi zor i comuni , numer e, di vi z, nr di v : i nt eger ;
nr di vX, nr di vY : i nt eger ; {nr . de di vi zor i ai l ui X,
r espect i v Y }
begi n
wr i t el n( ' LI STA ESTE : ' ) ;
TRAVLISTA (nrnoduri);
wr i t el n ( ' Li st a ar e ' , nr nodur i , ' nodur i ' ) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
18
wr i t el n;
wr i t e ( ' Dat i numar ul X = ' ) ;
r eadl n ( x) ;
wr i t el n;
DETDIVIZORI (X, nrdivizori, A);
nr di vX : = nr di vi zor i ;
wr i t el n;
wr i t el n ( ' Numar ul X = ' , X, ' ar e ' , nr di vX, ' di vi zor i ' ) ;
wr i t el n;
numer e : = 0 ; {cont or ul " numer e" numar a numer el e di n
l i st a }
{ce sat i sf ac condi t i i l e pr obl emei }
p: =baza;
whi l e p<>ni l do
begi n
Y : = p^. dat a;
DETDIVIZORI (Y, nrdivizori, B);
nr di vY : = nr di vi zor i ;
wr i t el n ( ' Nr . Y = ' , Y, ' ar e ' , nr di vY, ' di vi zor i ,
i n af ar a de 1' ) ;
wr i t el n ( ' Apasat i ENTER' ) ;
r eadl n;
CLRSCR;
di vi zor i comuni : = 0; {numar a di vi zor i i comuni ai
l ui X si Y }
f or i : = 1 t o nr di vX do
begi n
f or j : = 1 t o nr di vY do
begi n
i f A [ i ] = B [ j ] t hen
begi n
wr i t el n ( ' Di vi zor comun al numer el or
' , X, ' si ' , Y, '
= ' , A[i]) ;
di vi zor i comuni : = di vi zor i comuni + 1;
end;
end;
end;
i f di vi zor i comuni = 1 t hen
begi n
numer e : = numer e + 1;
V [ numer e] : = B[ j ] ;
wr i t el n;
wr i t el n ( ' El ement ul Y = ' , Y, ' ar e ' ,
Probleme rezolvate de programare Subiecte date la ATESTAT
19
di vi zor i comuni , ' di vi zor comun
cu X = ' , X) ;
end;
p:=p^.urm;
wr i t el n
end; {sf ar si t Whi l e - t r aver sar e l i st a si
ver i f i car e}
wr i t el n;
wr i t el n ( ' Apasat i ENTER' ) ;
r eadl n;
i f numer e = 0 t hen
wr i t el n ( ' NU EXI STA SOLUTI E' )
el se
begi n
CLRSCR;
f or k : = 1 t o numer e do
wr i t el n ( ' Numar ul ' , V [ k] , ' ar e un
si ngur di vi zor comun
cu ' , X, ' , i n af ar a de 1' ) ;
end;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t el n;
wr i t el n ( ' SUGESTI E : ' ) ;
wr i t el n;
wr i t el n ( ' Pent r u si mpl i t at e, r ul at i pr ogr amul cu val or i l e
ur mat oar e: ' ) ;
wr i t el n;
wr i t el n ( ' Li st a va avea un numar de nodur i , n = 3' ) ;
wr i t el n;
wr i t el n ( ' Nodur i l e vor cont i ne val or i l e 111, 222, 333' ) ;
wr i t el n;
wr i t el n ( ' Numar ul X = 12' ) ;
wr i t el n;
wr i t el n ( ' Desi gur , pr ogr amul r ul eaza cor ect si cu al t e
val or i . ' ) ;
wr i t el n ( ' Nu va compl i cat i cu un n > 10. ' ) ;
wr i t el n ( ' Dur eaza mul t , ca sa const at at i acel asi l ucr u ca si
cu n = 3' ) ;
wr i t el n;
wr i t el n ( ' Apasat i o t ast a' ) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
20
r eadl n;
CLRSCR;
wr i t e ( ' Dat i numar ul de nodur i , N = ' ) ;
r eadl n( n) ;
wr i t el n;
i f n = 0 t hen
begi n
wr i t el n ( ' LI STA VI DA' ) ;
got o 500
end
el se
CREARELISTA ;
wr i t el n;
DIVIZOR (p, X, V);
500: r eadl n
end.
ATESTAT - 2008 - 10 REPETA 2007 10 Fiierele text F1.txt i F2.txt conin,
fiecare, elementele ale cte unui ir de numere reale A, respective B. Fiierele conin numere
distincte scrise pe o linie i separate printr-un singur spaiu. Scriei un program care citete cele
2 iruri de numere din fiierele F1.txt i F2.txt i care scrie n fiierul F3.txt toate elementele
comune irurilor A i B pe o linie i separate printr-un spaiu.
Exemplu : Dac fiierele F1.txt i F2.txt au coninuturile :
F1.txt : 10.3, 2.05, 5, 7.12, respectiv
F2.txt : 67, 7.12, 5, 3, 7.33, 9, atunci
F3.txt va conine: 7.12, 5.
Rezolvare:
Program ATESTAT_2008_10_FISIERE_TEXT_CU_ELEMENTE_COMUNE;
uses CRT;
CONST
nmax = 50;
TYPE
vect or = ar r ay [ 1. . nmax] of r eal ;
VAR
f 1, f 2, f 3 : TEXT;
ch : CHAR;
poz, k, m, n, i , j , ER : i nt eger ; {ER = var i abi l a EROARE i n
pr ocedur a VAL}
si r , l i ni e1, l i ni e2, l i ni e3 : STRI NG;
A, B, C : vect or ;
nr : r eal ;
Begin { PROGRAM PRINCIPAL }
Probleme rezolvate de programare Subiecte date la ATESTAT
21
CLRSCR;
ASSI GN ( f 1, ' C: \ f 1. t xt ' ) ;
ASSI GN ( f 2, ' C: \ f 2. t xt ' ) ;
ASSI GN ( f 3, ' C: \ f 3. t xt ' ) ;
r ewr i t e ( f 1) ;
r ewr i t e ( f 2) ;
r ewr i t e ( f 3) ;
l i ni e1 : = ' 10. 3 2. 05 5 7. 12' ;
l i ni e2 : = ' 67 7. 12 5 3 7. 33 9' ;
wr i t e ( f 1, l i ni e1) ;
wr i t e ( f 2, l i ni e2) ;
RESET ( f 1) ;
RESET ( f 2) ;
READ ( f 1, l i ni e1) ;
READ ( f 2, l i ni e2) ;
i : = 0; {i ndex i n vect or ul A }
si r : = ' ' ; {i ni t i al i zar e cu si r vi d }
poz : = 1;
whi l e poz < LENGTH ( l i ni e1) do
begi n
I F l i ni e1 [ poz] <> ' ' t hen
si r : = si r + l i ni e1 [ poz]
ELSE
begi n
i : = i + 1;
VAL ( si r , nr , ER) ;
A [ i ] : = nr ;
si r : = ' ' ;
wr i t el n ( ' A [ ' , i , ' ] = ' , A [ i ] : 10 :
2 ) ;
end;
poz : = poz + 1
end;
si r : = si r + l i ni e1 [ poz] ;
i : = i + 1;
VAL ( si r , nr , ER) ;
A [ i ] : = nr ;
m: = i ;
wr i t el n ( ' A [ ' , i , ' ] = ' , A [ i ] : 10 : 2 ) ;
wr i t el n;
wr i t el n ( ' Vect or ul A ar e m= ' , m, ' el ement e' ) ;
wr i t el n;
j : = 0; {i ndex i n vect or ul B }
si r : = ' ' ; {i ni t i al i zar e cu si r vi d }
poz : = 1;
Probleme rezolvate de programare Subiecte date la ATESTAT
22
whi l e poz < LENGTH ( l i ni e2) do
begi n
I F l i ni e2 [ poz] <> ' ' t hen
si r : = si r + l i ni e2 [ poz]
ELSE
begi n
j : = j + 1;
VAL (sir, nr, ER);
B [ j ] : = nr ;
si r : = ' ' ;
wr i t el n ( ' B [ ' , j , ' ] = ' , B [ j ] : 10 :
2 ) ;
end;
poz : = poz + 1
end;
si r : = si r + l i ni e2 [ poz] ;
j : = j + 1;
VAL (sir, nr, ER);
B [ j ] : = nr ;
n : = j ;
wr i t el n ( ' B [ ' , j , ' ] = ' , B [ j ] : 10 : 2 ) ;
wr i t el n;
wr i t el n ( ' Vect or ul B ar e n = ' , n, ' el ement e' ) ;
wr i t el n;
{VERI FI CARE ELEMENTE COMUNE SI SCRI EREA LOR I N F3. t xt }
k : = 0; {i ndex i n vect or ul C };
f or i : = 1 t o mdo
begi n
f or j : = 1 t o n do
begi n
i f A[ i ] = B [ j ] t hen
begi n
k : = k + 1;
C [ k] : = A [ i ] ;
wr i t el n;
wr i t el n ( ' C [ ' , k, ' ] =' , C [ k] :
10 : 2 ) ;
end
end
end;
f or i : = 1 t o k do
begi n
STR ( C[i], sir ); {t r ansf or mamC [ i ] i n si r }
wr i t e ( f 3, si r + ' ' ) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
23
end;
RESET ( f 3) ;
wr i t el n;
READ ( f 3, l i ni e3) ;
wr i t el n;
wr i t el n ( ' Fi si er ul f 3. t xt cont i ne: ' , l i ni e3) ;
r eadl n
end.
ATESTAT - 2008 - 11 ( REPETA 2007 11) Scriei un program care afieaz toate
numerele naturale formate din cifre identice, mai mari dect 10 i mai mici dect o valoare dat
n, n < = 2.000.000.000.
Exemplu: pentru n = 195, se afieaz : 11, 22, 33, 44, 55, 66, 77, 88, 99, 111.
Rezolvare:
1 - Determinm numrul de cifre ale numrului n.
2 - Folosim un vector V cu un numr de elemente = n.
Program
ATESTAT_2008_11_NUMERE_NATURALE_FORMATE_DIN_CIFRE_IDENTICE ;
uses CRT;
VAR
n : LONGI NT;
procedure ZECI (nr : LONGINT);
begi n
nr : = 11;
whi l e ( nr <= n) AND ( nr <= 100) do
begi n
wr i t e ( nr , ' ' ) ;
nr : = nr + 11;
end;
end;
procedure SUTE (nr : LONGINT);
begi n
nr : = 111;
whi l e ( nr <= n) AND ( nr <= 1000) do
begi n
wr i t e ( nr , ' ' ) ;
nr : = nr + 111;
end;
end;
procedure MII (nr : LONGINT);
begi n
nr : = 1111;
whi l e ( nr <= n) AND ( nr <= 10000) do
begi n
Probleme rezolvate de programare Subiecte date la ATESTAT
24
wr i t e ( nr , ' ' ) ;
nr : = nr + 1111;
end;
end;
procedure ZECIDEMII (nr : LONGINT);
begi n
nr : = 11111;
whi l e ( nr <= n) AND ( nr <= 100000) do
begi n
wr i t e ( nr , ' ' ) ;
nr : = nr + 11111;
end;
end;
procedure SUTEDEMII (nr : LONGINT);
begi n
nr : = 111111;
whi l e ( nr <= n) AND ( nr <= 1000000) do
begi n
wr i t e ( nr , ' ' ) ;
nr : = nr + 111111;
end;
end;
procedure MILIOANE (nr : LONGINT);
begi n
nr : = 1111111;
whi l e ( nr <= n) AND ( nr <= 10000000) do
begi n
wr i t e ( nr , ' ' ) ;
nr : = nr + 1111111;
end;
end;
procedure ZECIDEMILIOANE (nr : LONGINT);
begi n
nr : = 11111111;
whi l e ( nr <= n) AND ( nr <= 100000000) do
begi n
wr i t e ( nr , ' ' ) ;
nr : = nr + 11111111;
end;
end;
procedure SUTEDEMILIOANE (nr : LONGINT);
begi n
nr : = 111111111;
whi l e ( nr <= n) AND ( nr <= 1000000000) do
begi n
wr i t e ( nr , ' ' ) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
25
nr : = nr + 111111111;
end;
end;
procedure MILIARDE (nr : LONGINT);
begi n
nr : = 1111111111;
whi l e ( nr <= n) AND ( nr <= 1000000000) do
begi n
wr i t e ( nr , ' ' ) ;
nr : = nr + 1111111111;
end;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i n = ' ) ; r eadl n ( n) ;
wr i t el n;
i f ( n > 10) and ( n <= 100) t hen
begi n
ZECI ( n)
end;
i f ( n > 100) and ( n <= 1000) t hen
begi n
ZECI ( n) ;
wr i t el n;
SUTE ( n) ;
end;
i f ( n > 1000) and ( n <= 10000) t hen
begi n
ZECI ( n) ;
wr i t el n;
SUTE ( n) ;
wr i t el n;
MI I ( n) ;
end;
i f ( n > 10000) and ( n <= 100000) t hen
begi n
ZECI ( n) ;
wr i t el n;
SUTE ( n) ;
wr i t el n;
MI I ( n) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
26
wr i t el n;
ZECI DEMI I ( n) ;
end;
i f ( n > 100000) and ( n <= 1000000) t hen
begi n
ZECI ( n) ;
wr i t el n;
SUTE ( n) ;
wr i t el n;
MI I ( n) ;
wr i t el n;
ZECI DEMI I ( n) ;
wr i t el n;
SUTEDEMI I ( n) ;
end;
i f ( n > 1000000) and ( n <= 10000000) t hen
begi n
ZECI ( n) ;
wr i t el n;
SUTE ( n) ;
wr i t el n;
MI I ( n) ;
wr i t el n;
ZECI DEMI I ( n) ;
wr i t el n;
SUTEDEMI I ( n) ;
wr i t el n;
MI LI OANE ( n) ;
end;
i f ( n > 10000000) and ( n <= 100000000) t hen
begi n
ZECI ( n) ;
wr i t el n;
SUTE ( n) ;
wr i t el n;
MI I ( n) ;
wr i t el n;
ZECI DEMI I ( n) ;
wr i t el n;
SUTEDEMI I ( n) ;
wr i t el n;
MI LI OANE ( n) ;
wr i t el n;
ZECI DEMI LI OANE ( n) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
27
end;
i f ( n > 100000000) and ( n <= 1000000000) t hen
begi n
ZECI ( n) ;
wr i t el n;
SUTE ( n) ;
wr i t el n;
MI I ( n) ;
wr i t el n;
ZECI DEMI I ( n) ;
wr i t el n;
SUTEDEMI I ( n) ;
wr i t el n;
MI LI OANE ( n) ;
wr i t el n;
ZECI DEMI LI OANE ( n) ;
wr i t el n;
SUTEDEMI LI OANE ( n) ;
end;
i f ( n > 1000000000) and ( n <= 2000000000) t hen
begi n
ZECI ( n) ;
wr i t el n;
SUTE ( n) ;
wr i t el n;
MI I ( n) ;
wr i t el n;
ZECI DEMI I ( n) ;
wr i t el n;
SUTEDEMI I ( n) ;
wr i t el n;
MI LI OANE ( n) ;
wr i t el n;
ZECI DEMI LI OANE ( n) ;
wr i t el n;
SUTEDEMI LI OANE ( n) ;
wr i t el n;
MI LI ARDE ( n) ;
end;
r eadl n
end.
ATESTAT - 2008 12 REPETA 2007 - 12 Prin asocierea fiecrei litere mici din
alfabetul englez cu un numr egal cu poziia cifrei n alfabet, se poate codifica orice secven de
Probleme rezolvate de programare Subiecte date la ATESTAT
28
litere mici cu o secven de numere. Scriei un program care citete din fiierul text COD.TXT
un numr "n" (0 < n < 30000) i, de pe rndul urmtor, o secven de n litere mici.
Programul va codifica secvena de caractere citit i va scrie pe ecran numerele asociate,
separate prin spaii.
Exemplu: pentru fiierul COD.TXT : 7
bacinfo
se vor afia pe ecran, n ordine, numerele :
2 1 3 9 14 6 15
Rezolvare:
Stiind c literele mici au codurile ASCII ncepnd cu 97,
a =97, b =98, c =99 ...
vom asocia noi coduri literelor mici astfel:
codul lui a = 97 - 96, adic ORD ('a') - 96
codul lui b = 98 - 96, adic ORD ('b') - 96
..............................................
codul lui z =ORD ('z') - 96
Program ATESTAT_2008_12_CODURI_ASCII ;
uses CRT;
CONST
nmax = 100;
VAR
c, l i t er ami ca : CHAR;
n, ER, nr , i : i nt eger ;
COD : ar r ay [ ' a' . . ' z' ] of 1. . 30;
f : t ext ;
l i ni e1, l i ni e2, si r , si r ci t i t : STRI NG;
Begin { PROGRAM PRINCIPAL }
ASSI GN ( f , ' C: \ COD. t xt ' ) ;
REWRI TE ( f ) ;
{- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - }
{CODI FI CARE LI TERE MI CI }
i : = 0;
f or l i t er ami ca : = ' a' t o ' z' do
begi n
i : = i + 1;
COD [ l i t er ami ca ] : = ORD ( l i t er ami ca) - 96;
end;
{- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - }
CLRSCR;
wr i t e ( ' Dat i un numar de l i t er e, n = ' ) ;
r eadl n ( n) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
29
STR (n, sir); { t r ansf or mnr . n i n si r pt . a- l scr i e i n
f i si er ul COD. t xt }
l i ni e1 : = si r ;
wr i t el n ( f , l i ni e1) ;
wr i t el n;
wr i t el n ( ' Scr i et i un si r de car act er e cu l ungi mea mai mi ca
de 255' ) ;
wr i t e ( ' AI CI i nt r oducet i t ext ul = ' ) ;
r eadl n ( l i ni e2) ;
wr i t el n ( f , l i ni e2) ; { scr i emt ext ul i n f i si er ul COD. t xt }
RESET ( f ) ;
r eadl n ( f , l i ni e1) ; {l i ni e1 cont i ne doar car act er e numer i ce
}
VAL (linie1, nr, ER); { t r ansf or ml i ni e1 i n numar }
wr i t el n;
si r ci t i t : = ' ' ; {i ni t i al i zar e si r ci t i t di n f i si er cu
si r ul vi d}
f or i : = 1 t o nr do
begi n
r ead ( f , c) ;
si r ci t i t : = si r ci t i t + c;
end;
wr i t el n ( ' si r ci t i t = ' , si r ci t i t ) ;
wr i t el n;
wr i t el n ( ' Si r ul ci t i t codi f i cat numer i c est e: ' ) ;
f or i : = 1 t o LENGTH ( si r ci t i t ) do
begi n
wr i t e ( COD [ si r ci t i t [ i ] ] , ' ' )
end;
r eadl n
end.
ATESTAT - 2008 - 13 REPETA 20076 13 Scriei un program care citete de la
tastatur trei numere naturale x, y i k, (1 < x < y < 2.000.000, k < 1.000) i afieaz pe ecran k
numere prime din intervalul [x, y]. Dac nu exist k numere prime n intervalul [x, y], se vor
afia toate numerele prime gsite, iar pe linia urmtoare se va afia mesajul S-au gsit mai
puine numere prime: , urmat de numrul acestora.
Probleme rezolvate de programare Subiecte date la ATESTAT
30
Exemplu: pentru x = 3, y = 12 i k = 5 se vor afia pe ecran:
3 5 7 11
s-au gsit mai puine numere prime: 4
Rezolvare:
Program ATESTAT_2008_13_NUMERE_PRIME;
uses CRT;
VAR
x, y, k, i , j : LONGI NT ;
nr pr i me : LONGI NT; {cont or car e numar a numer el e pr i me gasi t e
}
PRI ME : ar r ay [ 1. . 10000] of LONGI NT; {vect or cu numer el e
pr i me gasi t e}
nr : LONGI NT; {un numar oar ecar e di n i nt er val ul [ X, y] }
function PRIM (z : LONGINT) : BOOLEAN;
VAR
B : bool ean;
di vi zor , nr di v : LONGI NT;
begi n
nr di v : = 0;
di vi zor : = 1;
f or di vi zor : = 1 t o Z do
begi n
i f Z MOD di vi zor = 0 t hen
nr di v : = nr di v + 1;
end;
i f nr di v <= 2 t hen
B : = TRUE
el se
B : = FALSE;
PRI M : = B
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t el n ( ' Dat i i nt er val ul [ x, y] i n car e se caut a k numer e
pr i me' ) ;
wr i t el n;
wr i t e ( ' Dat i x = ' ) ;
r eadl n ( x) ;
wr i t el n;
wr i t e ( ' Dat i y = ' ) ;
r eadl n ( y) ;
wr i t el n;
wr i t e ( ' Pr eci zat i cat e numer e pr i me caut at i , k = ' ) ;
r eadl n ( k) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
31
i := 0; {i ndex i n vect or ul de numer e pr i me}
nrprime := 0; {nr pr i me = cont or pent r u numer el e pr i me
gasi t e}
f or nr : = y DOWNTO x do {Se cer e af i sar ea cel or MAI MARI
numer e pr i me}
{I ncepemcaut ar ea de l a y spr e x}
begi n
i f PRI M ( nr ) = TRUE t hen
begi n
i : = i + 1;
PRI ME [ i ] : = nr ;
nr pr i me : = nr pr i me + 1;
end;
end;
wr i t el n;
i f nr pr i me >= k t hen
begi n
wr i t el n ( ' S- au gasi t ' , k, ' numer e pr i me' ) ;
wr i t el n;
wr i t el n ( ' Cel e ' , k, ' numer e pr i me sunt : ' ) ;
f or i : = 1 t o k do
wr i t el n ( ' Numar ul pr i m' , i , ' est e = ' , PRI ME [ i ]
) ;
end
el se
begi n
i f ( nr pr i me < k) AND ( nr pr i me > 0) t hen
begi n
wr i t el n ( ' Nu s- au gasi t ' , k, ' numer e pr i me, ci
doar ' ,
nr pr i me, ' numer e pr i me' ) ;
wr i t el n ( ' Acest e numer e pr i me sunt : ' ) ;
f or i : = nr pr i me DOWNTO 1 do {l e af i sami n or di ne
CRESCATOARE}
wr i t e ( PRI ME [ i ] , ' ' )
end
el se
wr i t el n ( ' NU s- au gasi t numer e pr i me i n i nt er val ul
[ ' , x, ' , ' , y, ' ] ' ) ;
end;
r eadl n
end.
Probleme rezolvate de programare Subiecte date la ATESTAT
32
ATESTAT - 2008 - 14 REPETA 2007 - 14 Scriei un program care citete un numr
natural "n" nenul i par, n < 100 i apoi n numere naturale de cel mult 4 cifre fiecare i
determin cea mai mare sum care poate fi obinut adunnd numai o jumtate din toate
numerele naturale citite. Rezultatul se va afia pe ecran.
Exemplu: pentru n = 6 i numerele 728, 10, 103, 44, 1000, 94 se va afia:
1731 (reprezentnd suma : 728 +103 +1000).
Rezolvare:
1 - Introducem cele n numere, pe care le reinem n vectorul X.
2 - Ordonm descresctor vectorul X.
3 - Facem suma primelor n / 2 numere X [i].
Program ATESTAT_2008_14_SUMA_NUMERE;
uses CRT;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of i nt eger ;
VAR
n, i , j : i nt eger ;
SUMA : i nt eger ;
X : vect or ;
procedure ORDONEAZA (VAR X : vector); {or donar e dupa al gor i t mul
bubble sort}
VAR
aux, k : i nt eger ;
begi n
REPEAT
k : = 0;
i : = 1;
REPEAT
i f X [ i ] < X [ i + 1] t hen
begi n
aux : = X [ i ] ;
X [ i ] : = X [ i +1] ;
X [ i +1] : = aux;
k : = 1
end;
i : = i + 1
unt i l ( i > n- 1)
unt i l ( k = 0) ;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i numar ul de numer e, n = ' ) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
33
r eadl n ( n) ;
wr i t el n;
wr i t el n ( ' I nt r oducet i numer el e i nt r egi mai mi ci ca
30. 000' ) ;
wr i t el n;
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i X [ ' , i , ' ] = ' ) ;
r eadl n ( X [ i ] ) ;
end;
ORDONEAZA ( X );
SUMA : = 0;
f or i : = 1 t o n DI V 2 do {se aduna doar j umat at e di n
numer e}
begi n
SUMA : = SUMA + X [ i ] ;
end;
wr i t el n;
wr i t el n ( ' Suma maxi ma ce se poat e obt i ne di n numer el e
ci t i t e = ' , SUMA) ;
wr i t el n;
wr i t el n ( ' Ea r ezul t a di n i nsumar ea numer el or ' ) ;
f or i : = 1 t o n DIV 2 do
begi n
wr i t el n ( X [ i ] ) ;
end;
r eadl n;
end.
ATESTAT - 2008 - 15 REPETA 2007 - 15 Scriei un program care citete din
fiierul text BAC.TXT, cel mult 100 de numere naturale aflate pe o singur linie, formate din
cel mult 9 cifre fiecare, separate prin spaii i dintre aceastea le afieaz pe ecran doar pe acelea
care au proprietatea de a fi palindrom. Dac nu se gsesc numere palindrom, se va afia pe
ecran valoarea 1. Un numr are proprietatea de a fi palindrom dac citit de la dreapta la stnga
sau de la stnga la dreapta are aceeai valoare. De exemplu, 1221 este palindrom, n timp ce
1210 nu este palindrom.
Exemplu : dac din fiierul BAC.TXT se citesc numerele : 7341, 8228, 660, 2, 80, 131, atunci
pe ecran se vor afia : 8228, 2, 131.
Rezolvare:
Program ATESTAT_2008_15_PALINDROAME_IN_FISIER_TEXT;
Probleme rezolvate de programare Subiecte date la ATESTAT
34
uses CRT;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of LONGI NT;
VAR
nr , n, i , t : LONGI NT;
f : t ext ;
si r , l i ni e : STRI NG;
EROARE : i nt eger ; {var i abi l a EROARE di n apel ul pr ocedur i i
VAL}
A : vect or ;
pal i ndr oame : i nt eger ; {cont or car e numar a pal i ndr oamel e
gasi t e}
function NRCIFRE ( nr : LONGINT) : integer;
VAR
m: i nt eger ; {cont or pt . nr . ci f r e}
begi n
i f nr = 0 t hen
m: = 1
el se
begi n
m: = 0;
whi l e nr <> 0 do
begi n
nr : = nr DI V 10;
m: = m+ 1
end
end;
NRCI FRE : = m
end;
function PALINDROM (nr : LONGINT) : BOOLEAN;
VAR
nr ci f : i nt eger ;
x1 : LONGI NT;
k : BOOLEAN;
y : vect or ; {y = vect or i n car e scr i emci f r el e numar ul ui
nr }
begi n
nr ci f : = NRCIFRE ( nr );
x1 : = nr ;
f or i : = 1 t o nr ci f do
begi n
Probleme rezolvate de programare Subiecte date la ATESTAT
35
y [ i ] : = x1 MOD 10;
x1 : = x1 DI V 10;
end;
K : = TRUE;
f or i : = 1 t o nr ci f DI V 2 do
begi n
i f y[ i ] <> y [ nr ci f - i + 1] t hen {ver i f i cam
SI METRI A}
k : = FALSE
end;
PALI NDROM : = K;
end;
procedure EXTRAGENUMEREDINFISIERTEXT (VAR f: text; VAR A :
vector);
VAR
poz, k : i nt eger ;
numar : LONGI NT;
begi n
RESET ( f ) ;
r ead ( f , l i ni e) ;
i : = 0; { i = i ndex vect or A }
si r : = ' ' ; {i ni t i al i zar e cu si r ul vi d}
{ I n " si r " se ci t est e cat e un numar di n
l i ni e}
poz : = 1;
whi l e poz < LENGTH ( l i ni e) do
begi n
k: = poz;
whi l e l i ni e [ k ] <> ' ' do
begi n
si r : = si r + l i ni e [ k ] ;
{concat enar e}
k : = k + 1;
end;
poz : = k;
i : = i + 1;
VAL (sir, numar, EROARE); {transforma sir
in numar }
A [ i ] : = numar ;
si r : =' ' ;
poz : = poz + 1;
end; {sf ar si t WHI LE ext er i or }
Probleme rezolvate de programare Subiecte date la ATESTAT
36
end; {sf ar si t pr ocedur a}
Begin { PROGRAM PRINCIPAL }
CLRSCR;
ASSI GN ( f , ' C: \ BAC. t xt ' ) ;
r ewr i t e ( f ) ; {gener amf i si er ul t ext }
wr i t el n ( ' Scr i et i , i n BAC. TXT, n numer e nat ur al e cu cel
mul t 9 ci f r e' ) ;
wr i t el n;
wr i t e ( ' Cat e numer e dor i t i sa i nt r oducet i , n = ' ) ;
r eadl n ( n) ;
wr i t el n;
l i ni e : = ' ' ; {i ni t i al i zar e var i abi l a " l i ni e" cu si r ul vi d}
{" l i ni e" = un STRI NG car e se va i nt r oduce i n
f i si er ul t ext }
{" l i ni e" va cont i ne cel e n numer e separ at e de
spat i i }
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i un numar nat ur al , nr = ' ) ;
r eadl n ( nr ) ;
STR (nr, sir); {t r ansf or ma nr i n sir, pent r u a- l
memor a i n f i si er }
l i ni e : = l i ni e + si r + ' ' ; {const r ui m" l i ni e" pr i n
concat enar e, }
end; {scr i i nd un " spat i u" dupa
f i ecar e si r }
wr i t el n ( f , l i ni e) ; {scr i em" l i ni e" i n f i si er ul t ext }
wr i t el n;
wr i t el n ( ' Fi si er ul t ext ar e cont i nut ul : ' ) ;
RESET ( f ) ;
READLN ( f , l i ni e) ;
wr i t el n ( linie) ;
wr i t el n;
{ Di n STRI NG- ul " linie" trebuie s extragem numerele }
EXTRAGENUMEREDINFISIERTEXT ( f, A );
wr i t el n;
wr i t el n ( ' Vect or ul A, r ezul t at dupa ext r ager e est e: ' ) ;
f or i : = 1 t o n do
begi n
wr i t el n ( ' A [ ' , i , ' ] = ' , A[ i ] ) ;
end;
Probleme rezolvate de programare Subiecte date la ATESTAT
37
wr i t el n;
palindroame := 0;
f or t : = 1 t o n do
begi n
i f PALINDROM ( A [t] ) = TRUE t hen
begi n
pal i ndr oame : = pal i ndr oame + 1;
wr i t el n ( ' A [ ' , t , ' ] = ' , A [ t ] , ' est e
PALI NDROM' ) ;
wr i t el n;
end
el se
begi n
wr i t el n ( ' A [ ' , t , ' ] = ' , A [ t ] , ' NU est e
PALI NDROM' ) ;
wr i t el n;
end;
end; {sf ar si t FOR}
wr i t el n;
wr i t el n ( ' Apasat i ENTER' ) ;
r eadl n;
CLRSCR;
i f pal i ndr oame = 0 t hen
begi n
wr i t el n ( ' Nu exi st a pal i ndr oame i n f i si er ul t ext ' ) ;
wr i t el n ( ' Rezul t at ul caut ar i i = - 1' ) ;
end
el se
begi n
f or t : = 1 t o n do
begi n
i f PALI NDROM ( A [ t ] ) = TRUE t hen
begi n
wr i t el n ( ' A [ ' , t , ' ] = ' , A [ t ] , '
est e PALI NDROM' )
end
end;
end;
r eadl n
end.
ATESTAT - 2008 - 16 REPETA 2007 - 16 Scriei un program care citete un numr
natural nenul n (n < 100) i un ir de n numere naturale nenule de cel mult 4 cifre fiecare, i
care afieaz pe ecran irul ordonat cresctor n funcie de suma cifrelor corespunztoare
Probleme rezolvate de programare Subiecte date la ATESTAT
38
fiecrui termen al su. Dac dou numere au aceeai sum a cifrelor, se va afia cel mai mic
dintre ele.
Exemplu: pentru n = 5 i numerele 701, 1000, 49, 99, 143, se va afia irul 1000 44 143
701 99.
Rezolvare:
1 - Introducem cele n numere, pe care le reinem n vectorul X.
2 - Pentru fiecare numr X [i], determinm numrul de cifre.
3 - Pentru fiecare numr calculm suma cifrelor i o depunem n vectorul S.
Evident, unui numr X[i] i corespunde un S[i].
4 - Ordonm vectorul S descresctor i, simultan, rearanjm numerele X[i], astfel ca unui S [i]
s-i
corespund acelai X [i].
5 - Facem suma primelor n / 2 numere X [i].
Program ATESTAT_2008_16_SUMA_CIFRE;
uses CRT;
LABEL
100;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of i nt eger ;
VAR
aux1, aux2, t , n, nr , i , j : i nt eger ;
SUMA, nr ci f r e : i nt eger ;
S, X, A : vect or ; {X = vect or ul cu t oat e numer el e}
{A = vect or ul cu j umat at e di n numer el e l ui X }
{car e sat i sf ac condi t i i l e pr obl emei }
CI FRA : vect or ; {vect or car e r et i ne ci f r el e unui numar }
EGALE : BOOLEAN;
function MIN (nr1, nr2 : integer) : integer;
begi n
i f nr 1 < nr 2 t hen
MI N : = nr 1
el se
MI N : = nr 2
end;
procedure ORDONEAZA (VAR S, X : vector);
VAR
aux1, aux2, k : i nt eger ;
begi n
Probleme rezolvate de programare Subiecte date la ATESTAT
39
REPEAT
k : = 0;
i : = 1;
REPEAT
i f S [ i ] > S [ i + 1] t hen
begi n
aux1 : = S [ i ] ;
S [ i ] : = S [ i +1] ;
S [ i +1] : = aux1;
aux2 : = X [ i ] ;
X [ i ] : = X [ i +1] ;
X [ i +1] : = aux2;
k : = 1
end;
i : = i + 1
unt i l ( i > n- 1)
unt i l ( k = 0) ;
end;
function NRCIF (y : integer) : integer;
VAR
nr : i nt eger ;
begi n
i f y = 0 t hen
nr : = 1
el se
begi n
nr : = 0;
whi l e y <> 0 do
begi n
nr : = nr + 1;
y : = y DI V 10
end;
end;
NRCI F : = nr
end;
procedure DETCIFRE (VAR nr : integer; VAR CIFRA : vector);
VAR
numar : i nt eger ;
begi n
numar : = nr ;
f or j : = 1 t o NRCI F ( numar ) do
begi n
CI FRA [ j ] : = numar MOD 10;
numar : = numar DI V 10;
Probleme rezolvate de programare Subiecte date la ATESTAT
40
end;
{ wr i t e ( ' Ci f r el e l ui ' , nr , ' sunt ' ) ;
f or j : = NRCI F ( nr ) DOWNTO 1 do
wr i t e ( CI FRA [ j ] , ' ' ) ; }
wr i t el n;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i numar ul de numer e, n = ' ) ;
r eadl n ( n) ;
wr i t el n;
wr i t el n ( ' I nt r oducet i numer el e i nt r egi mai mi ci ca
30. 000' ) ;
wr i t el n;
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i X [ ' , i , ' ] = ' ) ;
r eadl n ( X [ i ] ) ;
{ wr i t el n ( ' X [ ' , i , ' ] ar e ' , NRCIF (x [i] ),'
ci f r e' ) ; }
DETCIFRE ( X [i], CIFRA ); {det er mi namci f r el e l ui
X [ i ] }
S [ i ] : = 0;
f or j : = 1 t o NRCIF ( X[i] ) do
begi n
S [ i ] : = S [ i ] + CI FRA [ j ] ;
end;
end;
ORDONEAZA ( S, X);
wr i t el n;
100: wr i t el n ( ' Numer el e or donat e cr escat or dupa suma ci f r el or
sunt : ' ) ;
f or i : = 1 t o n do
begi n
wr i t el n ( ' X [ ' , i , ' ] = ' , X[ i ] , ' S [ ' , i ,
' ] = ' , S[ i ] ) ;
end;
wr i t el n;
EGALE : = FALSE;
Probleme rezolvate de programare Subiecte date la ATESTAT
41
f or i : = 1 t o n- 1 do
begi n
i f S[ i ] = S[ i +1] t hen
begi n
EGALE : = TRUE;
end;
end;
i f EGALE = TRUE t hen
begi n
f or i : = 1 t o n- 1 do
begi n
i f S[ i ] = S[ i +1] t hen
begi n
X [ i ] : = MIN ( X[i], X[i+1] );
f or t : = i t o n- 1 do
begi n
S [ t ] : = S [ t +1] ;
end;
f or t : = i +1 t o n- 1 do
begi n
X [ t ] : = X [ t +1]
end;
end;
end;
n : = n - 1;
GOTO 100;
end;
r eadl n;
end.
ATESTAT - 2008 - 17 (REPETA 2007 17) Pentru orice numr natural nenul "n" definim
n factorial (notat n !) ca fiind produsul tuturor numerelor naturale nenule mai mici sau egale cu
n (n ! =1*2*3**n). De exemplu : 3 ! =1*2*3 =6 ; 5 ! = 1*2*3*4*5 = 120. Descriei n
limbaj natural o metod eficient de determinare a numrului de cifre nule aflate pe ultimele
poziii consecutive ale valorii obinute n urma evalurii lui n !, n fiind un numr natural de cel
mult 4 cifre.
Exemplu: dac n = 10, n ! =3.628.800 rezultatul va fi 2, deoarece 3.628.800 are doi de 0 la
sfrit.
Rezolvare:
1 - Calculm n FACTORIAL i memorm rezultatul n variabila F.
2 - Determinm numrul de cifre pentru F.
3 - Determinm cifrele lui F i le memorm ntr-un vector.
4 - Numrm cifrele nule consecutive de la sfritul lui F.
Probleme rezolvate de programare Subiecte date la ATESTAT
42
Program ATESTAT_2008_17_NR_CIFRE_NULE_LA_FACTORIAL;
uses CRT;
LABEL
100;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of i nt eger ;
VAR
n, nr , i , j : LONGI NT;
NULE, nr ci f r e : i nt eger ;
F : LONGI NT;
CI FRA : vect or ; {vect or car e r et i ne ci f r el e unui numar }
function FACTORIAL (m: integer): LONGINT;
VAR
f 1 : LONGI NT;
i : i nt eger ;
begi n
f 1 : = 1;
f or i : = 1 t o mdo
f 1 : = f 1 * i ;
FACTORI AL : = F1
end;
function NRCIF (y : LONGINT) : integer;
VAR
nr : LONGI NT;
begi n
i f y = 0 t hen
nr : = 1
el se
begi n
nr : = 0;
whi l e y <> 0 do
begi n
nr : = nr + 1;
y : = y DI V 10
end;
end;
NRCI F : = nr
end;
procedure DETCIFRE (VAR nr : LONGINT; VAR CIFRA : vector);
VAR
numar : LONGI NT;
Probleme rezolvate de programare Subiecte date la ATESTAT
43
begi n
numar : = nr ;
f or j : = NRCI F ( numar ) DOWNTO 1 do
begi n
CI FRA [ j ] : = numar MOD 10;
numar : = numar DI V 10;
end;
{ wr i t e ( ' Ci f r el e l ui ' , nr , ' sunt ' ) ;
f or j : = NRCIF (nr) DOWNTO 1 do
wr i t e ( CI FRA [ j ] , ' ' ) ; }
wr i t el n;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i numar ul n = ' ) ;
r eadl n ( n) ;
wr i t el n;
F : = FACTORI AL ( n) ;
nr ci f r e : = NRCIF (F);
DETCIFRE ( F, CIFRA ); { det er mi namci f r el e l ui F si }
{ l e memor ami n vect or ul CI FRA }
NULE : = 0; {cont or car e numar a ci f r el e nul e}
f or i : = NRCI F ( F) DOWNTO 1 do
begi n
i f CI FRA [ i ] = 0 t hen
begi n
wr i t el n ( ' CI FRA [ ' , i , ' ] = ' , CI FRA [ i ] ) ;
NULE : = NULE + 1;
end
el se
GOTO 100;
end;
wr i t el n;
100: wr i t el n ( ' Nr . ' , F, ' ar e ' , NULE, ' ci f r e consecut i ve
NULE l a sf ar si t ' ) ;
r eadl n;
end.
ATESTAT - 2008 - 18 (REPETA 2007 18) Scriei un program care citete de la
tastatur un numr natural nenul "n" de cel mult 4 cifre i afieaz pe ecran numrul de cifre
nenule aflate la sfritul lui n !
Exemplu: pentru n = 6 se va afia 0.
Rezolvare:
1 Cu funcia FACTORIAL calculm n factorial i memorm rezultatul n variabila F.
Probleme rezolvate de programare Subiecte date la ATESTAT
44
2 - Determinm numrul de cifre pentru F, folosind o funcie NRCIF.
3 Definim o funcie DETCIFRE cu care determinm cifrele lui F i le memorm ntr-un
vector.
4 - Numrm cifrele NEnule consecutive de la sfritul lui F.
Program ATESTAT_2008_18_NR_CIFRE_NENULE_LA_SFARSIT_FACTORIAL;
uses CRT;
LABEL
100;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of i nt eger ;
VAR
n, nr , i , j : LONGI NT;
NENULE, nr ci f r e : i nt eger ;
F : LONGI NT;
CI FRA : vect or ; {vect or car e r et i ne ci f r el e unui numar }
function FACTORIAL ( m: i nt eger ) : LONGI NT;
VAR
f 1 : LONGI NT;
i : i nt eger ;
begi n
f 1 : = 1;
f or i : = 1 t o mdo
f 1 : = f 1 * i ;
FACTORI AL : = F1
end;
function NRCIF ( y : LONGI NT) : i nt eger ;
VAR
nr : LONGI NT;
begi n
i f y = 0 t hen
nr : = 1
el se
begi n
nr : = 0;
whi l e y <> 0 do
begi n
nr : = nr + 1;
y : = y DI V 10
end;
end;
NRCI F : = nr
end;
procedure DETCIFRE ( VAR nr : LONGI NT; VAR CI FRA : vect or ) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
45
VAR
numar : LONGI NT;
begi n
numar : = nr ;
f or j : = NRCI F ( numar ) DOWNTO 1 do
begi n
CI FRA [ j ] : = numar MOD 10;
numar : = numar DI V 10;
end;
{ wr i t e ( ' Ci f r el e l ui ' , nr , ' sunt ' ) ;
f or j : = NRCI F ( nr ) DOWNTO 1 do
wr i t e ( CI FRA [ j ] , ' ' ) ; }
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i numar ul n = ' ) ; r eadl n ( n) ;
F := FACTORIAL (n);
nrcifre := NRCIF (F);
DETCIFRE ( F, CIFRA ); { det er mi namci f r el e l ui F si }
{ l e memor ami n vect or ul CI FRA }
NENULE : = 0; {cont or car e numar a ci f r el e nul e}
f or i : = NRCI F ( F) DOWNTO 1 do
begi n
i f CI FRA [ i ] <> 0 t hen
begi n
wr i t el n ( ' CI FRA [ ' , i , ' ] = ' , CI FRA [ i ] ) ;
NENULE : = NENULE + 1;
end
el se
GOTO 100;
end;
wr i t el n;
100: wr i t el n ( ' Nr . ' , F, ' ar e ' , NENULE, ' ci f r e consecut i ve
NENULE l a sf ar si t ' ) ;
r eadl n;
end.
ATESTAT - 2008 - 19 (REPETA 2007 19) Scriei un program care citete din
fiierul text DATE.IN cel mult 100 de numere naturale nenule aflate pe o singur linie, formate
din cel mult 4 cifre fiecare, separate prin spaii i scrie n fiierul DATE.OUT, n ordine invers
fa de cea n care au fost citite, pe o singur linie, separate prin spaii. Exemplu: dac din
fiierul DATE.IN se citesc numerele 93 207 15 1982 3762, atunci coninutul fiierului
DATE.OUT va fi 3762 1982 15 207 93.
Rezolvare: Pentru a extrage numerele din fiierul text DATE.IN, definim procedura
EXTRAGENUMEREDINFISIERTEXT.
Probleme rezolvate de programare Subiecte date la ATESTAT
46
Program ATESTAT_2008_19_INVERSAREA_ELEMENTELOR_UNUI_SIR;
uses CRT;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of LONGI NT;
VAR
nr , n, i , t : LONGI NT;
si r , l i ni e : STRI NG;
EROARE : i nt eger ; {var i abi l a EROARE di n apel ul pr ocedur i i
VAL}
A : vect or ;
f 1, f 2 : TEXT;
l i ni e1, l i ni e2 : STRI NG; {l i ni i di n cel e doua f i si er e t ext }
procedure EXTRAGENUMEREDINFISIERTEXT ( VAR f : t ext ; VAR A :
vect or ) ;
VAR
poz, k : i nt eger ;
numar : LONGI NT;
begi n
RESET ( f ) ;
r ead ( f , l i ni e) ;
i : = 0; { i = i ndex vect or A }
si r : = ' ' ; {i ni t i al i zar e cu si r ul vi d}
{ Cu var i abi l a " si r " se ci t est e cat e un
numar di n l i ni e}
poz : = 1;
whi l e poz < LENGTH ( l i ni e) do
begi n
k: = poz;
whi l e l i ni e [ k ] <> ' ' do
begi n
si r : = si r + l i ni e [ k ] ;
{concat enar e}
k : = k + 1;
end;
poz : = k;
i : = i + 1;
VAL ( si r , numar , EROARE) ; {t r ansf . si r - > i n
numar }
A [ i ] : = numar ;
si r : =' ' ;
poz : = poz + 1;
end;
end;
Probleme rezolvate de programare Subiecte date la ATESTAT
47
Begin { PROGRAM PRINCIPAL }
CLRSCR;
ASSI GN ( f 1, ' C: \ DATE. I N' ) ;
ASSI GN ( f 2, ' C: \ DATE. OUT' ) ;
REWRI TE ( f 1) ; {Gener ar e ( i ni t i al i zar e) f i si er e}
REWRI TE ( f 2) ;
wr i t el n ( ' I nt r oducet i , i n f i si er ul DATE. I N, n numer e
nat ur al e cu cel mul t 4 ci f r e' ) ;
wr i t el n;
wr i t e ( ' Cat e numer e dor i t i sa i nt r oducet i , n = ' ) ;
r eadl n ( n) ;
wr i t el n;
linie1 : = ' ' ; {i ni t i al i zar e var i abi l a " l i ni e1" cu si r ul
vi d}
{" l i ni e1" va cont i ne cel e n numer e separ at e de
spat i i }
{" l i ni e1" = un STRI NG car e se va i nt r oduce i n
f i si er ul t ext }
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i un numar nat ur al , nr = ' ) ;
r eadl n ( nr ) ;
STR ( nr , si r ) ; {t r ansf or mamnumar ul i n si r , pent r u a-
l memor a i n
f i si er ul t ext }
linie1 := linie1 + sir + ' '; {const r ui m" l i ni e1"
pr i n concat enar e, }
end; {scr i i nd un " spat i u" dupa
f i ecar e si r }
wr i t el n ( f 1, l i ni e1) ; {scriem "linie1" in fisierul text
DATE.IN}
wr i t el n;
wr i t el n ( ' Fi si er ul t ext DATE. I N ar e cont i nut ul : ' ) ;
RESET ( f 1) ;
READLN ( f 1, l i ni e1) ;
wr i t el n ( ' Li ni e 1 = ' , l i ni e1) ;
wr i t el n;
{Di n STRI NG- ul " l i ni e1" t r ebui e sa ext r agemnumer el e }
EXTRAGENUMEREDINFISIERTEXT ( f1, A );
wr i t el n;
{ wr i t el n ( ' Vect or ul A, r ezul t at dupa ext r ager e numer e di n
f i si er ul DATE. I N est e: ' ) ;
f or i : = 1 t o n do
begi n
wr i t el n ( ' A [ ' , i , ' ] = ' , A[ i ] ) ;
end; }
Probleme rezolvate de programare Subiecte date la ATESTAT
48
wr i t el n;
{Const r ui mlinie2 pe car e o vomscr i e i n f i si er ul DATE.OUT}
linie2 := ''; {i ni t i al i zar e var i abi l a " l i ni e2" cu si r ul
vi d}
{" l i ni e2" va cont i ne cel e n numer e separ at e de
spat i i }
{" l i ni e2" = un STRI NG car e se va i nt r oduce i n
f i si er ul t ext }
f or i : = n DOWNTO 1 do
begi n
STR ( A[i], sir); {t r ansf or mamnumar ul A [ i ] i n si r ,
}
{pent r u a- l memor a i n f i si er ul
t ext }
l i ni e2 : = l i ni e2 + si r + ' ' ; {const r ui m" l i ni e2"
pr i n concat enar e}
end; {scr i i nd un " spat i u" dupa
f i ecar e si r }
wr i t el n ( f 2, l i ni e2) ; {scriem "linie2" in fisierul text
DATE.OUT}
wr i t el n;
wr i t el n ( ' Fi si er ul t ext DATE. OUT ar e cont i nut ul : ' ) ;
RESET ( f 2) ;
READLN ( f 2, l i ni e2) ;
wr i t el n ( ' Li ni e 2 = ' , l i ni e2) ;
wr i t el n;
RESET ( f 1) ;
RESET ( f 2) ;
r eadl n ( f 1, l i ni e1) ;
r eadl n ( f 2, l i ni e2) ;
wr i t el n;
wr i t el n ( ' ============================================' ) ;
wr i t el n;
wr i t el n ( ' Li ni e 1 = ' , l i ni e1) ;
wr i t el n;
wr i t el n ( ' Li ni e 2 = ' , l i ni e2) ;
wr i t el n;
r eadl n
end.
ATESTAT - 2008 - 20 (REPETA 2007 20) Scriei un program care citete de pe
prima linie a fiierului text BAC.TXT, cel mult 100 de numere naturale nenule formate din cel
mult 4 cifre fiecare, separate prin spaii i afieaz pe ecran cifra care apare de cele mai multe
ori n scrierea numerelor citite. Dac exist mai multe cifre care apar de cele mai multe ori, se
vor afia toate acestea.
Probleme rezolvate de programare Subiecte date la ATESTAT
49
Exemplu: dac din fiier se citesc numerele : 90, 73, 109, 1248, 2771, atunci se afieaz 1, 7,
deoarece fiecare dintre aceastea apare de 3 ori.
Rezolvare:
Program ATESTAT_2008_20_FRECVENTA_APARITIE_CIFRE_IN_FISIER_TEXT;
uses CRT;
TYPE
vect or CI FRE = ar r ay [ ' 0' . . ' 9' ] of i nt eger ;
vect or 1 = ar r ay [ 0. . 9] of i nt eger ; {t i p pt . vector FRECVENTA
aparitii }
vect or 2 = ar r ay [ 0. . 9] of ' 0' . . ' 9' ;
VAR
ci f r a : char ;
si r , l i ni e : st r i ng;
nr max : i nt eger ; {numar ul maxi mde apar i t i i al e unei ci f r e }
NRCI FRE : vect or CI FRE;
FRECVENTA : vect or 1;
CI FRE : vect or 2;
{ vect or auxi l i ar f ol osi t l a or donar ea vect or ul ui
FRECVENTA }
nr , n, i : I nt eger ;
f : t ext ;
procedure ORDONEAZA ( VAR v1 : vect or 1; VAR v2 : vect or 2; m:
i nt eger ) ;
VAR
ci f : CHAR ;
j , aux1, aux2, t est : i nt eger ;
begi n
r epeat
t est : = 0;
ci f : = ' 0' ;
j : = 0;
r epeat
i f v1 [ j ] < v1 [ j + 1] t hen
begi n
aux1 : = v1 [ j ] ;
v1 [ j ] : = v1 [ j +1] ;
v1 [ j + 1] : = aux1;
ci f : = v2 [ j ] ;
v2 [ j ] : = v2 [ j + 1 ] ;
v2 [ j + 1 ] : = ci f ;
t est : = 1;
end;
Probleme rezolvate de programare Subiecte date la ATESTAT
50
j : = j + 1 ;
unt i l j > m- 1
unt i l t est = 0
end;
procedure ORDONEAZANRCIFRE ( VAR v : vect or CI FRE ) ;
VAR
ci f : CHAR ;
aux, t est : i nt eger ;
begi n
r epeat
t est : = 0;
ci f : = ' 0' ;
r epeat
i f v [ ci f ] < v [ SUCC ( ci f ) ] t hen
begi n
aux : = v[ ci f ] ;
v [ ci f ] : = v [ SUCC ( ci f ) ] ;
v [ SUCC ( ci f ) ] : = aux;
t est : = 1;
end;
ci f : = SUCC ( ci f )
unt i l ci f > ' 9'
unt i l t est = 0
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
ASSI GN ( f , ' C: \ BAC. t xt ' ) ;
r ewr i t e ( f ) ; {gener amf i si er ul t ext }
wr i t el n ( ' I nt r oducet i , i n f i si er ul BAC. TXT, n numer e
nat ur al e NENULE, de cel mul t 4 ci f r e' ) ;
wr i t el n;
wr i t e ( ' Cat e numer e dor i t i sa i nt r oducet i , n = ' ) ;
r eadl n ( n) ;
l i ni e : = ' ' ; {i ni t i al i zar e var i abi l a " l i ni e" cu si r ul vi d}
{" l i ni e" = un STRI NG car e se va i nt r oduce i n
f i si er ul t ext }
{" l i ni e" va cont i ne cel e n numer e separ at e de
spat i i }
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i un numar nat ur al , nr = ' ) ;
r eadl n ( nr ) ;
STR (nr, sir); {t r ansf or mamnumar ul i n si r , pent r u a-
l memor a i n f i si er ul t ext }
Probleme rezolvate de programare Subiecte date la ATESTAT
51
l i ni e : = l i ni e + si r + ' ' ; {const r ui m" l i ni e" pr i n
concat enar e}
end; {scr i i nd un " spat i u" dupa
f i ecar e si r }
wr i t el n ( f , l i ni e) ; {scr i em" l i ni e" i n f i si er ul t ext }
wr i t el n;
wr i t el n ( ' Fi si er ul t ext ar e cont i nut ul : ' ) ;
RESET ( f ) ;
READLN ( f , l i ni e) ;
wr i t el n ( l i ni e) ;
{ Di n STRI NG- ul " l i ni e" t r ebui e sa ext r agemnumer el e }
RESET ( f ) ;
READLN ( f , l i ni e) ;
f or ci f r a : = ' 0' t o ' 9' do
begi n
NRCI FRE [ ci f r a ] : = 0 {i ni t i al i zar e el ement e
vect or NRCI FRE }
end;
CI FRE [ 0] : = ' 0' ; { Initializare vector CIFRE }
CI FRE [ 1] : = ' 1' ;
CI FRE [ 2] : = ' 2' ;
CI FRE [ 3] : = ' 3' ;
CI FRE [ 4] : = ' 4' ;
CI FRE [ 5] : = ' 5' ;
CI FRE [ 6] : = ' 6' ;
CI FRE [ 7] : = ' 7' ;
CI FRE [ 8] : = ' 8' ;
CI FRE [ 9] : = ' 9' ;
f or i : = 1 t o LENGTH ( l i ni e ) do
begi n
f or ci f r a : = ' 0' t o ' 9' do
begi n
i f l i ni e [ i ] = ci f r a t hen
NRCI FRE [ ci f r a ] : = NRCI FRE [ ci f r a ]
+ 1
end
end;
{ f or ci f r a : = ' 0' t o ' 9' do
begi n
wr i t el n ( ' Ci f r a ' , ci f r a, ' apar e i n l i ni e de ' , NRCI FRE
[ ci f r a] , ' or i ' ) ;
end; }
ci f r a : = ' 0' ;
f or i : = 0 t o 9 do
begi n
FRECVENTA [ i ] : = NRCI FRE [ ci f r a] ;
Probleme rezolvate de programare Subiecte date la ATESTAT
52
wr i t el n ( ' Fr ecvent a ci f r ei ' , ci f r a, ' est e = ' ,
FRECVENTA [ i ] ) ;
ci f r a : = SUCC ( ci f r a) ;
{ wr i t el n ( ' Ci f r a = ' , ci f r a) ; }
end;
ORDONEAZA (FRECVENTA, CIFRE, 9);
{ f or i : = 0 t o 9 do
begi n
wr i t el n ( ' FRECVENTA [ ' , i , ' ] = ' , FRECVENTA [ i ] , '
CI FRE [ ' , i , ' ] = ' , CI FRE [ i ] ) ;
end; }
nr max : = FRECVENTA [ 0] ;
wr i t el n;
wr i t el n ( ' Ci f r el e car e apar de cel e mai mul t e or i , adi ca de
' , nr max, ' or i , sunt : ' ) ;
f or i : = 0 t o 9 do
begi n
i f FRECVENTA [ i ] = nr max t hen
wr i t el n ( ' Ci f r a = ' , CI FRE [ i ] )
end;
r eadl n
end.
ATESTAT - 2008 - 21 (REPETA 2007 21) Scriei un program care citete de pe
prima linie a fiierului text BAC.TXT trei numere naturale A, B, C formate din cel mult patru
cifre fiecare, separate prin cte un spaiu i afieaz pe ecran cel mai mare divizor comun al
acestora.
Exemplu: dac din fiier se citesc numerele: 9 27 15, atunci se afieaz 3.
Rezolvare:
Program ATESTAT_2007_21_CMMDC_PENTRU_3_NUMERE;
uses CRT;
{ ATENTIE ! Programul se poate aplica i pentru n numere
nat ur al e }
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of i nt eger ;
VAR
A : vect or ;
C, n, i : i nt eger ;
l i ni e, si r : st r i ng;
nr , EROARE : i nt eger ;
f : TEXT;
Probleme rezolvate de programare Subiecte date la ATESTAT
53
function CMMDC (P, Q : integer) : integer;
VAR
aux, R : i nt eger ;
begi n
i f P < Q t hen
begi n
aux : = P;
P : = Q;
Q : = aux
end;
R : = P MOD Q;
whi l e R <> 0 do
begi n
P : = Q;
Q : = R;
R : = P MOD Q
end;
CMMDC : = Q
end; { sf . f ct . CMMDC }
function DIVCOM (incep, sf : integer): integer;
{ det er mi na CMMDC pr i n DEI }
VAR
mi j , C1, C2 : i nt eger ;
begi n
i f i ncep = sf t hen
DI VCOM : = A [ i ncep]
el se
begi n
i f sf - i ncep = 1 t hen
DI VCOM : = CMMDC ( A [ i ncep] , A [ sf ] )
el se
begi n
mi j : = ( i ncep + sf ) DI V 2;
C1 : = DI VCOM ( i ncep, mi j ) ;
C2 : = DI VCOM ( mi j + 1, sf ) ;
DI VCOM : = CMMDC ( C1, C2)
end
end
end; { sf . f ct . DI VCOM }
procedure EXTRAGENUMEREDINFISIERTEXT (VAR f: text; VAR A :
vector);
VAR
poz, k : i nt eger ;
numar : LONGI NT;
begi n
RESET ( f ) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
54
r ead ( f , l i ni e) ;
i : = 0; { i = i ndex vect or A }
si r : = ' ' ; {i ni t i al i zar e cu si r ul vi d}
{ I n " si r " se ci t est e cat e un numar di n
l i ni e}
poz : = 1;
whi l e poz < LENGTH ( l i ni e) do
begi n
k: = poz;
whi l e l i ni e [ k ] <> ' ' do
begi n
si r : = si r + l i ni e [ k ] ;
{concat enar e}
k : = k + 1;
end;
poz : = k;
i : = i + 1;
VAL ( si r , numar , EROARE) ; {t r ansf . si r - > i n
numar }
A [ i ] : = numar ;
si r : =' ' ;
poz : = poz + 1;
end;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
ASSI GN ( f , ' C: \ BAC. t xt ' ) ;
r ewr i t e ( f ) ; {gener amf i si er ul t ext }
wr i t el n ( ' I nt r oducet i , i n f i si er ul BAC. TXT, n numer e
nat ur al e cu cel mul t 9 ci f r e' ) ;
wr i t el n;
wr i t e ( ' Cat e numer e dor i t i sa i nt r oducet i , n = ' ) ;
r eadl n ( n) ;
wr i t el n;
l i ni e : = ' ' ; {i ni t i al i zar e var i abi l a " l i ni e" cu si r ul vi d}
{" l i ni e" va cont i ne cel e n numer e separ at e de
spat i i }
{" l i ni e" = un STRI NG car e se va i nt r oduce i n
f i si er ul t ext }
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i un numar nat ur al , nr = ' ) ;
r eadl n ( nr ) ;
STR (nr, sir); {t r ansf or mamnumar ul i n si r , pent r u a-
l memor a i n f i si er ul t ext }
Probleme rezolvate de programare Subiecte date la ATESTAT
55
l i ni e : = l i ni e + si r + ' ' ; {const r ui m" l i ni e" pr i n
concat enar e}
end; {scr i i nd un " spat i u" dupa
f i ecar e si r }
wr i t el n ( f , l i ni e) ; {scr i em" l i ni e" i n f i si er ul t ext }
wr i t el n;
wr i t el n ( ' Fi si er ul t ext ar e cont i nut ul : ' ) ;
RESET ( f ) ;
READLN ( f , l i ni e) ;
wr i t el n ( l i ni e) ;
wr i t el n;
{ Di n STRI NG- ul " l i ni e" t r ebui e sa ext r agemnumer el e }
EXTRAGENUMEREDINFISIERTEXT ( f, A );
{ wr i t el n;
wr i t el n ( ' Vect or ul A, r ezul t at dupa ext r ager e est e: ' ) ;
f or i : = 1 t o n do
begi n
wr i t el n ( ' A [ ' , i , ' ] = ' , A[ i ] ) ;
end;
} wr i t el n;
C : = DI VCOM ( 1, n) ;
wr i t el n ( ' CMMDC al cel or ' , n, ' numer e est e = ' , C) ;
r eadl n
end.
ATESTAT - 2008 - 22 REPETA 2007 22 Se consider programul pseudocod n care s-a
folosit notaia [x] pentru partea ntreag a lui x :
citete n (numr natural)
repet
n <- [n / 10]
pn cnd n < 10
scrie n
Scriei algoritmul pseudocod echivalent cu algoritmul dat, dar care s utilizeze un alt tip de
structur repetitiv.
Rezolvare:
Transformm ciclul cu test final "REPET" - REPEAT
n ciclu cu test iniial "CT TIMP" - WHILE
citete n (numr natural)
ct timp n >= 10 execut
n <- [n / 10]
sfrit structur "ct timp"
scrie n
Program ATESTAT_2008_22_STRUCTURI_ECHIVALENTE;
uses CRT;
Probleme rezolvate de programare Subiecte date la ATESTAT
56
VAR
n : i nt eger ;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i n = ' ) ;
r eadl n ( n) ;
wr i t el n;
wr i t el n ( ' Var i ant a 1 - Cu REPEAT' ) ;
wr i t el n ( ' ==========' ) ;
REPEAT
n : = n DI V 10
UNTI L n < 10;
wr i t el n ( ' Dupa r ul ar e, n = ' , n) ;
wr i t el n;
wr i t el n ( ' Var i ant a 2 - Cu WHI LE' ) ;
wr i t el n ( ' ==========' ) ;
WHI LE n >= 10 do
begi n
n : = n DI V 10
end;
wr i t el n ( ' Dupa r ul ar e, n = ' , n) ;
r eadl n
end.
ATESTAT - 2008 - 23 (REPETA 2007 23) Se consider programul pseudocod n care s-a
folosit notaia [x] pentru partea ntreag a lui x :
citete n (numr natural)
repet
n <- [n / 10]
pn cnd n < 10
scrie n
Scriei programul pentru algoritmul dat, apoi afiati numrul de repetiii efectuate de algoritm.
Rezolvare:
Pentru ciclul cu test final "REPET", n program folosim REPEAT.
Folosim un contor "i" care numr repetiiile.
Program ATESTAT_2008_23_PSEUDOCOD;
uses CRT;
VAR
n, i : i nt eger ;
Begin { PROGRAM PRINCIPAL }
Probleme rezolvate de programare Subiecte date la ATESTAT
57
CLRSCR;
wr i t e ( ' Dat i n = ' ) ;
r eadl n ( n) ;
wr i t el n;
i : = 0;
REPEAT
n : = n DI V 10;
i : = i + 1
UNTI L n < 10;
wr i t el n ( ' Dupa r ul ar e, n = ' , n) ;
wr i t el n;
wr i t el n ( ' Numar ul de r epet i t i i , i = ' , i ) ;
r eadl n
end.
ATESTAT - 2008 - 24 (REPETA 2007 24) Se consider subprogramul NRDIVIZORI,
care:
- primete, prin intermediul unicului su parametru, A, un numr natural de cel mult 4
cifre;
- returneaz numrul divizorilor lui A;
Scriei un program care stabilete dac un numr este prim, utiliznd aceast funcie.
Rezolvare:
Program ATESTAT_2008_24_VERIFICARE_NUMAR_PRIM ;
uses CRT;
VAR
x : i nt eger ;
function NRDIVIZORI ( A : integer) : integer;
VAR
di vi zor , nr di v : i nt eger ;
begi n
nr di v : = 0;
di vi zor : = 1;
f or di vi zor : = 1 t o A do
begi n
i f A MOD di vi zor = 0 t hen
nr di v : = nr di v + 1;
end;
NRDI VI ZORI : = nr di v;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
Probleme rezolvate de programare Subiecte date la ATESTAT
58
wr i t e ( ' Dat i un numar nat ur al f or mat di n cel mul t 4 ci f r e, x
= ' ) ;
r eadl n ( x) ;
i f NRDI VI ZORI ( x) <= 2 t hen
wr i t el n ( ' Numar ul ' , x, ' est e PRI M' )
el se
wr i t el n ( ' Numar ul ' , x, ' NU est e PRI M' ) ;
r eadl n
end.
ATESTAT - 2008 - 25 (REPETA 2007 25) Scriei un program care verific dac un numr
natural k (1 < k < 100.000) citit de la tastatur este prim. Programul va afia pe
ecran mesajul DA sau mesajul NU;
Rezolvare:
Program ATESTAT_2008_25_VERIFICARE_NUMAR_PRIM ;
uses CRT;
VAR
k : LONGI NT ;
function PRIM (z : LONGINT) : BOOLEAN;
VAR
B : bool ean;
di vi zor , nr di v : LONGI NT;
begi n
nr di v : = 0;
di vi zor : = 1;
f or di vi zor : = 1 t o Z do
begi n
i f Z MOD di vi zor = 0 t hen
nr di v : = nr di v + 1;
end;
i f nr di v <= 2 t hen
B : = TRUE
el se
B : = FALSE;
PRI M : = B
end; {sf ar si t f unct i e}
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t el n ( ' I nt r oducet i un numar i nt r e 1 si 100. 000' ) ;
wr i t el n ;
wr i t e ( ' Dat i numar ul k = ' ) ;
r eadl n ( k) ;
i f PRI M ( k ) = TRUE t hen
Probleme rezolvate de programare Subiecte date la ATESTAT
59
wr i t el n ( ' DA, ' , k, ' est e numar PRI M' )
el se
wr i t el n ( ' NU, ' , k, ' NU est e numar PRI M' ) ;
r eadl n
end.
ATESTAT - 2008 - 26 (REPETA 2007 26) Se citesc dou numere naturale A i B nenule,
cu maximum 9 cifre. S se verifice dac cele dou numere sunt doi termeni consecutivi ai
irului lui Fibonacci. irul lui Fibonacci are urmtoarea definiie :
f1 =1
f2 =1
fi = fi-1 + fi-2, dac i>2.
Exemplu: dac A = 3 i B = 5, atunci se va afia mesajul Da , iar pentru A = 21 i B = 5 se
va afia mesajul Nu .
Rezolvare:
Program ATESTAT_2008_26_TERMENI_CONSECUTIVI_SIR_FIBONACCI ;
VAR
A, B : LONGI NT;
function FIBO (m : integer) : integer;
begi n
i f ( m= 1) or ( m=2) t hen
FI BO : = 1
el se
FI BO : = FI BO ( m- 1) + FI BO ( m- 2)
end;
function VERIFICA (y : integer) : boolean;
l abel
10;
VAR
j : LONGI NT;
k : bool ean;
begi n
k : = FALSE; {pr esupun ca y nu est e el ement al si r ul ui
Fi bo}
j : = 1;
whi l e FI BO ( j ) <= y do
begi n
i f FI BO ( j ) = y t hen
begi n
k : = TRUE;
GOTO 10;
end;
j : = j + 1;
Probleme rezolvate de programare Subiecte date la ATESTAT
60
end;
10: VERI FI CA : = k
end;
function SUCCESIVE (A, B : LONGINT) : BOOLEAN;
VAR
aux : LONGI NT;
begi n
i f A > B t hen
begi n
aux : = A;
A : = B; { schi mbamA si B i nt r e el e, pent r u a
avea A < B }
B : = aux;
end;
{ A si B sunt doi t er meni SUCCESI VI ai si r ul ui Fi bonacci
daca: }
{ - at at A, cat si B sunt t er meni ai si r ul ui , }
{ - di f er ent a B - A est e, de asemenea, t er men al si r ul ui
Fi bonacci }
{ - di f er ent a B - A < A }
i f ( VERI FI CA ( A) = TRUE ) AND ( VERI FI CA ( B) = TRUE ) t hen
begi n
i f ( VERI FI CA ( B - A) = TRUE ) AND ( B - A <= A )
t hen
SUCCESI VE : = TRUE
el se
SUCCESI VE : = FALSE;
end
el se
SUCCESI VE : = FALSE;
end;
Begin { PROGRAM PRINCIPAL }
wr i t e ( ' Dat i A = ' ) ;
r eadl n ( A) ;
wr i t el n ;
wr i t e ( ' Dat i B = ' ) ;
r eadl n ( B) ;
wr i t el n ;
i f VERI FI CA ( A) = TRUE t hen
wr i t el n ( A, ' apar t i ne si r ul ui l ui Fi bonacci ' )
el se
wr i t el n ( A, ' NU apar t i ne si r ul ui l ui Fi bonacci ' ) ;
i f VERI FI CA ( B) = TRUE t hen
wr i t el n ( B, ' apar t i ne si r ul ui l ui Fi bonacci ' )
Probleme rezolvate de programare Subiecte date la ATESTAT
61
el se
wr i t el n ( B, ' NU apar t i ne si r ul ui l ui Fi bonacci ' ) ;
wr i t el n;
i f SUCCESI VE ( A, B) = TRUE t hen
wr i t el n ( ' Da, ' , A, ' si ' , B, ' sunt t er meni SUCCESI VI ai
si r ul ui Fi nonacci ' )
el se
wr i t el n ( ' Nu, ' , A, ' si ' , B, ' NU sunt t er meni SUCCESI VI
ai si r ul ui Fi nonacci ' ) ;
r eadl n
end.
ATESTAT - 2008 - 27 (REPETA 2007 27) Se consider fiierul text DATE.IN care conine
exact 899 numere distincte formate fiecare din cte 3 cifre. S se afieze i s se scrie n fiierul
text DATE.OUT numrul format din 3 cifre care lipsete din fiierul DATE.IN.
Rezolvare:
TOATE numerele de 3 cifre sunt cuprinse ntre 100 i 999.
n total sunt 900 de numere, adic 999 - 100 + 1.
Dac n fiierul DATE.IN sunt exact 899 numere DISTINCTE formate din 3 cifre, rezult
c, ntr-adevr, lipsete doar un singur numr.
Prelum toate numerele din fiierul DATE.IN ntr-un vector X.
Ordonm vectorul i cutm numrul care lipsete, tiind c, dup ordonare, vectorul va
conine doar numere consecutive, cu excepia locului unde lipsete numrul cutat.
Atunci cnd diferena dintre dou numere succesive din vector este > 1, rezult c n acel loc
lipsete nu numr, care nu poate fi dect media aritmetic a celor dou numere succesive.
Program ATESTAT_2008_27_NUMERE_CONSECUTIVE ;
uses CRT;
CONST
nmax = 1000;
TYPE
vect or = ar r ay [ 1. . nmax] of i nt eger ;
VAR
x, y, i , j , t : i nt eger ;
f 1, f 2 : t ext ;
si r , l i ni e1, l i ni e2 : STRI NG;
EROARE : i nt eger ; {var i abi l a EROARE di n apel ul pr ocedur i i
VAL}
A : vect or ;
procedure EXTRAGENUMEREDINFISIERTEXT (VAR f: text; VAR A :
vector);
VAR
c : CHAR;
poz, k : i nt eger ;
Probleme rezolvate de programare Subiecte date la ATESTAT
62
numar : i nt eger ;
nr car act er e : i nt eger ;
begi n
RESET ( f ) ;
i : = 0; { i = i ndex vect or A }
nr car act er e : = 0; {cont or car e numar a t oat e car act er el e
di n f i si er }
whi l e not EOF ( f ) do
begi n
r ead ( f , c) ;
nr car act er e : = nr car act er e + 1
end;
wr i t el n;
wr i t el n ( ' Fi si er ul DATE. I N ar e ' , nr car act er e, '
car act er e' ) ;
wr i t el n;
RESET ( f ) ;
poz : = 1;
si r : = ' ' ; {i ni t i al i zar e cu si r ul vi d}
{ I n " si r " se ci t est e cat e un numar di n
f i si er }
whi l e not EOF ( f ) do
{ whi l e poz < nr car act er e do }
begi n
READ ( f , c) ;
k: = poz;
whi l e c <> ' ' do
begi n
si r : = si r + c ; {concat enar e}
READ ( f , c) ;
end;
poz : = k;
i : = i + 1;
VAL ( si r , numar , EROARE) ; {t r ansf . si r - > i n
numar }
A [ i ] : = numar ;
si r : =' ' ;
poz : = poz + 1;
end;
end;
procedure AFISAREVECTOR ;
begi n
f or i : = 1 to 200 do
Probleme rezolvate de programare Subiecte date la ATESTAT
63
begi n
wr i t e ( A[ i ] , ' ' ) ;
end;
wr i t el n;
wr i t el n ( ' Apasat i ENTER' ) ;
r eadl n;
f or i : = 201 to 400 do
begi n
wr i t e ( A[ i ] , ' ' ) ;
end;
wr i t el n;
wr i t el n ( ' Apasat i ENTER' ) ;
r eadl n;
f or i : = 401 to 600 do
begi n
wr i t e ( A[ i ] , ' ' ) ;
end;
wr i t el n;
wr i t el n ( ' Apasat i ENTER' ) ;
r eadl n;
f or i : = 601 to 800 do
begi n
wr i t e ( A[ i ] , ' ' ) ;
end;
wr i t el n ( ' Apasat i ENTER' ) ;
r eadl n;
f or i : = 801 t o 899 do
begi n
wr i t e ( A[ i ] , ' ' ) ;
end;
wr i t el n ( ' Apasat i ENTER' ) ;
r eadl n;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
ASSI GN ( f 1, ' C: \ DATE. I N' ) ;
ASSI GN ( f 2, ' C: \ DATE. OUT' ) ;
r ewr i t e ( f 1) ; {gener amf i si er ul t ext f 1}
r ewr i t e ( f 2) ; {gener amf i si er ul t ext f 2}
{ I nt r oducemcel e 899 numer e di n 3 ci f r e i n f i si er ul
DATE. I N }
{ mai put i n un numar , de exempl u x = 456 }
wr i t e ( ' Ce numar de 3 ci f er dor i t i sa l i pseasca ? Dat i
numar ul x = ' ) ;
r eadl n ( x) ;
si r : = ' ' ; {i ni t i al i zar e cu si r ul vi d}
Probleme rezolvate de programare Subiecte date la ATESTAT
64
f or i : = 100 t o x - 1 do
begi n
STR (i, sir);
{t r ansf or mamnumar ul " i " i n si r , pent r u a- l memor a i n
f i si er ul t ext }
si r : = si r + ' ' ; {const r ui m" si r " pr i n concat enar e}
{scr i i nd un " spat i u" dupa f i ecar e
si r }
wr i t e ( f 1, si r ) ;
end;
f or i : = x + 1 t o 999 do
begi n
STR (i, sir);
{t r ansf or mamnumar ul " i " i n si r , pent r u a- l memor a i n
f i si er ul t ext }
si r : = si r + ' ' ; {const r ui m" si r " pr i n concat enar e}
{scr i i nd un " spat i u" dupa f i ecar e
si r }
wr i t e ( f 1, si r ) ;
end;
EXTRAGENUMEREDINFISIERTEXT ( f1, A );
wr i t el n ( ' Vect or ul A, r ezul t at dupa ext r ager e est e: ' ) ;
AFISAREVECTOR ;
f or i : = 1 t o 898 do
begi n
i f A [ i + 1] - A [ i ] > 1 t hen
y : = ( A[ i ] + A [ i +1] ) DI V 2
end;
wr i t el n ( ' Numar ul car e l i psest e est e y = ' , y) ;
r eadl n
end.
ATESTAT - 2008 - 28 (REPETA 2007 28) Se citesc dou numere naturale nenule m i n (2
< m, n < 20). S se scrie programul care construiete n memorie o matrice A cu m linii i n
coloane cu proprietatea c elementul A [i, j] este egal cel mai mare divizor comun dintre
numerele i i j. Matricea se va afia pe ecran, cte o linie a matricei pe cte o linie a ecranului,
elementele fiecrei linii fiind separate prin spaii.
Exemplu: pentru m = 3 i n = 4, se va afia matricea urmtoare :
1 1 1 1
1 2 1 2
1 1 3 1
Rezolvare:
Program ATESTAT_2008_28_MATRICE_CU_CMMDC_AL_INDECSILOR ;
CONST
Probleme rezolvate de programare Subiecte date la ATESTAT
65
nmax = 20;
TYPE
mat r i ce = ar r ay [ 1. . nmax, 1. . nmax] of i nt eger ;
VAR
A : mat r i ce;
m, n, i , j : 1. . 20;
function CMMDC ( X, Y : integer) : integer;
VAR
aux, R : i nt eger ;
begi n
i f X < Y t hen
begi n
aux : = X;
X : = Y;
Y : = aux
end;
R : = X MOD Y;
whi l e R <> 0 do
begi n
X : = Y;
Y : = R;
R : = X MOD Y
end;
CMMDC : = Y
end; { sf . f ct . CMMDC }
Begin { PROGRAM PRINCIPAL }
r epeat
wr i t e ( ' Dat i m= ' ) ;
r eadl n ( m) ;
unt i l ( m>= 1) and ( m<= nmax) ;
r epeat
wr i t e ( ' Dat i n = ' ) ;
r eadl n ( n)
unt i l ( n >= 1) and ( n<=nmax) ;
wr i t el n;
{ Gener amel ement el e mat r i ci i A }
f or i : = 1 t o mdo
begi n
f or j : = 1 t o n do
begi n
A [ i , j ] : = CMMDC ( i , j ) ;
end
end;
wr i t el n ( ' Mat r i cea A est e: ' ) ;
f or i : = 1 t o mdo
begi n
Probleme rezolvate de programare Subiecte date la ATESTAT
66
f or j : = 1 t o n do
begi n
wr i t e ( A [ i , j ] , ' ' ) ;
end ;
wr i t el n;
end;
r eadl n
end.
ATESTAT - 2008 - 29 (REPETA 2007 29) Se citete de la tastatur un numr natural nenul
"n" (n < 1.000). Scriei un program care construiete fiierul text BAC.TXT, care conine pe
prima linie toi divizorii lui n n ordine strict descresctoare. Divizorii vor fi separai prin
spaiu.
Exemplu: dac n = 10, atunci fiierul BAC.TXT va conine: 10, 5, 2, 1.
Rezolvare:
Program
ATESTAT_2008_29_DIVIZORII_UNUI_NUMAR_IN_ORDINE_DESCRESCATOARE ;
uses CRT;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of i nt eger ;
VAR
n, nr , i , j , s1 : i nt eger ;
di vi zor , nr di v, EROARE : i nt eger ;
x, A : vect or ;
f : t ext ;
si r , l i ni e : STRI NG;
procedure ORDONEAZADESCRESCATOR (VAR v : vector; m : integer);
VAR
j , aux, t est : i nt eger ;
begi n
r epeat
t est : = 0;
j : = 1;
r epeat
i f v [ j ] < v [ j + 1] t hen
begi n
aux : = v[ j ] ;
v [ j ] : = v [ j +1] ;
v [ j + 1] : = aux;
t est : = 1;
Probleme rezolvate de programare Subiecte date la ATESTAT
67
end;
j : = j + 1
unt i l j > m- 1
unt i l t est = 0
end;
procedure EXTRAGENUMEREDINFISIERTEXT (VAR f: text; VAR A :
vector);
VAR
poz, k : i nt eger ;
numar : LONGI NT;
begi n
RESET ( f ) ;
r ead ( f , l i ni e) ;
i : = 0; { i = i ndex vect or A }
si r : = ' ' ; {i ni t i al i zar e cu si r ul vi d}
{ I n " si r " se ci t est e cat e un numar di n
l i ni e}
poz : = 1;
whi l e poz < LENGTH ( l i ni e) do
begi n
k: = poz;
whi l e l i ni e [ k ] <> ' ' do
begi n
si r : = si r + l i ni e [ k ] ;
{concat enar e}
k : = k + 1;
end;
poz : = k;
i : = i + 1;
VAL (sir, numar, EROARE); {transf. sir -> in
numar }
A [ i ] : = numar ;
si r : =' ' ;
poz : = poz + 1;
end;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i numar ul n = ' ) ;
r eadl n ( n) ;
i : = 0 ; {i ndex pt . vect or ul X i n car e scr i u di vi zor i i l ui
n }
nr di v : = 0 ; {cont or car e numar a di vi zor i i l ui n }
f or di vi zor : = 1 t o n do
begi n
Probleme rezolvate de programare Subiecte date la ATESTAT
68
i f n MOD di vi zor = 0 t hen
begi n
nr di v : = nr di v + 1;
i : = i + 1;
X [ i ] : = di vi zor
end;
end;
ORDONEAZADESCRESCATOR ( X , nrdiv);
wr i t el n;
wr i t el n ( ' Di vi zor i i l ui ' , n, ' , or donat i descr escat or ,
sunt : ' ) ;
wr i t el n;
f or i : = 1 t o nr di v do
begi n
wr i t e ( x [ i ] , ' ' ) ;
end;
wr i t el n;
ASSI GN ( f , ' C: \ BAC. t xt ' ) ;
r ewr i t e ( f ) ; {gener amf i si er ul t ext }
l i ni e : = ' ' ; {i ni t i al i zar e var i abi l a " l i ni e" cu si r ul vi d}
{" l i ni e" = un STRI NG car e se va i nt r oduce i n
f i si er ul t ext }
{" l i ni e" va cont i ne cel e n numer e separ at e de
spat i i }
f or i : = 1 t o nr di v do
begi n
STR ( X [i], sir);
{t r ansf or ma numar ul X [ i ] i n si r , pent r u a- l memor a i n
f i si er ul t ext }
l i ni e : = l i ni e + si r + ' ' ; {const r ui m" l i ni e" pr i n
concat enar e}
end; {scr i i nd un " spat i u" dupa
f i ecar e si r }
wr i t el n ( f , l i ni e) ; {scr i em" l i ni e" i n f i si er ul t ext }
wr i t el n;
wr i t el n ( ' Fi si er ul t ext ar e cont i nut ul : ' ) ;
RESET ( f ) ;
READLN ( f , l i ni e) ;
wr i t el n ( l i ni e) ; wr i t el n;
{ Di n STRI NG- ul " l i ni e" t r ebui e sa ext r agemnumer el e }
EXTRAGENUMEREDINFISIERTEXT ( f, A ); wr i t el n;
wr i t el n ( ' Vect or ul A, r ezul t at dupa ext r ager e numer e di n
f i si er , est e: ' ) ;
f or i : = 1 t o nr di v do
begi n
wr i t el n ( ' A [ ' , i , ' ] = ' , A[ i ] ) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
69
end;
r eadl n
end.
ATESTAT - 2008 - 30 (REPETA 2007 30) Se consider fiierul text CUVINTE.IN, care
conine pe prima linie un numr natural nenul n (n <= 100), iar pe urmtoarele n linii cte un
cuvnt cu maximum 10 caractere. S se afieze pe ecran cuvintele din fiierul dat care sunt
palindroame. n cazul n care nu exist nici un cuvnt palindrom, se va afia mesajul NU.
Un cuvnt este palindrom dac citindu-l de la dreapta la stnga sau de la stnga la dreapta se
obine acelai cuvnt.
Exemplu: dac fiierul CUVINTE.IN are urmtorul coninut :
3
sas
creion
Ion
atunci se va afia pe ecran :
sas
Rezolvare:
Program ATESTAT_2008_30_PALINDROAME_IN_FISIER_TEXT;
uses CRT;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of STRI NG;
VAR
nr , n, i , t : i nt eger ;
f : t ext ;
cuvant , si r , l i ni e : STRI NG;
EROARE : i nt eger ; {var i abi l a EROARE di n apel ul pr ocedur i i
VAL}
A : vect or ;
pal i ndr oame : i nt eger ; {cont or car e numar a pal i ndr oamel e
gasi t e}
function PALINDROM ( si r : STRI NG) : BOOLEAN; {ver i f i ca si r ul
est e pal i ndr om}
VAR
k : BOOLEAN;
begi n
K : = TRUE;
f or i : = 1 t o LENGTH ( si r ) DI V 2 do
begi n
i f si r [ i ] <> si r [ LENGTH ( si r ) - i + 1] t hen
{ver i f i camSI METRI A}
Probleme rezolvate de programare Subiecte date la ATESTAT
70
k : = FALSE
end;
PALI NDROM : = K;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
ASSI GN ( f , ' C: \ CUVI NTE. I N' ) ;
REWRI TE ( f ) ; {gener amf i si er ul t ext }
wr i t el n ( ' I nt r oducet i , i n f i si er ul CUVI NTE. I N, n CUVI NTE cu
cel mul t 10 car act er e' ) ;
wr i t el n;
wr i t e ( ' Cat e cuvi nt e dor i t i sa i nt r oducet i , n = ' ) ;
r eadl n ( n) ;
wr i t el n;
STR (n, sir); {t r ansf or mamnumar ul n i n si r , i nai nt e de a- l
scr i i n f i si er }
wr i t el n ( f , si r ) ; {scr i emn i n f i si er ul t ext pe pr i ma l i ni e
}
{scr i emcel e n cuvi nt e i n f i si er ul t ext }
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i cuvant ul nr ' , i , ' = ' ) ;
r eadl n ( cuvant ) ;
wr i t el n ( f , cuvant ) ;
{ f ol osi m" writeln" si NU " write" pent r u a
scr i e }
{ f i ecar e cuvant pe cat e o l i ni e }
end;
wr i t el n;
wr i t el n ( ' Fi si er ul t ext ar e cont i nut ul : ' ) ;
RESET ( f ) ;
readln (f, linie); {ci t i mprima linie, car e cont i ne numar ul
n }
i : = 0; {cont or - opt i onal , pt . numar ar e l i ni i di n f i si er ul
t ext }
whi l e not EOF ( f ) do
begi n
READLN (f, linie); {se ci t esc ur mat oar el e l i ni i di n
f i si er }
i : = i + 1;
A [ i ] : = l i ni e;
wr i t el n ( ' Li ni a ' , i , ' = ' , l i ni e) ;
end;
wr i t el n;
wr i t el n ( ' Vect or ul A cont i ne cuvi nt el e di n f i si er ul t ext ' ) ;
wr i t el n;
Probleme rezolvate de programare Subiecte date la ATESTAT
71
pal i ndr oame : = 0; {cont or car e numar a pal i ndr oamel e }
f or t : = 1 t o n do
begi n
i f PALI NDROM ( A [ t ] ) = TRUE t hen
begi n
pal i ndr oame : = pal i ndr oame + 1;
wr i t el n ( ' A [ ' , t , ' ] = ' , A [ t ] , ' est e
PALI NDROM' ) ;
end
el se
begi n
wr i t el n ( ' A [ ' , t , ' ] = ' , A [ t ] , ' NU est e
PALI NDROM' ) ;
end;
end; {sf . FOR}
wr i t el n ( ' Apasat i ENTER' ) ;
r eadl n;
CLRSCR;
i f pal i ndr oame = 0 t hen
begi n
wr i t el n ( ' NU exi st a pal i ndr oame i n f i si er ul t ext ' ) ;
end
el se
begi n
f or t : = 1 t o n do
begi n
i f PALI NDROM ( A [ t ] ) = TRUE t hen
begi n
wr i t el n ( ' A [ ' , t , ' ] = ' , A [ t ] , '
est e PALI NDROM' )
end
end;
end;
r eadl n
end.
ATESTAT - 2008 - 31 (REPETA 2007 31) Scriei definiia unui subprogram
SUMADIVIZORI cu doi parametri, subprogram care:
- primete, prin intermediul parametrului n, un numr ntreg de maxim 9 cifre;
- returneaz, prin intermediul celui de-al doilea parametru k, suma tuturor divizorilor lui n.
Exemplu: pentru n = 6, valoarea lui k va fi 12 (1 +2 +3 +6).
Rezolvare:
Program ATESTAT_2008_31_SUMA_DIVIZORILOR_UNUI_NUMAR ;
uses CRT;
Probleme rezolvate de programare Subiecte date la ATESTAT
72
VAR
n, K : LONGI NT;
procedure SUMADIVIZORI (nr : LONGINT ; VAR S : LONGINT );
VAR
di vi zor : LONGI NT;
nr di v : i nt eger ;
begi n
S : = 0;
nr di v : = 0;
f or di vi zor : = 1 t o nr do
begi n
i f nr MOD di vi zor = 0 t hen
begi n
nr di v : = nr di v + 1;
S : = S + di vi zor
end;
end;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i numar ul n = ' ) ;
r eadl n ( n) ;
wr i t el n;
SUMADIVIZORI (n, k); {apel ul pr ocedur i i , i n ur ma car ui a
obt i n k }
wr i t el n ( ' Suma di vi zor i l or l ui ' , n, ' = ' , k ) ;
r eadl n
end.
ATESTAT - 2008 - 32 (REPETA 2007 32) Scriei un program care s determine n mod
eficient primele "p" perechi distincte de numere prietene. Fiecare pereche va fi afiat pe cte
un rnd, iar elementele unei perechi vor fi separate prin cte un spaiu. Dou numere naturale
distincte A i B sunt numere prietene dac A este egal cu suma divizorilor lui B mai mici
dect B, iar B este egal cu suma divizorilor lui A mai mici dect A. Valoarea numrului p se
citete de la tastatur. (1 <= p <= 8).
Exemplu: pentru p = 3 se vor afia :
220 284
1184 1210
2620 2924
Rezolvare:
Program ATESTAT_2008_32_PERECHI_DISTINCTE_DE_NUMERE_PRIETENE ;
uses CRT;
CONST
nmax = 100;
Probleme rezolvate de programare Subiecte date la ATESTAT
73
TYPE
mat r i ce = ar r ay [ 1. . nmax, 1. . nmax] of i nt eger ; {mat r i cea
per echi l or }
VAR
SA, SB, A, B, di vi zor : i nt eger ;
i , j , p : i nt eger ;
PERECHE : mat r i ce; {mat r i cea per echi l or }
function PRIETENE (A, B : integer) : boolean;
VAR
SA, SB : i nt eger ;
di vi zor : i nt eger ;
begi n
SA : = 0;
SB : = 0;
f or di vi zor : = 1 t o A- 1 do
begi n
i f A MOD di vi zor = 0 t hen
SA : = SA + di vi zor ;
end;
f or di vi zor : = 1 t o B- 1 do
begi n
i f B MOD di vi zor = 0 t hen
SB : = SB + di vi zor ;
end;
i f ( SA = B ) AND ( SB = A ) t hen
begi n
PRI ETENE : = TRUE
end
el se
begi n
PRI ETENE : = FALSE
end;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
p : = 0;
whi l e p <= 6 do
begi n
f or A: = 1 t o 100 do
begi n
f or B : = 1 t o 100 do
begi n
i f PRI ETENE ( A, B) t hen
begi n
wr i t el n ( A, ' si ' , B, ' sunt pr i et ene ' ) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
74
p : = p + 1;
PERECHE [ p, 1] : = A;
PERECHE [ p, 2] : = B;
end
el se
wr i t el n ( A, ' si ' , B, ' NU sunt pr i et ene ' ) ;
end;
end;
end;
r eadl n
end.
ATESTAT - 2008 - 33 (REPETA 2007 33) Se citete de la tastatur un numr natural nenul
"n", care are cel mult 9 cifre. S se scrie n fiierul DATE.OUT numrul natural "k", astfel
nct produsul 1*2**(k-1)*k s aib o valoare ct mai apropiat de numrul "n".
Exemplu: dac se citete numrul n = 25, n fiierul DATE.OUT se va scrie 4 (4 ! =24), iar
dac se citete numrul n = 119, n fiierul DATE.OUT se va scrie 5 (5 ! =125).
Rezolvare:
Program
ATESTAT_2008_33_FACTORIAL_IN_PROXIMITATEA_UNUI_NUMAR_NATURAL ;
uses CRT;
VAR
f : TEXT;
MI N, nr , n, k, di f er ent a1, di f er ent a2 : LONGI NT;
si r : STRI NG;
EROARE : i nt eger ; {var i abi l a er oar e di n apel ul pr ocedur i i
VAL}
f unct i on
f act or i al ( m: i nt eger ) : i nt eger ;
VAR
p, i : i nt eger ;
begi n
p : = 1;
f or i : = 1 t o mdo
begi n
p : = p * i
end;
f act or i al : = p
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
ASSI GN ( f , ' C: \ DATE. OUT' ) ;
REWRI TE ( f ) ; {gener ar e- i ni t i al i zar e f i si er }
wr i t e ( ' Dat i un numar f or mat di n cel mul t 9 ci f r e, n = ' ) ;
r eadl n ( n) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
75
wr i t el n;
k : = 1;
whi l e FACTORI AL ( K) <= n do
begi n
di f er ent a1 : = ABS ( n - FACTORI AL ( k) ) ;
wr i t el n ( ' Pent r u k = ' , k, ' , Fact or i al ( ' , k, ' ) = ' ,
FACTORI AL ( K) ,
' , i ar di f er ent a ' , n, ' - FACTORI AL ( ' , K, ' ) = ' ,
di f er ent a1) ;
k : = k + 1;
end;
wr i t el n;
di f er ent a2 : = ABS ( n - FACTORI AL ( k) ) ;
wr i t el n ( ' Pent r u k = ' , k, ' , Fact or i al ( ' , k, ' ) = ' ,
FACTORI AL ( K) ,
' , i ar di f er ent a | ' , n, ' - FACTORI AL ( ' , K, ' ) | = ' ,
di f er ent a2) ;
i f di f er ent a2 > di f er ent a1 t hen
K : = k - 1 ;
i f di f er ent a2 > di f er ent a1 t hen
MI N : = di f er ent a1
el se
MI N : = di f er ent a2;
wr i t el n;
wr i t el n ( ' Rezul t a ca numar ul k pent r u car e di f er ent a di nt r e
n si FACTORI AL ( k) ' ) ;
wr i t el n ( ' est e mi ni ma, adi ca di f er ent a = ' , MI N, ' , est e k =
' , k) ;
wr i t el n;
STR (k, sir); {t r ansf or mk i n si r , pt . a- l scr i e i n f i si er ul
t ext }
wr i t e ( f , k) ; {scr i u val oar ea l ui k i n f i si er ul DATE. OUT}
RESET ( f ) ; {pr egat i mf i si er ul pent r u ci t i r e}
r ead ( f , si r ) ;
VAL ( si r , nr , EROARE) ;
wr i t el n ( ' Numar ul scr i s " i n" si ci t i t " di n" f i si er est e nr =
' , nr ) ;
wr i t el n ( ' Acest numar t r ebui e sa f i e egal cu k = ' , k) ;
r eadl n
end.
ATESTAT - 2008 - 34 (REPETA 2007 34) Pentru o valoare "n" (numr natural, 1 <= n <=
100) citit de la tastatur, concepei un program care scrie n fiierul BAC.TXT un tablou
bidimensional cu n linii i n coloane cu formatul de jos. Elementele de pe fiecare linie sunt
separate prin spaiu.
1 0 0 0 . . . 0
Probleme rezolvate de programare Subiecte date la ATESTAT
76
2 1 0 0 . . . 0
3 2 1 0 . . . 0
. . . . . . . . . . . . . . .
n n-1 n-2 n-3 ...1
Exemplu: pentru n = 4, coninutul fiierului BAC.TXT este:
1 0 0 0
2 1 0 0
3 2 1 0
4 3 2 1
Rezolvare:
Program
ATESTAT_2008_34_ELEMENTE_NULE_DEASUPRA_DIAGONALEI_PRINCIPALE ;
uses CRT;
CONST
nmax = 100;
TYPE
MATRI CE = ar r ay [ 1. . nmax, 1. . nmax] of i nt eger ;
VAR
n, i , j , k : i nt eger ;
A : mat r i ce;
f : TEXT;
l i ni e, si r : STRI NG;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
ASSI GN ( f , ' C: \ BAC. TXT' ) ;
REWRI TE ( f ) ; {gener ar e- i ni t i al i zar e f i si er }
r epeat
wr i t e ( ' Dat i n = ' ) ;
r eadl n ( n)
unt i l ( n >= 1) AND ( n <= nmax) ;
{Gener amel ement el e mat r i ci i }
f or i : = 1 t o n do
begi n
f or j : = 1 t o n do
begi n
A [ i , j ] : = i - j + 1;
i f A [ i , j ] < 0 t hen
A [ i , j ] : = 0;
end;
end;
wr i t el n;
wr i t el n ( ' Mat r i cea est e: ' ) ;
f or i : = 1 t o n do
Probleme rezolvate de programare Subiecte date la ATESTAT
77
begi n
f or j : = 1 t o n do
begi n
wr i t e ( A [ i , j ] , ' ' ) ;
end;
wr i t el n
end;
wr i t el n;
{ Scr i emmat r i cea i n f i si er ul t ext }
f or i : = 1 t o n do
begi n
l i ni e : = ' ' ; {i ni t i al i zar e l i ni e cu si r ul vi d}
f or j : = 1 t o n do
begi n
STR ( A[i,j], sir ); {t r ansf or mamf i ecar e el ement i n
si r }
l i ni e : = l i ni e + si r + ' ' ; {adaug " si r " l a " l i ni e"
pr i n
concat enar e}
end;
wr i t el n ( f , l i ni e) ; {scr i u " l i ni e" i n f i si er ul t ext }
end;
wr i t el n;
wr i t el n ( ' Cont i nut ul f i si er ul ui t ext est e: ' ) ;
RESET ( f ) ;
whi l e NOT EOF ( f ) do
begi n
r eadl n ( f , l i ni e) ;
wr i t el n ( l i ni e)
end;
r eadl n
end.
ATESTAT - 2008 - 35 (REPETA 2007 35) Scriei un program care citete un numr natural
n > 1, cu maximum 9 cifre, i afieaz valoarea celui mai mic divizor prim a lui n, precum i
puterea la care acest divizor apare n descompunerea n factori primi a numrului n.
Rezolvare:
Program ATESTAT_2008_35_CEL_MAI_MIC_DIVIZOR_PRIM_AL_UNUI_NUMAR ;
{det . di vi zor ul car e apar e l a put er ea maxi ma }
uses CRT;
VAR
Probleme rezolvate de programare Subiecte date la ATESTAT
78
x, d, di vi z, p, MAX : i nt eger ;
function PRIM (nr : integer) : BOOLEAN;
VAR
nr di v, di vi zor : i nt eger ;
begi n
nr di v : = 0;
di vi zor : = 1;
f or di vi zor : = 1 t o nr do
begi n
i f nr MOD di vi zor = 0 t hen
begi n
nr di v : = nr di v + 1;
end;
end;
i f nr di v <= 2 t hen
PRI M : = TRUE
el se
PRI M : = FALSE;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i x = ' ) ;
r eadl n ( x) ;
d : = 2; {pr i mul di vi zor }
max : = 0; {i ni t i al i zar e MAX; MAX = put er ea maxi ma}
p : = 0; {i ni t i al i zar e put er e p = numar de i mpar t i r i l a d}
whi l e x > 1 do
begi n
I f x MOD d = 0 t hen {daca x se i mpar t e l a d}
begi n
p : = p + 1;
x : = x DI V d;
i f max < p t hen {daca MAX ant er i or < put er ea maxi ma
act ual a a l ui d}
begi n
max : = p;
{ wr i t el n ( ' Di vi zor ul ' , d, ' apar e l a put er ea
' , max ) ; }
di vi z : = d;
end
end
el se
begi n
d : = d + 1; { se t r ece l a ur mat or ul di vi zor }
Probleme rezolvate de programare Subiecte date la ATESTAT
79
p : = 0 ; {pt . noul di vi zor , se r ei ni t i al i zeaza
put er e }
end;
end;
wr i t el n ( ' Di vi zor ul ' , di vi z, ' apar e l a put er ea maxi ma,
Pmax = ' , max ) ;
i f PRI M ( di vi z) t hen
wr i t el n ( di vi z, ' est e numar pr i m' )
el se
wr i t el n ( di vi z, ' NU est e numar pr i m' ) ;
r eadl n
end.
ATESTAT - 2008 - 36 (REPETA 2007 36) Scriei programul care citete de la tastatur
dou numere naturale x i n (x, n > 1), cu maximum 9 cifre i verific dac x este
divizibil cu 2
n
. Programul afieaz DA, n caz afirmativ i NU, n caz contrar.
Rezolvare:
Program ATESTAT_2008_36_DIVIZIBILITATE_CU_O_PUTERE_A_LUI_2 ;
uses CRT;
VAR
n, x : LONGI NT;
function PUTERE ( y : LONGINT; m : LONGINT ) : LONGINT;
VAR
p, i : LONGI NT;
begi n
p : = 1;
f or i : = 1 t o mdo
p : = p * y;
PUTERE : = p
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i un numar nat ur al cu cel mul t 9 ci f r e, X = ' ) ;
r eadl n ( x) ;
wr i t e ( ' Dat i un numar nat ur al cu cel mul t 9 ci f r e, car e va f i
exponent ul l ui
2, n = ' ) ;
r eadl n ( n) ;
wr i t el n;
i f X MOD ( PUTERE ( 2, n) ) = 0 t hen
wr i t el n ( X, ' est e di vi zi vi l cu 2 l a put er ea ' , n, ' = ' ,
PUTERE ( 2, n) )
el se
Probleme rezolvate de programare Subiecte date la ATESTAT
80
wr i t el n ( X, ' NU est e di vi zi vi l cu 2 l a put er ea ' , n, ' = ' ,
PUTERE ( 2, n) ) ;
r eadl n
end.
ATESTAT - 2008 - 37 (REPETA 2007 37) Se citete de la tastatur un numr natural "n" (1
<= n <= 100). S se afieze pe ecran al n-lea termen al irului 11, 22, 33, 44, 55, 66, 77, 88,9 9,
111, 222, 333, 444, etc.
Exemplu: dac n = 11, se va afia 222.
Rezolvare:
ATENIE ! NU introducei un n > 73.
Pentru n = 73, se obine numrul 1.111.111.111.
Pentru n = 74, ar trebui s se afieze 2.222.222.222, dar se tie c,
pentru tipul LONGINT, numrul ntreg maxim este MAXLONGINT =2.147.483.647.
Deci, pentru orice n > 74, vom obine rezultate eronate.
Program ATESTAT_2008_37_SIR_DE_NUMERE_CU_CIFRE_IDENTICE ;
uses CRT;
VAR
n : i nt eger ;
function ZECI (m:integer) : LONGINT ;
begi n
i f m= 1 t hen
ZECI : = 11
el se
ZECI : = 11 + ZECI ( m- 1)
end;
function SUTE (m:integer) : LONGINT ;
begi n
i f m= 10 t hen
SUTE : = 111
el se
SUTE : = 111 + SUTE ( m- 1)
end;
function MII (m:integer) : LONGINT ;
begi n
i f m= 19 t hen
MI I : = 1111
el se
MI I : = 1111 + MI I ( m- 1)
end;
function ZECIDEMII (m:integer) : LONGINT ;
begi n
i f m= 28 t hen
ZECI DEMI I : = 11111
Probleme rezolvate de programare Subiecte date la ATESTAT
81
el se
ZECI DEMI I : = 11111 + ZECI DEMI I ( m- 1)
end;
function SUTEDEMII (m:integer) : LONGINT ;
begi n
i f m= 37 t hen
SUTEDEMI I : = 111111
el se
SUTEDEMI I : = 111111 + SUTEDEMI I ( m- 1)
end;
function MILIOANE (m:integer) : LONGINT ;
begi n
i f m= 46 t hen
MI LI OANE : = 1111111
el se
MI LI OANE : = 1111111 + MI LI OANE ( m- 1)
end;
function ZECIDEMILIOANE (m:integer) : LONGINT ;
begi n
i f m= 55 t hen
ZECI DEMI LI OANE : = 11111111
el se
ZECI DEMI LI OANE : = 11111111 +
ZECI DEMI LI OANE ( m- 1)
end;
function SUTEDEMILIOANE (m:integer) : LONGINT ;
begi n
i f m= 64 t hen
SUTEDEMI LI OANE : = 111111111
el se
SUTEDEMI LI OANE : = 111111111 +
SUTEDEMI LI OANE ( m- 1)
end;
function MILIARDE (m:integer) : LONGINT ;
begi n
i f m= 73 t hen
MI LI ARDE : = 1111111111
el se
MI LI ARDE : = 1111111111 + MI LI ARDE ( m-
1)
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
Probleme rezolvate de programare Subiecte date la ATESTAT
82
wr i t e ( ' Dat i n = ' ) ;
r eadl n ( n) ;
i f ( n >= 1) and ( n <= 9) t hen
begi n
wr i t el n ( ZECI (n) ) ;
end;
i f ( n >= 10) and ( n <= 18) t hen
begi n
wr i t el n ( SUTE (n) ) ;
end;
i f ( n >= 19) and ( n <= 27) then
begi n
wr i t el n ( MII (n) ) ;
end;
i f ( n >= 28) and ( n <= 36) t hen
begi n
wr i t el n ( ZECIDEMII (n) ) ;
end;
i f ( n >= 37) and ( n <= 45) t hen
begi n
wr i t el n ( SUTEDEMII (n) ) ;
end;
i f ( n >= 46) and ( n <= 54) t hen
begi n
wr i t el n ( MILIOANE (n) ) ;
end;
i f ( n >= 55) and ( n <= 63) t hen
begi n
wr i t el n ( ZECIDEMILIOANE (n) ) ;
end;
i f ( n >= 64) and ( n <= 72) t hen
begi n
wr i t el n ( SUTEDEMILIOANE (n) ) ;
end;
i f ( n = 73) t hen
begi n
wr i t el n ( MILIARDE (n) ) ;
end;
r eadl n
end.
ATESTAT - 2008 - 38 (REPETA 2007 38) Scriei un program care citete de la tastatur un
numr natural "n" (2 < n < 30) i construiete n memorie o matrice patratic n x n,
format numai din valori ce aparin mulimii {1, 2, 3, , n}, astfel nct elementele din matrice
situate pe diagonala secundar s fie egale cu n, elementele situate pe celelalte
dou "semidiagonale" paralele cu diagonala secundar, de o parte i de alta a acesteia, s fie
Probleme rezolvate de programare Subiecte date la ATESTAT
83
egale cu n - 2 etc. Matricea se va afia pe ecran, cte o linie a matricei pe cte o linie a
ecranului, cu spaii ntre elementele fiecrei linii (ca n exemplu).
Exemplu: pentru n = 5, se construiete n memorie i se afieaz matricea :
a11 a12 a13 a14 a15 1 2 3 4 5
a21 a22 a23 a24 a25 2 3 4 5 4
a31 a32 a33 a34 a35 3 4 5 4 3
a41 a42 a43 a44 a45 4 5 4 3 2
a51 a52 a53 a54 a55 5 4 3 2 1
Rezolvare:
Elementele de pe diagonala secundar sunt: a15 a24 a33 a42 a51.
Observm c i = 1 . . 5, iar j = 5 . . 1.
k =0
La aceste elemente, indicii i i j satisfac relaia i + j = 6 =n - k + 1.
---------------------------------------
Elementele de pe diagonala imediat vecin-sus cu diagonala secundar sunt: a14 a23 a32
a41.
Observm c i = 1 . . 4, iar j = 4 . . 1.
k =1
La aceste elemente, indicii i i j satisfac relaia: i + j = 5 =n - k + 1.
---------------------------------------
Elementele de pe urmtoarea diagonal vecin-sus cu diagonala secundar sunt: a13 a22
a31.
Observm c i = 1 . . 3, iar j = 3 . . 1.
k =2
La aceste elemente, indicii i i j satisfac relaia: i + j = 4 =n - k + 1.
---------------------------------------
Elementele de pe urmtoarea diagonal vecin-sus cu diagonala secundar sunt: a12 a21.
Observm c i = 1 . . 2, iar j = 2 . . 1.
k =3
La aceste elemente, indicii i i j satisfac relaia i + j = 3 =n - k + 1.
---------------------------------------
Elementele de pe urmtoarea diagonal vecin-sus cu diagonala secundar sunt: a11.
Observm c i = 1, iar j = 1.
k =4
La aceste elemente, indicii i i j satisfac relaia i + j = 2 =n - k + 1.
---------------------------------------
Pentru diagonalele secundar i cele de DEASUPRA ei, indecii i i j iau valori n
intervalele:
i = 1 . . n k i j = n k . . 1 unde k ia valori de la 0 . . n 1.
Indecii satisfac relaia i + j = n - k + 1
Un element A [i, j] = n - k
---------------------------------------
Elementele de pe diagonala imediat vecin SUB diagonala secundar sunt: a25 a34 a43
a52.
Observm c i = 2 . . 5, iar j = 5 . . 2.
k =2
La aceste elemente, indicii i i j satisfac relaia i + j =7 = n + k.
---------------------------------------
Elementele de pe urmtoarea diagonala vecin SUB diagonala secundar sunt: a35 a44 a53.
Probleme rezolvate de programare Subiecte date la ATESTAT
84
Observm c i = 3 . . 5, iar j = 5 . . 3.
k =3
La aceste elemente, indicii i i j satisfac relaia i + j =8 =n + k.
---------------------------------------
Elementele de pe urmtoarea diagonala vecin SUB diagonala secundar sunt: a45 a54.
Observm c i = 4 . . 5, iar j = 5 . . 4.
k =4
La aceste elemente, indicii i i j satisfac relaia i + j =9 =n + k.
---------------------------------------
Elementele de pe urmtoarea diagonala vecin SUB diagonala secundar sunt: a55.
Observm c i = 5, iar j = 5.
k =5
La aceste elemente, indicii i i j satisfac relaia i + j =10 =n + k.
---------------------------------------
Pentru diagonalele secundar i cele de SUB ea, indecii i i j iau valori n intervalele
i = k . . n j = n . . k
unde k ia valori de la 2 . . n.
Indecii satisfac relaia i + j = n + k
Un element A [i, j] = n - (k - 1).
Program ATESTAT_2008_38_DIAGONALA_SECUNDARA_MATRICE ;
uses CRT;
CONST
nmax = 30;
TYPE
MATRI CE = ar r ay [ 1. . nmax, 1. . nmax] of i nt eger ;
VAR
n, i , j , k : i nt eger ;
A : mat r i ce;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
r epeat
wr i t e ( ' Dat i n = ' ) ;
r eadl n ( n)
unt i l ( n >= 1) AND ( n <= nmax) ;
{ Gener amel ement el e mat r i ci i }
{ El ement el e de DEASUPRA diagonalei secundare}
{- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - }
k : = 0;
whi l e k <= n - 1 do
begi n
i : = 1;
whi l e i <= n - k do
begi n
Probleme rezolvate de programare Subiecte date la ATESTAT
85
j : = n - k;
whi l e j >= 1 do
begi n
i f i + j = n - k + 1 t hen
begi n
A [ i , j ] : = n - k;
{ wr i t e ( ' A [ ' , i , ' , ' , j , ' ] = ' ,
A[ i , j ] ) ;
r eadl n;
}
end;
j : = j - 1
end;
i : = i + 1
end;
k : = k + 1
end;
{ El ement el e de SUB diagonala secundare}
{- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - }
k : = 2;
whi l e k <= n do
begi n
i : = k;
whi l e i <= n do
begi n
j : = n;
whi l e j >= k do
begi n
i f i + j = n + k t hen
begi n
A [ i , j ] : = n - ( k - 1) ;
{ wr i t e ( ' A [ ' , i , ' , ' , j , ' ] = ' ,
A[ i , j ] ) ;
r eadl n;
}
end;
j : = j - 1
end;
i : = i + 1
end;
k : = k + 1
end;
wr i t el n;
Probleme rezolvate de programare Subiecte date la ATESTAT
86
wr i t el n ( ' Mat r i cea est e: ' ) ;
f or i : = 1 t o n do
begi n
f or j : = 1 t o n do
begi n
wr i t e ( A [ i , j ] , ' ' ) ;
end;
wr i t el n
end;
wr i t el n;
r eadl n
end.
ATESTAT - 2008 - 39 (REPETA 2007 39) Scriei un program care citete de la tastatur un
ir "S" de cel mult 30 de caractere i un caracter "C" ; programul realizeaz dublarea fiecrei
apariii a caracterului C n S i apoi scrie noul ir obinut n fiierul text BAC.TXT.
Exemplu: dac se citete de la tastatur irul alfabetar i caracterul "a", atunci fiierul
BAC.TXT va conine irul : aalfaabetaar.
Rezolvare:
Program ATESTAT_2008_39_DUBLAREA_UNUI_CARACTER;
uses CRT;
VAR
c : CHAR ;
si r : STRI NG;
Si r Nou : ar r ay [ 1. . 255] of CHAR ;
l ungi mesi r , i , poz : i nt eger ;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i si r ul S = ' ) ;
r eadl n ( si r ) ;
wr i t el n;
wr i t e ( ' Dat i car act er ul al e car ui apar i t i i t r ebui e dubl at e, c
= ' ) ;
r eadl n ( c) ;
poz : = 0; {pozi t i e i n si r ul al doi l ea }
l ungi mesi r : = LENGTH ( si r ) ;
f or i : = 1 t o l ungi mesi r do
begi n
poz : = poz + 1;
Si r Nou [ poz] : = Si r [ i ] ;
i f Si r [ i ] = c t hen
begi n
poz : = poz + 1;
Si r Nou [ poz] : = c;
Probleme rezolvate de programare Subiecte date la ATESTAT
87
{ wr i t el n ( ' Si r Nou [ ' , poz, ' ] = ' , Si r Nou [ poz] ) ;
}
end;
end;
wr i t el n;
wr i t el n ( ' Noul si r est e : ' ) ;
f or i : = 1 t o poz do
wr i t e ( Si r Nou [ i ] ) ;
r eadl n
end.
ATESTAT - 2008 - 40 (REPETA 2007 40) Scriei un program care citete de la tastatur
dou iruri de caractere, fiecare ir fiind format din cel mult 100 de litere din alfabetul englez, i
care afieaz mesajul DA , dac toate literele din primul ir se gsesc i n cel de-al doilea
ir, nu neaprat n aceeai ordine i de acelai numr de ori, sau mesajul NU , n caz contrar.
Exemplu: dac primul ir este baraca , iar cel de-al doilea ir este abracadabra ,
programul trebuie s afieze mesajul DA , deoarece toate literele primului ir apar n cel de-
al doilea ir.
Rezolvare: Pentru a rezolva mai simplu problema, apelm la date de tip mulime.
Program ATESTAT_2008_40_COMPARARE_SIRURI;
uses CRT;
VAR
si r 1, si r 2 : STRI NG;
l i t er a, c : CHAR;
cuvant , cuvant 1, cuvant 2 : set of CHAR;
i : i nt eger ;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
wr i t e ( ' Dat i pr i mul si r = ' ) ;
r eadl n ( si r 1) ;
wr i t el n;
wr i t e ( ' Dat i al doi l ea si r = ' ) ;
r eadl n ( si r 2) ;
{- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - }
cuvant 1 : = [ ] ; {i ni t i al i zar e multime cuvant1 cu mul t i mea
vi da }
f or i : = 1 t o LENGTH ( si r 1) do
begi n
l i t er a : = si r 1 [ i ] ;
cuvant 1 : = cuvant 1 + [ l i t er a]
{ const r ui mmul t i mea cuvant 1 pr i n reuniune ( oper at or ul
" + " ) }
end;
wr i t el n;
wr i t el n ( ' Si r ul 1 cont i ne l i t er el e : ' ) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
88
f or l i t er a : = ' a' t o ' z' do
begi n
i f l i t er a IN cuvant 1 t hen
wr i t e ( l i t er a, ' ' )
end;
{- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - }
cuvant 2 : = [ ] ; {i ni t i al i zar e multime cuvant2 cu mul t i mea
vi da }
f or i : = 1 t o LENGTH ( si r 2) do
begi n
l i t er a : = si r 2 [ i ] ;
cuvant 2 : = cuvant 2 + [ l i t er a]
{ const r ui mmul t i mea cuvant 2 pr i n reuniune ( oper at or ul
" + " ) }
end;
wr i t el n;
wr i t el n ( ' Si r ul 2 cont i ne l i t er el e : ' ) ;
f or l i t er a : = ' a' t o ' z' do
begi n
i f l i t er a I N cuvant 2 t hen
wr i t e ( l i t er a, ' ' )
end;
wr i t el n;
{- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - }
if cuvant1 <= cuvant2 t hen { t est ar ea INCLUZIUNII cel or doua
mul t i mi }
wr i t el n ( ' TOATE l i t er el e di n pr i mul si r se r egasesc si i n
al doi l ea si r ' )
el se
wr i t el n ( ' NU t oat e l i t er el e di n pr i mul si r se r egasesc si
i n al doi l ea' ) ;
r eadl n
end.
ATESTAT - 2008 - 41 (REPETA 2007 41) Pe prima linie a fiierului text BAC.TXT se afl
un numr natural nenul n (2 <=n < 1000000), iar pe a doua linie a fiierului se afl un ir
format din n numere ntregi, desprite prin cte un spaiu, fiecare numr fiind format din cel
mult 4 cifre. tiind c n fiier exist cel puin un numr strict pozitiv, se cere s se afieze
lungimea maxim a unei secvene din ir, care are proprietatea c este format doar din valori
strict pozitive. O secven a unui ir este format dintr-unul sau mai multe elemente aflate pe
poziii consecutive. Alegei o metod eficient de rezolvare, att ca timp de executare, ct i ca
gestionare a memoriei.
Exemplu: dac fiierul BAC.TXT are coninutul :
10
7 22 - 3 10 3 14 0 21 10
Probleme rezolvate de programare Subiecte date la ATESTAT
89
atunci programul trebuie s afieze pe ecran doar valoarea 3, deoarece, n irul considerat,
exist mai multe secvene care sunt formate doar din numere strict pozitive, dar lungimea
maxim a unei astfel de secvene este 3.
Rezolvare:
Program ATESTAT_2008_41_SECVENTA_A_UNUI_SIR;
uses CRT;
CONST
nmax = 100;
TYPE
vect or = ar r ay [ 1. . nmax] of LONGI NT;
VAR
nr , n, i , t : LONGI NT;
f : t ext ;
si r , l i ni e : STRI NG;
EROARE : i nt eger ; { var i abi l a EROARE di n apel ul pr ocedur i i
VAL }
A : vect or ;
Lung, Lmax : i nt eger ; { Lung si Lmax = lungimea curenta,
r espect i v
lungimea maxima a unei secvent e de numer e }
procedure EXTRAGENUMEREDINFISIERTEXT ( VAR f : t ext ; VAR A :
vect or ; VAR m: LONGI NT) ;
VAR
poz, k : i nt eger ;
numar : LONGI NT;
begi n
RESET ( f ) ;
readln (f, linie); {ci t i m" n" de pe pr i ma l i ni e
a f i si er ul ui }
VAL (linie, m, EROARE); {t r ansf or ma sirul linie i n
numarul m }
readln (f, linie); { ci t i ma doua l i ni e, car e cont i ne
cel e n numer e}
i := 0; { i = i ndex i n vect or ul A }
sir := ''; { i ni t i al i zar e cu si r ul vi d}
{ I n " sir" se ci t est e cat e un numar di n
linie}
poz := 1; { poz = pozi t i e cur ent a i n l i ni a cu numer e
di n f i si er }
whi l e poz < LENGTH ( l i ni e) do
Probleme rezolvate de programare Subiecte date la ATESTAT
90
begi n
k: = poz;
whi l e linie [ k ] <> ' ' do
begi n
sir := sir + linie [ k ];
{concatenare}
k : = k + 1;
end;
poz : = k;
i : = i + 1;
VAL (sir, numar, EROARE); {t r ansf or ma sir i n
numar }
A [ i ] : = numar ;
si r : =' ' ;
poz : = poz + 1;
end;
end;
Begin { PROGRAM PRINCIPAL }
CLRSCR;
ASSI GN ( f , ' C: \ BAC. t xt ' ) ;
r ewr i t e ( f ) ; {gener amf i si er ul t ext }
wr i t el n ( ' I nt r oducet i , i n f i si er ul BAC. TXT, n numer e
I NTREGI cu cel mul t 9 ci f r e' ) ;
wr i t el n;
wr i t e ( ' Cat e numer e dor i t i sa i nt r oducet i , n = ' ) ;
r eadl n ( n) ;
wr i t el n;
writeln (f, n); {Se scr i e n pe pr i ma l i ni e di n f i si er ul
t ext }
l i ni e : = ' ' ; {i ni t i al i zar e var i abi l a " linie" cu si r ul vi d}
{" l i ni e" va cont i ne cel e n numer e separ at e de
spat i i }
{" l i ni e" = un STRI NG car e se va i nt r oduce i n
f i si er ul t ext }
f or i : = 1 t o n do
begi n
wr i t e ( ' Dat i un numar I NTREG, nr = ' ) ;
r eadl n ( nr ) ;
STR (nr, sir); {t r ansf or mamnumar ul nr i n sir,
pent r u a- l memor a
Probleme rezolvate de programare Subiecte date la ATESTAT
91
i n f i si er ul t ext }
linie := linie + sir + ' '; {const r ui m" l i ni e" pr i n
concat enar e}
end; {scr i i nd un " spat i u" dupa
f i ecar e si r }
writeln (f, linie); {scr i em" l i ni e" i n f i si er ul t ext }
wr i t el n;
wr i t el n ( ' Fi si er ul t ext ar e cont i nut ul : ' ) ;
RESET ( f ) ;
whi l e not EOF ( f ) do
begi n
READLN ( f , l i ni e) ;
wr i t el n ( l i ni e) ;
end;
wr i t el n;
{ Di n STRI NG- ul " l i ni e" t r ebui e sa ext r agemnumer el e }
EXTRAGENUMEREDINFISIERTEXT ( f, A, n );
wr i t el n;
wr i t el n ( ' Numar ul n ci t i t de pe pr i ma l i ni e est e n = ' , n) ;
wr i t el n;
wr i t el n ( 'Vectorul A, r ezul t at dupa ext r ager e est e: ' ) ;
f or i : = 1 t o n do
begi n
wr i t el n ( ' A [ ' , i , ' ] = ' , A[ i ] ) ;
end;
wr i t el n;
Lmax := 0; { i ni t i al i zar e Lmax }
Lung := 0; { i ni t i al i zar e lungime curenta a unei secvente }
f or i : = 1 t o n do
begi n
i f A [ i ] > 0 t hen
begi n
Lung : = Lung + 1;
end
el se
begi n
i f Lung > Lmax t hen
Lmax := Lung;
Lung : = 0
Probleme rezolvate de programare Subiecte date la ATESTAT
92
end;
end;
wr i t el n;
wr i t el n ( ' Lungi mea maxi ma a unei secvent e de numer e
pozi t i ve, Lmax =' , Lmax) ;
r eadl n;
CLRSCR;
end.
ATESTAT - 2008 - 42 Se citesc de la tastatur patru numerere naturale nenule A, B, C, D.
Scriei un program care s afieze rezultatul expresiei A / B + C / D sub forma unei fracii
ireductibile.
Rezolvare:
program ATESTAT_2008_42_FRACTII_IREDUCTIBILE;
uses CRT;
function CMMDC (A1, B1 : integer) : integer;
var
A1i ni t , B1i ni t , aux, R : i nt eger ;
begi n
A1i ni t : = A1;
B1i ni t : = B1;
i f A1i ni t < B1i ni t t hen
begi n
aux : = A1i ni t ;
A1i ni t : = B1i ni t ;
B1i ni t : = aux
end;
R : = A1i ni t MOD B1i ni t ;
whi l e R <> 0 do
begi n
A1i ni t : = B1i ni t ;
B1i ni t : = R;
R : = A1i ni t MOD B1i ni t
end;
CMMDC : = B1i ni t ;
end;
VAR
A, A1, B, B1, C, C1, D, D1 : i nt eger ;
numar at or , numar at or 1, numi t or , numi t or 1, numar at or 2,
numi t or 2 : i nt eger ;
begi n {PROGRAM PRI NCI PAL}
Probleme rezolvate de programare Subiecte date la ATESTAT
93
CLRSCR;
wr i t el n;
wr i t el n;
wr i t e ( ' Dat i A = ' ) ;
r eadl n ( A) ;
wr i t el n;
wr i t e ( ' Dat i B = ' ) ;
r eadl n ( B) ;
wr i t el n;
wr i t e ( ' Dat i C = ' ) ;
r eadl n ( C) ;
wr i t el n;
wr i t e ( ' Dat i D = ' ) ;
r eadl n ( D) ;
wr i t el n;
wr i t el n ( ' CMMDC ( A, B) = CMMDC ( ' , A, ' , ' , B, ' ) = ' , CMMDC
( A, B) ) ;
wr i t el n;
wr i t el n ( ' CMMDC ( C, D) = CMMDC ( ' , C, ' , ' , D, ' ) = ' , CMMDC
( C, D) ) ;
wr i t el n;
A1 : = A DI V CMMDC ( A, B) ;
B1 : = B DI V CMMDC ( A, B) ;
C1 : = C DI V CMMDC ( C, D) ;
D1 : = D DI V CMMDC ( C, D) ;
numar at or : = A * D + B * C ;
numi t or : = B * D;
numar at or 1 : = A1 * D1 + B1 * C1 ;
numi t or 1 : = B1 * D1;
numar at or 2 : = numar at or 1 DI V CMMDC ( numar at or 1, numi t or 1) ;
numi t or 2 : = numi t or 1 DI V CMMDC ( numar at or 1, numi t or 1) ;
wr i t el n ( ' Fr act i a I REDUCTI BI LA obt i nut a di n ' ) ;
wr i t el n;
wr i t el n ( A, ' / ' , B, ' + ' , C, ' / ' , D, ' = ' ,
A1, ' / ' , B1, ' + ' , C1, ' / ' , D1, ' = ' ,
numar at or , ' / ' , numi t or , ' = ' , numar at or 1, ' / ' , numi t or 1,
' = ' ,
Probleme rezolvate de programare Subiecte date la ATESTAT
94
numar at or 2, ' / ' , numi t or 2 ) ;
r eadl n
end.
ATESTAT - 2008 - 43 Se citesc de la tastatur patru numerere naturale nenule A, B, C, D.
Scriei un program care s afieze rezultatul expresiei A / B * C / D sub forma unei fracii
ireductibile.
{ Se citesc de la tastatura patru numerere naturale nenule A, B, C, D. }
{ Scrieti un program care sa afiseze rezultatul expresiei A / B * C / D }
{ sub forma unei fractii ireductibile. }
Rezolvare:
program ATESTAT_2008_43_FRACTII_IREDUCTIBILE;
uses CRT;
f unct i on CMMDC ( A1, B1 : i nt eger ) : i nt eger ;
var
A1i ni t , B1i ni t , aux, R : i nt eger ;
begi n
A1i ni t : = A1;
B1i ni t : = B1;
i f A1i ni t < B1i ni t t hen
begi n
aux : = A1i ni t ;
A1i ni t : = B1i ni t ;
B1i ni t : = aux
end;
R : = A1i ni t MOD B1i ni t ;
whi l e R <> 0 do
begi n
A1i ni t : = B1i ni t ;
B1i ni t : = R;
R : = A1i ni t MOD B1i ni t
end;
CMMDC : = B1i ni t ;
end;
VAR
A, A1, B, B1, C, C1, D, D1 : i nt eger ;
numar at or , numar at or 1, numi t or , numi t or 1, numar at or 2,
numi t or 2 : i nt eger ;
begi n {PROGRAM PRI NCI PAL}
CLRSCR;
wr i t el n;
wr i t el n;
Probleme rezolvate de programare Subiecte date la ATESTAT
95
wr i t e ( ' Dat i A = ' ) ;
r eadl n ( A) ;
wr i t el n;
wr i t e ( ' Dat i B = ' ) ;
r eadl n ( B) ;
wr i t el n;
wr i t e ( ' Dat i C = ' ) ;
r eadl n ( C) ;
wr i t el n;
wr i t e ( ' Dat i D = ' ) ;
r eadl n ( D) ;
wr i t el n;
wr i t el n ( ' CMMDC ( A, B) = CMMDC ( ' , A, ' , ' , B, ' ) = ' , CMMDC
( A, B) ) ;
wr i t el n;
wr i t el n ( ' CMMDC ( C, D) = CMMDC ( ' , C, ' , ' , D, ' ) = ' , CMMDC
( C, D) ) ;
wr i t el n;
A1 : = A DI V CMMDC ( A, B) ;
B1 : = B DI V CMMDC ( A, B) ;
C1 : = C DI V CMMDC ( C, D) ;
D1 : = D DI V CMMDC ( C, D) ;
numar at or : = A * C ;
numi t or : = B * D;
numar at or 1 : = A1 * C1 ;
numi t or 1 : = B1 * D1;
numar at or 2 : = numar at or 1 DI V CMMDC ( numar at or 1, numi t or 1) ;
numi t or 2 : = numi t or 1 DI V CMMDC ( numar at or 1, numi t or 1) ;
wr i t el n ( ' Fr act i a I REDUCTI BI LA obt i nut a di n ' ) ;
wr i t el n;
wr i t el n ( A, ' / ' , B, ' * ' , C, ' / ' , D, ' = ' ,
A1, ' / ' , B1, ' * ' , C1, ' / ' , D1, ' = ' ,
numar at or , ' / ' , numi t or , ' = ' , numar at or 1, ' / ' , numi t or 1,
' = ' ,
numar at or 2, ' / ' , numi t or 2 ) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
96
r eadl n
end.
ATESTAT - 2008 - 44 Se citesc de la tastatur patru numerere naturale nenule A, B, C, D cu
care se formeaz numerele complexe Z = (A, B) i T = (C, D). Scriei un program care s
afieze pe ecran rezultatul expresiei Z * T.
Rezolvare:
{ Z =A +j B, iar T =C +j D }
Program ATESTAT_2008_44_NUMERE_COMPLEXE_1;
uses CRT;
VAR
A, B, C, D : i nt eger ;
begi n {PROGRAM PRI NCI PAL}
CLRSCR;
wr i t el n;
wr i t el n;
wr i t e ( ' Dat i A = ' ) ;
r eadl n ( A ) ;
wr i t el n;
wr i t e ( ' Dat i B = ' ) ;
r eadl n ( B ) ;
wr i t el n;
wr i t e ( ' Dat i C = ' ) ;
r eadl n ( C ) ;
wr i t el n;
wr i t e ( ' Dat i D = ' ) ;
r eadl n ( D ) ;
wr i t el n;
wr i t el n ( ' Z = A + j * B = ' , A, ' + j * ' , B) ;
wr i t el n;
wr i t el n ( ' T = C + j * D = ' , C, ' + j * ' , D) ;
wr i t el n;
wr i t el n ( ' Z * T = ( AC - BD) + j ( AD + BC) = ' , A*C - B*D, ' +
j * ' , A*D + B*C) ;
wr i t el n;
r eadl n
Probleme rezolvate de programare Subiecte date la ATESTAT
97
end.
ATESTAT - 2008 - 45 Se citesc de la tastatur patru numerere naturale nenule A, B, C, D cu
care se formeaz numerele complexe Z = (A, B) i T = (C, D). Scriei un program care s
afieze pe ecran rezultatul expresiei Z / T.
Rezolvare:
{ Z =A +j B, iar T =C +j D }
{ Vom inmulti si numitorul si numaratorul cu aceeasi cantitate =C - j D}
{ Astfel, numitorul nu mai este complex }
Program ATESTAT_2008_45_NUMERE_COMPLEXE_2;
uses CRT;
VAR
A, B, C, D : i nt eger ;
Par t eaReal a, Par t eaI magi nar a : r eal ;
begi n {PROGRAM PRI NCI PAL}
CLRSCR;
wr i t el n;
wr i t el n;
wr i t e ( ' Dat i A = ' ) ;
r eadl n ( A ) ;
wr i t el n;
wr i t e ( ' Dat i B = ' ) ;
r eadl n ( B ) ;
wr i t el n;
wr i t e ( ' Dat i C = ' ) ;
r eadl n ( C ) ;
wr i t el n;
wr i t e ( ' Dat i D = ' ) ;
r eadl n ( D ) ;
wr i t el n;
wr i t el n ( ' Z = A + j * B = ' , A, ' + j * ' , B) ;
wr i t el n;
wr i t el n ( ' T = C + j * D = ' , C, ' + j * ' , D) ;
wr i t el n;
Par t eaReal a : = ( A*C + B*D) / ( C*C + D*D) ;
Par t eaI magi nar a : = ( B*C - A*D) / ( C*C + D*D) ;
Probleme rezolvate de programare Subiecte date la ATESTAT
98
wr i t el n ( ' Z / T = ( AC + BD) / ( C*C + D*D) + j ( BC - AD) / ( C*C
+ D*D) = ' ,
Par t eaReal a: 4: 2, ' + j * ' , Par t eaI magi nar a: 4: 2) ;
wr i t el n;
r eadl n
end.

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