Sunteți pe pagina 1din 43

Cuprins

Problema 1. irul Fibonacci....................................................................................................2


Problema 2. irul Fibonacci modificat...................................................................................4
Problema 3. Suma numerelor 1,5,9,13,...................................................................................6
Problema 4. Suma numerelor 1,6,11,16,21,............................................................................7
Problema 5. Algoritmul lui Euclid (2 numere)......................................................................8
Problema 6. Algoritmul lui Euclid (3 numere)......................................................................9
Problema 7. Recursivitatea indirect f(x) i g(x).................................................................10
Problema 8. Recursivitatea indirect a(x) i b(x)................................................................11
Problema 9. Recursivitatea indirect a(x), b(x) i c(x)........................................................12
Problema 10. Recursivitatea indirect an i bn.....................................................................13
Problema 11. Recursivitatea indirect an, bn i cn................................................................14
Problema 12. Ackerman.......................................................................................................15
Problema 13. Hermite............................................................................................................16
Problema 14. Cebev.............................................................................................................17
Problema 15. Manna Pnueli 1...............................................................................................18
Problema 16. Manna Pnueli 2...............................................................................................19
Problema 17. Turnurile Hanoi..............................................................................................20
Problema 18. Compoziia funciilor.....................................................................................21
Problema 19. Algoritmul lui Euclid pentru n numere........................................................22
Problema 20. Generarea partiiilor unui numar natural...................................................23
Problema 21. Permutri........................................................................................................24
Problema 22. Aranjamente....................................................................................................25
Problema 22. Combinri........................................................................................................27
Problema 24. Problema damelor...........................................................................................28
Problema 25. Problema Comis_Voiajorului........................................................................29
Problema 26. Produsul cartezian..........................................................................................31
Problema 27. Problema colorrii hrilor............................................................................33
Problema 28. Problema labirintului.....................................................................................36
Problema 29. Algoritmul de umplere a unei suprafee nchise (FILL).............................39
Problema 30. Problema fotografiei (aplicaie FILL)..........................................................41

Problema 1. irul Fibonacci


Se cere de scris un program n Pascal i C varianta recursiv i nerecursiv a
irului de numere Fibonacci:1,1,2,3,5,8,...21. S se afle raportul f(n) / f(n-1).
Pascal recursiv
Pascal nerecursiv
Program Fib_rec;
var i,n:integer;
function fib(k:integer):longint;
begin
if (k=1) or (k=2) then fib:=1 else
fib:=fib(k-1)+fib(k-2);
end;
begin
writeln('Introduceti n');
readln(n);
for i:=3 to n do
begin
writeln('Fib[',i,']=',fib(i));
writeln('R[',i,']=',fib(i)/fib(i1):10:10);
end;
readln;
end.
Introduceti n
15
Fib[3]=2
R[3]=2.0000000000
Fib[4]=3
R[4]=1.5000000000
Fib[5]=5
R[5]=1.6666666667
Fib[6]=8
R[6]=1.6000000000
Fib[7]=13
R[7]=1.6250000000
Fib[8]=21
R[8]=1.6153846154
Fib[9]=34
R[9]=1.6190476190
Fib[10]=55
R[10]=1.6176470588
Fib[11]=89
R[11]=1.6181818182
Fib[12]=144
R[12]=1.6179775281
Fib[13]=233
R[13]=1.6180555556
Fib[14]=377
R[14]=1.6180257511
Fib[15]=610
R[15]=1.6180371353

Program Fib_nerec;
var i,n:integer;
f:array[1..100] of longint;
begin
f[1]:=1;
f[2]:=1;
writeln('Introduceti n:');
readln(n);
for i:=3 to n do begin
f[i]:=f[i-1]+f[i-2];
writeln('Fib[',i,']=',f[i]);
writeln('R[',i,']=',f[i]/f[i1]:10:10);
end;
readln;
end.
Introduceti n:
15
Fib[3]=2
R[3]=2.0000000000
Fib[4]=3
R[4]=1.5000000000
Fib[5]=5
R[5]=1.6666666667
Fib[6]=8
R[6]=1.6000000000
Fib[7]=13
R[7]=1.6250000000
Fib[8]=21
R[8]=1.6153846154
Fib[9]=34
R[9]=1.6190476190
Fib[10]=55
R[10]=1.6176470588
Fib[11]=89
R[11]=1.6181818182
Fib[12]=144
R[12]=1.6179775281
Fib[13]=233
R[13]=1.6180555556
Fib[14]=377
R[14]=1.6180257511
Fib[15]=610
R[15]=1.6180371353

C recursiv

C nerecursiv

#include <stdio.h>
#include <conio.h>
int k,n;
int fib(int k)
{
if (k<2) return 1;
else return (fib(k-1)+fib(k2));
}
int main(){
printf("Introduceti n: ");
scanf("%d",&n);
printf("Primele n numere
Fibonacci sunt ");
for(k=0;k<n;k++){
printf("Fib[%d]= %d
\n",k,fib(k));
printf("R[%d]= %0.20f \n",k,
(double)fib(k)/fib(k-1));
}
getch();
}

#include <stdio.h>
#include <conio.h>
int main(){
int f[40];
int n=0;
int k;
printf("introdu n: ");
scanf("%d",&n);
f[0]=1;
f[1]=1;
for(k=2;k<n;k++)
f[k]=f[k-1]+f[k-2];
printf("Sirul Fibonacci
este: ");
for(k=0;k<n;k++){
printf("Fib[%d]= %d
\n",k,f[k]);
printf("R[%d]= %0.20f \n",k,
(double)f[k]/f[k-1]);
}
getch();
return 0;
}

Introduceti n: 15
Primele n numere Fibonacci sunt
Fib[0]= 1
R[0]= 1.00000000000000000000
Fib[1]= 1
R[1]= 1.00000000000000000000
Fib[2]= 2
R[2]= 2.00000000000000000000
Fib[3]= 3
R[3]= 1.50000000000000000000
Fib[4]= 5
R[4]= 1.66666666666666670000
Fib[5]= 8
R[5]= 1.60000000000000010000
Fib[6]= 13
R[6]= 1.62500000000000000000
Fib[7]= 21
R[7]= 1.61538461538461540000
Fib[8]= 34
R[8]= 1.61904761904761910000
Fib[9]= 55
R[9]= 1.61764705882352940000
Fib[10]= 89
R[10]= 1.61818181818181820000
Fib[11]= 144
R[11]= 1.61797752808988760000
Fib[12]= 233
R[12]= 1.61805555555555560000
Fib[13]= 377
R[13]= 1.61802575107296140000
Fib[14]= 610
R[14]= 1.61803713527851460000

Introduceti n: 15
Primele n numere Fibonacci sunt
Fib[0]= 1
R[0]= 0.06666666666666666600
Fib[1]= 1
R[1]= 1.00000000000000000000
Fib[2]= 2
R[2]= 2.00000000000000000000
Fib[3]= 3
R[3]= 1.50000000000000000000
Fib[4]= 5
R[4]= 1.66666666666666670000
Fib[5]= 8
R[5]= 1.60000000000000010000
Fib[6]= 13
R[6]= 1.62500000000000000000
Fib[7]= 21
R[7]= 1.61538461538461540000
Fib[8]= 34
R[8]= 1.61904761904761910000
Fib[9]= 55
R[9]= 1.61764705882352940000
Fib[10]= 89
R[10]= 1.61818181818181820000
Fib[11]= 144
R[11]= 1.61797752808988760000
Fib[12]= 233
R[12]= 1.61805555555555560000
Fib[13]= 377
R[13]= 1.61802575107296140000
Fib[14]= 610
R[14]= 1.61803713527851460000

Problema 2. irul Fibonacci modificat.


Se cere de scris un program n Pascal i C,varianta recursiv i nerecursiv, care
afieaz la ecran irul de numere: 1,1,1,2,3,7,23,164....
Pascal recursiv
Pascal nerecursiv
Program PR2_rec;
var i,n:integer;
function f(k:integer):longint;
begin
if (k=1) or (k=2) or (k=3) then
f:=1 else f:=(f(k-1)*f(k-2))+f(k3);
end;
begin
writeln('Introduceti n');
readln(n);
writeln('Primele ',n,' numere sunt:
');
for i:=4 to n do
begin
writeln(i,': ',f(i):3);
writeln('R[',i,']: ',f(i)/f(i1):10:10);
end;
readln;
end.

Program PR2_nerec;
var i,n:integer;
f:array[1..100] of longint;
begin
f[1]:=1;
f[2]:=1;
f[3]:=1;
writeln('Introduceti n:');
readln(n);
for i:=4 to n do
f[i]:=f[i-1]*f[i-2]+f[i-3];
writeln('Primele ',i,' numere sunt:
');
for i:=4 to n do begin
writeln(i,': ',f[i]);
writeln('R[',i,')=',f[i]/f[i1]:10:10);
end;
readln;
end.

Introduceti n
15
Primele 15 numere sunt:
4:
2
R[4]: 2.0000000000
5:
3
R[5]: 1.5000000000
6:
7
R[6]: 2.3333333333
7: 23
R[7]: 3.2857142857
8: 164
R[8]: 7.1304347826
9: 3779
R[9]: 23.0426829268
10: 619779
R[10]: 164.0060862662
11: -1952822291
R[11]: -3150.8364933307
12: -757644406
R[12]: 0.3879740668
13: -786404923
R[13]: 1.0379604426
14: 825548575
R[14]: -1.0497754412
15: -1830113947
R[15]: -2.2168458676

Introduceti n:
15
Primele 15 numere sunt:
4: 2
R[4)=2.0000000000
5: 3
R[5)=1.5000000000
6: 7
R[6)=2.3333333333
7: 23
R[7)=3.2857142857
8: 164
R[8)=7.1304347826
9: 3779
R[9)=23.0426829268
10: 619779
R[10)=164.0060862662
11: -1952822291
R[11)=-3150.8364933307
12: -757644406
R[12)=0.3879740668
13: -786404923
R[13)=1.0379604426
14: 825548575
R[14)=-1.0497754412
15: -1830113947
R[15)=-2.2168458676

C recursiv

C nerecursiv

#include <stdio.h>
#include <conio.h>
int k,n;
int f(int k)
{
if (k<3) return 1;
else return (f(k-1)*f(k2)+f(k-3));
}
int main(){
printf("Introduceti n:\n");
scanf("%d",&n);
printf("Primii %d termeni
sunt: ",n);
for(k=0;k<=n;k++){
printf("%d: %d \n",k,f(k));
printf("R[%d]: %0.20f \n",k,
(double)f(k)/f(k-1));
}
getch();
}

#include <stdio.h>
#include <conio.h>
int main(){
int f[40];
int n=0;
int k;
printf("Introduceti n:\n");
scanf("%d",&n);
f[0]=1;
f[1]=1;
f[2]=1;
for(k=3;k<=n;k++)
f[k]=(f[k-1]*f[k-2])
+f[k-3];
printf("Primii %d termeni
sunt: ",n);
for(k=0;k<=n;k++){
printf("%d: %d \n",k,f[k]);
printf("R[%d]: %0.20f \n",k,
(double)f[k]/f[k-1]);
}
getch();
return 0;
}
Introduceti n:
15
Primii 15 termeni sunt: 0: 1
R[0]: 0.06666666666666666600
1: 1
R[1]: 1.00000000000000000000
2: 1
R[2]: 1.00000000000000000000
3: 2
R[3]: 2.00000000000000000000
4: 3
R[4]: 1.50000000000000000000
5: 7
R[5]: 2.33333333333333350000
6: 23
R[6]: 3.28571428571428560000
7: 164
R[7]: 7.13043478260869530000
8: 3779
R[8]: 23.04268292682926900000
9: 619779
R[9]: 164.00608626620800000000
10: -1952822291
R[10]: -3150.83649333068710000000
11: -757644406
R[11]: 0.38797406681179675000
12: -786404923
R[12]: 1.03796044261956830000
13: 825548575
R[13]: -1.04977544119468850000
14: -1830113947
R[14]: -2.21684586760991030000
15: -1776655360
R[15]: 0.97078947620303557000

Introduceti n:
15
Primii 15 termeni sunt: 0: 1
R[0]: 1.00000000000000000000
1: 1
R[1]: 1.00000000000000000000
2: 1
R[2]: 1.00000000000000000000
3: 2
R[3]: 2.00000000000000000000
4: 3
R[4]: 1.50000000000000000000
5: 7
R[5]: 2.33333333333333350000
6: 23
R[6]: 3.28571428571428560000
7: 164
R[7]: 7.13043478260869530000
8: 3779
R[8]: 23.04268292682926900000
9: 619779
R[9]: 164.00608626620800000000
10: -1952822291
R[10]: -3150.83649333068710000000
11: -757644406
R[11]: 0.38797406681179675000
12: -786404923
R[12]: 1.03796044261956830000
13: 825548575
R[13]: -1.04977544119468850000
14: -1830113947
R[14]: -2.21684586760991030000
15: -1776655360
R[15]: 0.97078947620303557000

Problema 3. Suma numerelor 1,5,9,13,...


Se cere de scris un program n Pascal i C,varianta recursiv i nerecursiv, care
afieaz la ecran suma primelor n termeni ai irului de numere:
1,5,9,13,17,...,4n-3,...
0, n 0

S (n 1) 4 * n 3

S(n)=
Pascal recursiv

Pascal nerecursiv

Program PR3_rec;
var n:integer;
function sum(k:integer):longint;
begin
if (k=0) then sum:=0 else
sum:=sum(k-1)+4*k-3;
end;
begin
writeln('Introduceti n: ');
readln(n);
writeln('Suma este: ',sum(n));
readln;
end.
Introduceti n:
15
Suma este: 435

Program PR3_nerec;
var i,n,sum:integer;
begin
writeln('Introduceti n: ');
readln(n);
sum:=0;
for i:=1 to n do
sum:=sum+4*i-3;
writeln('Suma este: ',sum);
readln;
end.

C recursiv

C nerecursiv

#include <stdio.h>
#include <conio.h>
int k,n;
int sum(int k)
{
if (k==0) return 0;
else return sum(k-1)+4*k-3;
}
int main(){
printf("Introduceti n:
\n");
scanf("%d",&n);
printf("Suma este:
%d",sum(n));
getch();
}

#include <stdio.h>
#include <conio.h>
int main(){
int n,sum,k,x;
printf("Introduceti n: \n");
scanf("%d",&n);
sum=0;
k=1;
x=4*n-3;
while(k<=x)
{
sum=sum+k;
k=k+4;
}
printf("Suma este: %d",sum);
getch();
return 0;
}
Introduceti n:
15
Suma este: 435

Introduceti n:
15
Suma este: 435

Introduceti n:
15
Suma este: 435

Problema 4. Suma numerelor 1,6,11,16,21,...


Este dat irul de numere 1,6,11,16,21,.S se determine suma primelor n
numere a irului dat. S se alcatuiasc varianta nerecursiv i recursiv n Pascal
i C.
S(n)=

1, n 0

5 * n 4 S (n 1)

Pascal recursiv

Pascal nerecursiv

Program suma1611_rec;
var n,k:integer;
function sum(k:integer):integer;
begin
if k=0 then sum:=0
else sum:=sum(k-1)+5*k-4;
end;
begin
writeln('Introduceti n: ');
readln(n);
writeln('Suma primelor ',n,'
termeni este ',sum(n));
readln;
end.
Introduceti n:
15
Suma primelor 15 termeni este 540

Program suma1611_nerec;
var i,n,sum:integer;
begin
writeln('Introduceti n: ');
readln(n);
sum:=0;
for i:=1 to n do
sum:=sum+5*i-4;
writeln('Suma primelor ',n,'
termeni este ',sum);
readln;
end.

C recursiv

C nerecursiv

#include <conio.h>
#include <stdio.h>
int sum(int k){
if (k==0) return 0;
else return sum(k-1)+5*k-4;
}
int main(){
int n;
printf("Introduceti n: \n");
scanf("%d",&n);
printf("Suma primelor %d numere
este: %d",n,sum(n));
getch();
}

Introduceti n:
15
Suma primelor 15 numere este: 540

Introduceti n:
15
Suma primelor 15 termeni este 540

#include <conio.h>
#include <stdio.h>
int main(){
int n,sum,k,x;
printf("Introduceti n: \n");
scanf("%d",&n);
sum=0;
k=1;
x=5*n-4;
while(k<=x)
{
sum=sum+k;
k=k+5;
}
printf("Suma primelor %d numere
este: %d",n,sum);
getch();
}
Introduceti n:
15
Suma primelor 15 numere este: 540

Problema 5. Algoritmul lui Euclid (2 numere)


S se determine cel mai mare divizor comun a dou numere naturale. S se scrie varianta
recursiv i nerecursiv n Pascal i C.
a, a b

cmmdc(a b, b), a b
cmmdc(a, b a), b a

CMMDC(a,b)=

Pascal recursiv

Pascal nerecursiv

Program Euclid_rec;
var a,b:integer;
function
cmmdc(a,b:integer):integer;
begin
if a=b then cmmdc:=a else
if a>b then cmmdc:=cmmdc(a-b,b)
else
cmmdc:=cmmdc(a,b-a);
end;
begin
writeln('Introduceti a si b: ');
readln(a,b);
writeln('Cmmdc este: ',cmmdc(a,b));
readln;
end.
Introdu a si b:
75 25
Cmmdc este: 25

Program Euclid_nerec;
var a,b:integer;
begin
writeln('Introduceti a si b: ');
readln(a,b);
while a<>b do
if a>b then a:=a-b else if
a<b then b:=b-a;
writeln('Cmmdc este: ',a);
readln;
end.

C recursiv

C nerecursiv

#include<conio.h>
#include<stdio.h>
int cmmdc(int a, int b){
if (a==b) return a; else
if (a>b) return cmmdc(a-b,b);
else if (a<b) return
cmmdc(a,b-a);
}
int main(){
int a=0,b=0;
printf("Introduceti a si
b: \n");
scanf("%d %d",&a,&b);
printf("Cmmdc(%d,%d)=%d",a,b,
cmmdc(a,b));
getch();
}
Introduceti a si b:
75 25
Cmmdc(75,25)=25

Introduceti a si b:
75 25
Cmmdc este: 25

#include<conio.h>
#include<stdio.h>
int main(){
int a=0,b=0;
printf("Introduceti a si b
\n");
scanf("%d %d",&a,&b);
do{
if (a>b) a=a-b;
else if (a<b) b=b-a;
}
while(a!=b);
printf("Cmmdc=%d",a);
getch();
}
Introduceti a si b
75 25
Cmmdc=25

Problema 6. Algoritmul lui Euclid (3 numere).


S se determine cel mai mare divizor comun a trei numere naturale. S se scrie varianta
recursiv i nerecursiv n Pascal i C.
a, a b

cmmdc(a b, b), a b
cmmdc(a, b a), b a

CMMDC(a,b)=

Pascal recursiv

Pascal nerecursiv

Program Euclid3_rec;
var a,b,c:integer;
function
cmmdc(a,b:integer):integer;
begin
if a=b then cmmdc:=a else
if a>b then cmmdc:=cmmdc(a-b,b)
else
cmmdc:=cmmdc(a,b-a);
end;
begin
writeln('Introduceti a, b si c: ');
readln(a,b,c);
writeln('Cmmdc este:
',cmmdc(cmmdc(a,b),c));
readln;
end.
Introdu a, b si c:
85 75 25
Cmmdc este: 5

Program Euclid3_nerec;
var a,b,c:integer;
begin
writeln('Introduceti a, b si c: ');
readln(a,b,c);
while a<>b do
if a>b then a:=a-b else if
a<b then b:=b-a;
while a<>c do
if a>b then a:=a-c else if
a<c then c:=c-a;
writeln('Cmmdc este: ',a);
readln;
end.

C recursiv

C nerecursiv

#include<conio.h>
#include<stdio.h>
int cmmdc(int a, int b){
if (a==b) return a; else
if (a>b) return cmmdc(a-b,b);
else if (a<b) return
cmmdc(a,b-a);
}
int main(){
int a=0,b=0,c=0;
printf("Introduceti a, b si
c: \n");
scanf("%d %d %d",&a,&b,&c);
printf("Cmmdc(%d,%d,%d)=
%d",a, b,c,cmmdc(cmmdc(a,b),c));
getch();
}

#include<conio.h>
#include<stdio.h>
int main(){
int a=0,b=0,c=0;
printf("Introduceti a, b si c
\n");
scanf("%d %d %d",&a,&b,&c);
do{
if (a>b) a=a-b;
else if (a<b) b=b-a;
}
while(a!=b);
do{
if (a>c) a=a-c;
else if (a<c) c=c-a;
}
while(a!=c);
printf("Cmmdc=%d",a);
getch();
}
Introdu a, b si c

Introduceti a, b si c:

Introduceti a, b si c:
85 75 25
Cmmdc este: 5

85 75 25
Cmmdc(85,75,25)=5

85 75 25
Cmmdc=5

Problema 7. Recursivitatea indirect f(x) i g(x).


Calcularea valorilor a dou funcii f(x) i g(x). S se alcatuiasc un program n Pascal i C
utiliznd recursia indirect pentru a calcula valorile funciilor
g ( x 1), x 1
f ( x) 1, x 0

x 2, x 1
x, x 0
f(x)=
i g(x)=

Pascal

Program f_g_x_Indirect;
var x:integer;
function f(x:integer):integer;
forward;
function g(x:integer):integer;
begin
if x>=0 then g:=f(x)+1
else g:=-x;
end;
function f(x:integer):integer;
begin
if x<=1 then f:=x+2
else f:=g(x-1);
end;
begin
writeln('Introduceti x: ');
readln(x);
writeln('f(',x,')= ',f(x),' si
g(',x,')= ',g(x));
readln;
end.
Introduceti x:
9
f(9)= 11 si g(9)= 12

#include <conio.h>
#include <stdio.h>
#include <math.h>
int g(int x);
int f(int x){
if (x>1) return g(x-1);
else return x+2;
}
int g(int x){
if (x>=0) return f(x)+1;
else return -x;
}
int main(){
int x;
printf("Introduceti x: \n");
scanf("%d",&x);
printf("f(%d)= %d si g(%d)=
%d",x,f(x),x,g(x));
getch();
}
Introduceti x:
9
f(9)= 11 si g(9)= 12

10

Problema 8. Recursivitatea indirect a(x) i b(x).


S se alctuiasc un program n Pascal i C pentru calcularea valorilor funciilor:
2
a( x) 5, x 0
x , x 3
2

b( x 2), x 3
x 1 , x 0
a(x)=
si b(x)=

Pascal

Program suma1611_nerec;
var x:integer;
function a(x:integer):integer;
forward;
function b(x:integer):integer;
begin
if x<0 then b:=sqr(x+1)
else if x>=0 then b:=a(x)+5;
end;
function a(x:integer):integer;
begin
if x<=3 then a:=sqr(x)
else a:=b(x-2);
end;
begin
writeln('Introduceti x: ');
readln(x);
writeln('a(',x,')= ',a(x),' si
b(',x,')= ',b(x));
readln;
end.

#include <conio.h>
#include <stdio.h>
#include <math.h>
int b(int x);
int a(int x){
if (x<=3) return pow(x,2);
else return b(x-2);
}
int b(int x){
if (x<0) return pow(x+1,2);
else return a(x)+5;
}
int main(){
int x;
printf("Introduceti x: \n");
scanf("%d",&x);
printf("a(%d)= %d si b(%d)=
%d",x,a(x),x,b(x));
getch();
}

Introduceti x:
9
a(9)= 24 si b(9)= 29

Introduceti x:
9
a(9)= 24 si b(9)= 29

11

Problema 9. Recursivitatea indirect a(x), b(x) i c(x).


Aplicnd recursivitatea indirect s se determine valorile funciilor

a(x)=

a ( x 1),0 x 5
b( x 1), x 5

x ,x 0

a( x) 5,0 x 5
c(1) 1, x 5

b( x 2),3 x 6
c( x 1), x 6

,x 0
x 1

,x 3

, b(x)=

, c(x)=

Pascal

program a_b_c_ind;
var x:integer;
function a(x:integer):integer;
forward;
function b(x:integer):integer;
forward;
function c(x:integer):integer;
begin
if x<0 then c:=sqr(x) else
if (x>=0) and (x<5) then
c:=a(x+1) else
if x>=5 then c:=b(x+1);
end;
function a(x:integer):integer;
begin
if x<=3 then a:=sqr(x) else
if (x>3) and (x<=6) then
a:=b(x-2) else
if x>6 then a:=c(x-1);
end;
function b(x:integer):integer;
begin
if x<0 then b:=sqr(x+1) else
if (x>=0) and (x<5) then
b:=a(x)+5 else
if x>=5 then b:=c(-1)-1;
end;
begin
writeln('Introduceti x: ');
readln(x);
writeln('a(',x,')=
',a(x),' ... b(',x,')= ',b(x),' ...
c(',x,')= ',c(x));
readln;
end.
Introduceti x:
3
a(3)= 9 ... b(3)= 14 ... c(3)= 9

#include <conio.h>
#include <stdio.h>
#include <math.h>
int x;
int a(int x);
int b(int x);
int c(int x){
if (x<0) return pow(x,2);
else if (x>=0 && x<5) return
a(x+1);
else if (x>=5) return b(x+1);
}
int a(int x){
if (x<=3) return pow(x,2);
else if (x>3 && x<=6) return
b(x-2);
else if (x>6) return c(x-1);
}
int b(int x){
if (x<0) return pow(x+1,2);
else if (x>=0 && x<5) return
a(x)+5;
else if (x>=5) return c(-1)1;
}
int main(){
printf("Introduceti x: ");
scanf("%d",&x);
printf("\n a(%d)= %d \n
b(%d)= %d \n c(%d)=
%d",x,a(x),x,b(x),x,c(x));
getch();
}
Introduceti x: 3
a(3)= 9
b(3)= 14
c(3)= 9

12

Problema 10. Recursivitatea indirect an i bn.


Se d numrul ntreg n. S se calculeze irurile an i bn n Pascal i C definite
recurent astfel:
a, n 0

an1 bn1

An=

b, n 0

,n 0

n 1

* b n 1 , n 0

si bn=

Pascal

program PR_10_ind;
var a,b,n,i:integer;
function pa(n:integer):real;
forward;
function pb(n:integer):real;
begin
if n=0 then pb:=b else
if n>0 then pb:=sqrt(pa(n1)*pb(n-1));
end;
function pa(n:integer):real;
begin
if n=0 then pa:=a else
if n>0 then pa:=(pa(n-1)+pb(n1))/2;
end;
begin
writeln('Introduceti a,b,n:
');
readln(a,b,n);
for i:=0 to n do
writeln('a(',i,')=
',pa(i),' ... b(',i,')= ',pb(i));
readln;
end.
Introduceti a,b,n:
3 5 6
a(0)= 3 ... b(0)= 5
a(1)= 4 ... b(1)= 3.87298334620742
a(2)= 3.93649167310371 ...
b(2)= 3.93597934253086
a(3)= 3.93623550781729 ...
b(3)= 3.93623549948183
a(4)= 3.93623550364956 ...
b(4)= 3.93623550364956
a(5)= 3.93623550364956 ...
b(5)= 3.93623550364956

#include <conio.h>
#include <stdio.h>
#include <math.h>
int a,b,n,i;
double pa(int n);
double pb(int n){
if(n==0) return b;
else return sqrt(pa(n1)*pb(n-1));
}
double pa(int n){
if (n==0) return a;
else return (pa(n-1)+pb(n1))/2;
}
int main(){
printf("Introduceti
a,b,n:\n");
scanf("%d %d %d",&a,&b,&n);
for(i=0;i<=n;i++)
printf("\n a(%d)= %f ...
b(%d)= %f \n",i,pa(i),i,pb(i));
getch();
}
Introduceti a,b,n:
3 5 6
a(0)= 3.000000 ... b(0)= 5.000000
a(1)= 4.000000 ... b(1)= 3.872983
a(2)= 3.936492 ... b(2)= 3.935979
a(3)= 3.936236 ... b(3)= 3.936235
a(4)= 3.936236 ... b(4)= 3.936236

13

a(6)= 3.93623550364956 ...


b(6)= 3.93623550364956

a(5)= 3.936236 ... b(5)= 3.936236


a(6)= 3.936236 ... b(6)= 3.936236

Problema 11. Recursivitatea indirect an, bn i cn.


Aplicnd recursivitatea indirect s se determine valorile funciilor

a , x=0
A n= an1 +b n1 +c n1 , x> 0 B n=
3
Cn =

b , x=0
, x >0
an1b n1c n1
3

c , x=0
a n1+ bn1+ c n1 , x >0

Pascal

program pa_pb_pc_ind;
var a,b,c,n,i:integer;
function pa(n:integer):real;
forward;
function pb(n:integer):real;
forward;
function pc(n:integer):real;
begin
if n=0 then pc:=c else
if n>0 then pc:=sqrt(pa(n1)+pb(n-1)+pc(n-1));
end;
function pa(n:integer):real;
begin
if n=0 then pa:=a else
if n>0 then pa:=(pa(n-1)+pb(n1)+pc(n-1))/3;
end;
function pb(n:integer):real;
begin
if n=0 then pb:=b else
if n>0 then pb:=power(pa(n1)*pb(n-1)*pc(n-1),1/3);
end;
begin
writeln('Introduceti a,b,c,n:
');
readln(a,b,c,n);
for i:=0 to n do
writeln('a(',i,')=
',pa(i),' ... b(',i,')=
',pb(i),' ... c(',i,')= ',pc(i));
readln;
end.

#include <conio.h>
#include <stdio.h>
#include <math.h>
int a,b,c,n,i;
double pa(int n);
double pb(int n);
double pc(int n){
if(n==0) return c;
else return sqrt(pa(n1)+pb(n-1)+pc(n-1));
}
double pa(int n){
if (n==0) return a;
else return (pa(n-1)+pb(n1)+pc(n-1))/3;
}
double pb(int n){
if (n==0) return b;
else return pow(pa(n-1)*pb(n1)*pc(n-1),1/3);
}
int main(){
printf("Introduceti
a,b,c,n:\n");
scanf("%d %d %d
%d",&a,&b,&c,&n);
for(i=0;i<=n;i++)
printf("\n a(%d)= %0.16f ...
b(%d)= %0.16f ... c(%d)= %0.16f
\n",i,pa(i),i,pb(i),i,pc(i));
getch();
}

14

Introduceti a,b,c,n:
3 1000 15 3
a(0)= 3 ... b(0)= 1000
a(1)= 339.333333333333
b(1)= 35.5689330449006
c(1)= 31.9061122670876
a(2)= 135.602792881774
b(2)= 72.7540347887358
c(2)= 20.1694912837513
a(3)= 76.1754396514203
b(3)= 58.3812721380849
c(3)= 15.1170869863959

... c(0)= 15
...
...
...
...
...
...

Introduceti a,b,c,n:
3 1000 15 3
a(0)= 3.0000000000000000 ...
b(0)= 1000.0000000000000000 ...
c(0)= 15.0000000000000000
a(1)= 339.3333333333333100 ...
b(1)= 1.0000000000000000 ...
c(1)= 31.9061122670876320
a(2)= 124.0798152001403200 ...
b(2)= 1.0000000000000000 ...
c(2)= 19.2935078614652370
a(3)= 48.1244410205351870 ...
b(3)= 1.0000000000000000 ...
c(3)= 12.0155450588646020

Problema 12. Ackerman


S se alctuiasc un program n Pascal i C pentru calcularea funciei
Ackerman:
n 1, m 0

Ack (m 1,1), n 0

Ack(m,n)=
Pascal

Ack (m 1, Ack (m, n 1)), m, n 0

program Ackerman;
var m,n:integer;
function ack(m,n:integer):integer;
begin
if m=0 then ack:=n+1 else
if n=0 then ack:=ack(m-1,1) else
ack:=ack(m-1,ack(m,n-1));
end;
begin
writeln('Introduceti m si n: ');
readln(m,n);
writeln('Ackerman de ',m,', ',n,'
este ',ack(m,n));
readln;
end.

#include <conio.h>
#include <stdio.h>
int m,n;
int ack(int m,int n){
if (m==0) return n+1;
else if (n==0) return ack(m1,1);
else return ack(m-1,ack(m,n1));
}
int main(){
printf("Introdu m,n:\n");
scanf("%d %d",&m,&n);
printf("Ackerman de %d, %d
este %d",m,n,ack(m,n));
getch();
}

15

0
0
1
1
2
2
3
3
3
3
4
4
4
4
4

0
1
1
2
1
2
0
2
3
4
0
1
2
3
4

Ack(m,n
)
1
2
3
4
5
7
5
29
61
125
13
-

0
0
1
1
2
2
3
3
3
3
4
4
4
4
4

0
1
1
2
1
2
0
2
3
4
0
1
2
3
4

Ack(m,n
)
1
2
3
4
5
7
5
29
61
125
13
-

Problema 13. Hermite.


S se alctuiasc un program n Pascal i C pentru calcularea funciei Hermite.

H(n,x)=
Pascal

1, n 0

2 x, n 1
2 x * H (n 1, x) 2(n 1) * H (n 2, x)), n 2

program Hermite;
var n,x:integer;
function H(n,x:integer):integer;
begin
if n=0 then H:=1 else
if n=1 then H:=2*x else
H:=(2*x)*H(n-1,x)-2*(n-1)*H(n-2,x);
end;
begin
writeln('Introduceti n si x: ');
readln(n,x);
writeln('Hermite de ',n,', ',x,'
este ',H(n,x));
readln;
end.

C
#include <conio.h>
#include <stdio.h>
int n,x;
int H(int n,int x){
if (n==0) return 1;
else if (n==1) return 2*x;
else return (2*x)*H(n-1,x)2*(n-1)*H(n-2,x);
}
int main(){
printf("Introdu n,x:\n");
scanf("%d %d",&n,&x);
printf("Hermite de %d, %d
este %d",n,x,H(n,x));
getch();
}

16

n
0
0
1
1
2
1
2
3
1
2
3
3
3
4
5

x
0
1
0
1
1
2
2
1
3
3
3
2
4
7
7

H(n,x)
1
1
0
2
2
4
14
-4
6
34
180
40
464
36076
483784

n
0
0
1
1
2
1
2
3
1
2
3
3
3
4
5

x
0
1
0
1
1
2
2
1
3
3
3
2
4
7
7

H(n,x)
1
1
0
2
2
4
14
-4
6
34
180
40
464
36076
483784

Problema 14. Cebev.


S se alctuiasc un program n Pascal i C pentru calcularea funciei Cebev.

T(n,x)=
Pascal

1.n 0

2 x, n 1
2 x * T (n 1, x) T (n 2, x), n 2

program Cebisev;
var n,x:integer;
function T(n,x:integer):integer;
begin
if n=0 then T:=1 else
if n=1 then T:=2*x else
T:=(2*x)*T(n-1,x)-T(n-2,x);
end;
begin
writeln('Introduceti n si x: ');
readln(n,x);
writeln('Cebisev de ',n,', ',x,'
este ',T(n,x));
readln;
end.

C
#include <conio.h>
#include <stdio.h>
int n,x;
int T(int n,int x){
if (n==0) return 1;
else if (n==1) return 2*x;
else return (2*x)*T(n-1,x)T(n-2,x);
}
int main(){
printf("Introdu n,x:\n");
scanf("%d %d",&n,&x);
printf("Cebisev de %d, %d
este %d",n,x,T(n,x));
getch();
}

17

n
0
0
1
1
2
1
2
3
1
2
3
3
3
4
5

x
0
1
0
1
1
2
2
1
3
3
3
2
1
7
7

T(n,x)
1
1
0
2
3
4
15
4
6
35
204
56
4
37829
526890

n
0
0
1
1
2
1
2
3
1
2
3
3
3
4
5

x
0
1
0
1
1
2
2
1
3
3
3
2
1
7
7

T(n,x)
1
1
0
2
3
4
15
4
6
35
204
56
4
37829
526890

Problema 15. Manna Pnueli 1.


S se alctuiasc un program n Pascal i C pentru calcularea funciei Manna
Pnueli 1.
x 1, x 12

F ( F ( x 2)), x 12

F(x)=
Pascal

program Manna_Pnueli;
var x:integer;
function MP(x:integer):integer;
begin
if x>12 then MP:=x-1 else
if x<=12 then MP:=MP(MP(x+2));
end;
begin
writeln('Introduceti x: ');
readln(x);
writeln('Manna-Pnueli de ',x,' este
',MP(x));
readln;
end.

#include <conio.h>
#include <stdio.h>
int x;
int MP(int x){
if (x>12) return x-1;
else if (x<=12) return
MP(MP(x+2));
}
int main(){
printf("Introdu x:\n");
scanf("%d",&x);
printf("Manna-Pnueli de %d
este %d",x,MP(x));
getch();
}

18

x
0
1
4
7
8
9
10
11
12
13
14
15
16
17
18
19
20

MP(x)
12
12
12
12
12
12
12
12
12
12
13
14
15
16
17
18
19

x
0
1
4
7
8
9
10
11
12
13
14
15
16
17
18
19
20

MP(x)
12
12
12
12
12
12
12
12
12
12
13
14
15
16
17
18
19

Problema 16. Manna Pnueli 2.


S se alctuiasc un program n Pascal i C pentru calcularea funciei Manna
Pnueli 2.
F ( x 1), x 5

F ( F ( x 2)),5 x 12
x 1, x 12

F(x)=
Pascal

program Manna_Pnueli;
var x:integer;
function MP(x:integer):integer;
begin
if x<5 then MP:=MP(x+1) else
if x<=12 then MP:=MP(MP(x+2)) else
MP:=x-1;
end;
begin
writeln('Introduceti x: ');
readln(x);
writeln('Manna-Pnueli de ',x,' este

#include <conio.h>
#include <stdio.h>
int x;
int MP(int x){
if (x<5) return MP(x+1);
else if (x<=12) return
MP(MP(x+2));
else return x-1;
}
int main(){
printf("Introdu x:\n");
scanf("%d",&x);

19

',MP(x));
readln;
end.

x
1
2
3
4
5
6
7
8
9
10
11
12
13
14

printf("Manna-Pnueli de %d
este %d",x,MP(x));
getch();
}

MP(x)
12
12
12
12
12
12
12
12
12
12
12
12
12
13

x
1
2
3
4
5
6
7
8
9
10
11
12
13
14

MP(x)
12
12
12
12
12
12
12
12
12
12
12
12
12
13

Problema 17. Turnurile Hanoi.


Sunt date 3 tije ,x,y,z.Pe tija x sunt plasate n discuri aezate n ordinea descresctoare a
diametrelor(discul mic pe cel mai mare). Se cere de trecut toate discurile de pe tija x pe tija y,
folosind tija intermediar z, astfel nct s se respecte condiia: discul mai mic totdeauna se
afl supra celui mai mare la orice transfer.

Hn(x,y,z)=

xy, n 1

H n 1 ( x, z , y ), xy, H n 1 ( z, y, x), n 1

Pascal

Program Turnuri;
uses crt;
var x,y,z:char;
n:integer;
g:real;
procedure H(n:integer;x,y,z:char);
begin
if n=1 then writeln(x,' ',y)
else
begin
H(n-1,x,z,y);
writeln(x,' ',y);
H(n-1,z,y,x);
end;

#include<stdio.h>
#include<conio.h>
char x,y,z;
int n,g;
void hanoi(int n,char x,char y,char
z)
{
if (n==1) printf("%c %c
\n",x,y);
else {
hanoi(n-1,x,z,y);
printf("%c %c \n",x,y);
hanoi(n-1,z,y,x);
}

20

end;
begin
clrscr;
writeln('Introduceti numarul de
discuri: ');
readln(n);
H(n,'x','y','z');
g:=power(2,n)-1;
writeln('Numarul de miscari este
',g);
readkey;
end.

}
int main()
{
printf("Introduceti numarul de
discuri: \n");
scanf("%d",&n);
x='x';
y='y';
z='z';
hanoi(n,'x','y','z');
g=pow(2,n)-1;
printf("Numarul de miscari este
%d",g);
getch();
}
Introduceti numarul de discuri:
4
x z
x y
z y
x z
y x
y z
x z
x y
z y
z x
y x
z y
x z
x y
z y
Numarul de miscari este 15

Introduceti numarul de discuri:


4
x z
x y
z y
x z
y x
y z
x z
x y
z y
z x
y x
z y
x z
x y
z y
Numarul de miscari este 15

Problema 18. Compoziia funciilor


2 2 1, x 5
x

2,5 x 8

5 2 3 x 2, x 1
x

3, x 8

Sunt date 2 funcii f(x)=


De gsit :
1.Compoziia f(g(f(x)))
2.Compoziia g(f(g(x)))
3.Compoziia f(g(f(g(f(x)))))

Pascal

i g(x)=

Program compunere;
uses crt;
var x:integer;
function f(x:integer):integer;
begin
if x<5 then f:=2*sqr(x)+1 else
if x<8 then f:=x*x*x*x+2 else
if x>=8 then f:=3;
end;
function g(x:integer):integer;
begin
if x<=1 then g:=5*sqr(x)-(3*x)

x 5, x 1

#include<stdio.h>
#include<conio.h>
#include<math.h>
int x;
int f(int x){
if (x<5) return 2*pow(x,2)+1;
else if (x>=5 && x<8) return
pow(x,4)+2;
else return 3;
}
int g(int x){
if (x<=1) return 5*pow(x,2)-

21

+2 else
if x>1 then g:=sqr(x)-x+5;
end;
begin
writeln('Introduceti x: ');
readln(x);
writeln('f(g(f(',x,')))=
',f(g(f(x))));
writeln('g(f(g(',x,')=
',g(f(g(x))));
writeln('f(g(f(g(f(',x,')))))=
',f(g(f(g(f(x))))));
readkey;
end.

x
0
1
2
3
4
5
6
7

f(g(f(x)))
33
3
3
3
3
3
3
3

g(f(g(x)))
77
1061
5772011
11
11
11
11
11

f(g(f(g(f(x)))))
3
3
3
3
3
3
3
3

(3*x)+2;
else return pow(x,2)-x+5;
}
int main(){
printf("Introduceti x: \n");
scanf("%d",&x);
printf("f(g(f(%d)))=
%d",x,f(g(f(x))));
printf("\ng(f(g(%d)))=
%d",x,g(f(g(x))));
printf("\nf(g(f(g(f(%d)))))=
%d",x,f(g(f(g(f(x))))));
getch();
}

x
0
1
2
3
4
5
6
7

f(g(f(x)))
33
3
3
3
3
3
3
3

g(f(g(x)))
77
1061
5772011
11
11
11
11
11

f(g(f(g(f(x)))))
3
3
3
3
3
3
3
3

Problema 19. Algoritmul lui Euclid pentru n numere.


S se determine cel mai mare divizor comun a n numere naturale. S se scrie varianta
recursiv i nerecursiv n Pascal i C.
a, a b

cmmdc(a b, b), a b
cmmdc(a, b a), b a

CMMDC(a,b)=

Pascal

Program cmmdc_n;
uses crt;
type vector = array[1..50] of
integer;
var x:vector; n,i:integer;
function
cmmdc(a,b:integer):integer;
begin
if a=b then cmmdc:=a else
if a>b then cmmdc:=cmmdc(a-b,b)
else if a<b then cmmdc:=cmmdc(a,ba);
end;

#include <conio.h>
#include <stdio.h>
int cmmdc(int a,int b){
if(a==b) return a; else
if(a>b) return cmmdc(a-b,b);
else
if(a<b) return cmmdc(a,b-a);
}
int main(){
int n=0, x[50],i;
printf("Introduceti numarul de
elemente al tabloului: ");
scanf("%d",&n);

22

function
div_imp(p,q:integer) :integer;
var mij,d1,d2:integer;
begin
if abs(p-q)<=1 then
div_imp:=cmmdc(x[p],x[q]) else
begin
mij:=(p+q) div 2;
d1:=div_imp(p,mij);
d2:=div_imp(mij+1,q);
div_imp:=cmmdc(d1,d2);
end;
end;
begin
clrscr;
writeln('Introduceti numarul de
elemente al tabloului: ');
readln(n);
writeln('Introduceti elementele
tabloului: ');
for i:=1 to n do begin
write('x[',i,']= ');
readln(x[i]);
end;
writeln('CMMDC al acestor
numere este: ',div_imp(1,n));
readkey;end.
Introduceti numarul de elemente al
tabloului:
6
Introduceti elementele tabloului:
x[1]= 75
x[2]= 85
x[3]= 65
x[4]= 95
x[5]= 105
x[6]= 35
CMMDC al acestor numere este: 5

for(i=0;i<n;i++){
printf("a[%d]= ",i);
scanf("%d",&x[i]);
}
n=cmmdc(x[0],x[1]);
for(i=0;i<n;i++){
n=cmmdc(n,x[i]);
}
printf("CMMDC al acestor numere
este %d",n);
getch();
}

Introduceti numarul de elemente al


tabloului:
6
a[0]= 75
a[1]= 65
a[2]= 85
a[3]= 95
a[4]= 35
a[5]= 105
CMMDC al acestor numere este 5

Problema 20. Generarea partiiilor unui numar natural.


Se d un numar natural. Se cere s se elaboreze un program n Pascal i C care
ne va genera toate partiiile numrului n.
Pascal
C
program partitii;
uses crt;
var n,s,nr:integer;
x: array[1..31] of integer;
procedure tipar(k:integer);
var i:integer;
begin
nr:=nr+1;
for i:=1 to k-1 do
write(x[i],'+');
write(x[k]);
writeln;

end;
procedure back(k:integer);
var i:integer;

#include<iostream>
using namespace std;
int x[31];
int n,s=0,nr;
void tipar(int k){
int i;
nr++;
for(i=1;i<=k-1;i++)
cout<<x[i]<<'+';
cout<<x[k]<<endl;
}
void back(int k){
int i;
if(s==n) tipar(k-1);

23

begin

else
if s=n then tipar(k-1) else
begin
for i:=1 to n-s do
begin
x[k]:=i;
s:=s+x[k];
back(k+1);
s:=s-x[k];
end; end;

end;
begin
clrscr;
writeln('Introduceti un numar
natural: ');
readln(n);
back(1);
writeln(nr,' solutii');
readkey;
end.
Introduceti un numar natural:
3
1+1+1
1+2
2+1
3
4 solutii

for(i=1;i<=n-s;i++){
x[k]=i;
s=s+x[k];
back(k+1);
s=s-x[k];
}

int main(){
cout<<"Introduceti un numar
natural: ";
cin>>n;
back(1);
cout<<nr<<" solutii";
return 0;
}

Introduceti un numar natural: 3


1+1+1
1+2
2+1
3
4 solutii

Problema 21. Permutri.


Fie dat numarul natural n.S se genereze toate permutarile
din n elemente a mulimii A={1,2,3, .. . ,n} n Pascal i C.
Se numesc permutri ale unei multimi A cu n elemente toate mulimile ordonate care se pot forma cu cele n
elemente ale lui A. Pentru n N*, avem Pn = 1*2*3*...*n=n!

Pascal

program Permutari;
uses crt;
type vector = array[1..25] of byte;
var st:vector;
n:byte;
procedure init;
var i:byte;
begin
writeln('Introduceti n: ');
readln(n);
for i:=1 to n do
st[i]:=0;
end;
procedure tipar(p:byte);
var i:byte;

#include <iostream>
using namespace std;
int st[25],n,i,j;
void init(){
int i;
cout<<"Introduceti n: ";
cin>>n;
for(i=1;i<=n;i++)
st[i]=0;
}
void tipar(int p){
int j;
for(j=1;j<=p;j++)
cout<<st[j]<<" ";
cout<<endl;

24

begin

for i:=1 to p do
write(st[i],' ');
writeln;

end;
function valid(p:byte):boolean;
var i:byte;
begin
valid:=true;
for i:=1 to p-1 do
if st[p]=st[i] then
valid:=false;
end;
procedure back(p:byte);
var pval:byte;
begin
for pval:=1 to n do
begin
st[p]:=pval;
if valid(p) then
if p=n then tipar(p)
else back(p+1);
end;
end;
begin
clrscr;
init;
back(1);
readkey;
end.
Introduceti n:
3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

}
bool valid(int p){
int i;
bool ok;
ok=true;
for(i=1;i<=(p-1);i++)
if(st[p]==st[i])
ok=false;
return ok;
}
void back(int p){
int pval;
for(pval=1;pval<=n;pval++)
{
st[p]=pval;
if(valid(p))
if(p==n) tipar(p);
else back(p+1);
}
}
int main(){
init();
back(1);
return 0;
}

Introduceti n: 3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Problema 22. Aranjamente.


S se genereze aranjamentele din n luate cte k a multimii A={1,2, n} n
Pascal i C.
A mn =n ( n1 ) ( nm+1 )=

Pascal

n!
,nm
( nm ) !

program Aranjamente;
uses crt;
type vector = array[1..25] of byte;
var st:vector;
n,k:byte;
procedure init;
var i:byte;
begin
writeln('Introduceti n: ');
readln(n);
writeln('Introduceti k: ');
readln(k);
for i:=1 to n do

#include<iostream>
using namespace std;
int st[25],n,k,i,j;
void init(){
int i;
cout<<"Introduceti n: ";
cin>>n;
cout<<"Introduceti k: ";
cin>>k;
for (i=1;i<=n;i++)
st[i]=0;
}
void tipar(int p){

25

st[i]:=0;
end;
procedure tipar(p:byte);
var i:byte;
begin
for i:=1 to p do
write(st[i],' ');
writeln;
end;
function valid(p:byte):boolean;
var i:byte;
begin
valid:=true;
for i:=1 to p-1 do
if st[p]=st[i] then
valid:=false;
end;
procedure back(p:byte);
var pval:byte;
begin
for pval:=1 to n do
begin
st[p]:=pval;
if valid(p) then
if p=k then tipar(p)
else back(p+1);
end;
end;
begin
clrscr;
init;
back(1);
readkey;
end.
Introduceti n:
3
Introduceti k:
2
1 2
1 3
2 1
2 3
3 1
3 2

int j;
for (j=1;j<=p;j++)
cout<<st[j]<<" ";
cout<<endl;
}
bool valid(int p){
int i;
bool ok;
ok=true;
for(i=1;i<=(p-1);i++);
if (st[p]==st[i])
ok=false;
return ok;
}
void back(int p){
int pval;
for (pval=1;pval<=n;pval++){
st[p]=pval;
if (valid (p));
if (p==k) tipar(p);
else back(p+1);
}
}
int main(){
init();
back(1);
return 0;
}

Introduceti n: 3
Introduceti k: 2
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

26

Problema 22. Combinri.


S se genereze combinri din n elemente luate cte k n Pascal i C.
Numrul combinrilor a n elemente luate cte m este calculat cu formula:
m

Cn =

n!
m! ( nm ) !

Pascal

program Combinari;
uses crt;
type vector = array[1..25] of byte;
var st:vector;
n,k:byte;
procedure init;
var i:byte;
begin
writeln('Introduceti n: ');
readln(n);
writeln('Introduceti k: ');
readln(k);
for i:=1 to n do

#include <iostream>
using namespace std;
int st[25],n,i,j,k;
void init(){
int i;
cout<<"Introduceti n: ";
cin>>n;
cout<<"Introduceti k: ";
cin>>k;
for(i=1;i<=n;i++)
st[i]=0;
}

27

st[i]:=0;
end;
procedure tipar(p:byte);
var i:byte;
begin
for i:=1 to p do
write(st[i],' ');
writeln;
end;
function valid(p:byte):boolean;
var i:byte;
begin
valid:=true;
for i:=1 to p-1 do
if st[p]<=st[i] then
valid:=false;
end;
procedure back(p:byte);
var pval:byte;
begin
for pval:=1 to n do
begin
st[p]:=pval;
if valid(p) then
if p=k then tipar(p)
else back(p+1);
end;
end;
begin
clrscr;
init;
back(1);
readkey;
end.
Introduceti n:
3
Introduceti k:
2
1 2
1 3
2 3

void tipar(int p){


int j;
for(j=1;j<=p;j++)
cout<<st[j]<<" ";
cout<<endl;
}
bool valid(int p){
int i;
bool ok;
ok=true;
for(i=1;i<=(p-1);i++)
if(st[p]<=st[i])
ok=false;
return ok;
}
void back(int p){
int pval;
for(pval=1;pval<=n;pval++)
{
st[p]=pval;
if(valid(p))
if(p==k)tipar(p);
else back(p+1);
}
}
int main(){
init();
back(1);
return 0;
}

Introduceti n: 3
Introduceti k: 2
1 2
1 3
2 3

Problema 24. Problema damelor.


Considerndu-se o tabl de ah de dimensiune nxn, s se determine toate
modalitile de amplasare a n regine pe tabla de ah astfel nct s nu se atace
reciproc (dou regine se atac dac se afl pe aceeai linie, coloan, sau
diagonal).
Pascal
C
program dame;
uses crt;
var r:array[1..20] of integer;
n,j,n_s:integer;
f:boolean;
function valid(j:integer):boolean;
var i:integer;
begin
valid:=true;
for i:=1 to j-1 do

#include <iostream>
#include <math.h>
#include<stdlib.h>
using namespace std;
int t[20],n,a;
void tipar(){
for (int i=1;i<=n;i++)
cout<<t[i]<<" ";
cout<<endl;
}

28

if(r[i]=r[j]) or
(abs(r[i]-r[j])=j-i) then begin
valid:=false;
break;
end;
end;
procedure sol;
var i:integer;
begin
inc(n_s);
write(n_s,': ');
for i:=1 to n do write(r[i],' ');
writeln;
end;
begin
clrscr;
write('Introduceti numarul de dame:
');
readln(n);
j:=1;
n_s:=0;
while j>0 do begin
f:=false;
while not f and (r[j]<=n-1) do
begin
inc(r[j]);
if valid(j) then f:=true;
end;
if not f then dec(j)
else if j<n then begin inc(j);
r[j]:=0;
end
else sol;
end;
if n_s=0 then write('Nu exista
modalitati de amplasare a damelor
pe tabla de sah!');
readkey;
end.
Introduceti numarul de dame: 4
1: 2 4 1 3
2: 3 1 4 2

void dame(int k){


int i,j,corect;
if(k==n+1) tipar();
else {
for (i=t[k]+1;i<=n;i++)
{
t[k]=i;
corect=1;
for(j=1;j<=k-1;j+
+)
if (t[j]==t[k] ||
abs(t[k]-t[j])==(k-j)) corect=0;
if (corect)
dame(k+1);
}
}
t[k]=0;
a=t[k];
}
int main(){
cout<<"Introduceti numarul de
dame: ";
cin>>n;
dame(1);
}

Introduceti numarul de dame: 4


2 4 1 3
3 1 4 2

Problema 25. Problema Comis_Voiajorului.


Se consider n orae numerotate de la 1 la n. Un comis-voiajor trebuie s-i prezinte
produsele n cele n orae fr a trece de dou ori prin acelai ora cu condiia s se ntoarc n
oraul de start. ntre orae poate exista drum direct sau nu.
S se determine toate rutele posibile pe care poate s se deplaseze comis-voiajorul.

Pascal

program C_V;
uses crt;
type vector = array[1..25] of
integer;
matrice = array[1..25,1..25] of
0..1;
var st:vector;
a:matrice;
n,start:integer;

#include <iostream>
using namespace std;
int st[50],a[100][100],n,start,i;
void init(){
int i,i0,j0;
cout<<"Introduceti numarul de
orase: ";
cin>>n;
cout<<"Introduceti orasul de

29

procedure init;
var k,i,j:integer;
begin
writeln('Introduceti numarul de
orase: ');
readln(n);
writeln('Introduceti orasul de
start: ');
readln(start);
for k:=1 to n do
st[k]:=0;
for i:=1 to n do
for j:=i+1 to n do
if i<j then begin
write('a[',i,',',j,']= ');
readln(a[i,j]);
a[j,i]:=a[i,j];
end;
writeln('Traseele pe care le
poate urma comis-voiajorul: ');
st[1]:=start;
end;
procedure tipar(p:integer);
var i:integer;
begin
for i:=1 to p do
write(st[i]:3,' ');
writeln;
end;
function valid(p:integer):boolean;
var i:integer;
ok:boolean;
begin
ok:=true;
for i:=1 to p-1 do
if (st[p]=st[i]) or
(a[st[p-1],st[p]]=0) then
ok:=false;
valid:=ok;
end;
procedure back(p:integer);
var pval:integer;
begin
for pval:=1 to n do
begin
st[p]:=pval;
if valid(p) then
if (p=n) and
(a[st[p],start]=1) then tipar(p)
else back(p+1);
end;
end;

start: ";
cin>>start;
for(i=1;i<=50;i++)
st[i]=0;
for(i0=1;i0<=n;i0++)
a[i0][i0]=0;
for(i0=1;i0<=n;i0++)
for(j0=1;j0<=n;j0++)
if(i0<j0){
cout<<"a["<<i0<<","<<j0<<"]= ";
cin>>a[i0][j0];
a[j0][i0]=a[i0][j0];
}
cout<<"Traseele pe care le
poate urma comis-voiajorul: \n";
st[1]=start;
}
void tipar(int p){
for(i=1;i<=p;i++)
cout<<st[i]<<" ";
cout<<endl;
}
bool valid(int p){
bool ok;
int i;
ok=true;
for(i=1;i<=(p-1);i++)
if(st[p]==st[i]) ok=false;
if (a[st[p]][st[p1]]==0) ok=false;
return ok;
}
void back(int p){
int pval;
for (pval=1;pval<=n;pval++){
st[p]=pval;
if (valid(p))
if ((p==n) && (a[st[p]]
[start]==1)) tipar(p);
else back(p+1);
}
}
int main(){
init();
back(2);
return 0;
}

begin
clrscr;
init;
back(2);

30

readkey;
end.
Introduceti numarul de orase:
4
Introduceti orasul de start:
1
a[1,2]= 1
a[1,3]= 0
a[1,4]= 1
a[2,3]= 1
a[2,4]= 0
a[3,4]= 1
Traseele pe care le poate urma
comis-voiajorul:
1
2
3
4
1
4
3
2

Introduceti numarul de orase: 4


Introduceti orasul de start: 1
a[1,2]= 1
a[1,3]= 0
a[1,4]= 1
a[2,3]= 1
a[2,4]= 0
a[3,4]= 1
Traseele pe care le poate urma
comis-voiajorul:
1 2 3 4
1 4 3 2

Problema 26. Produsul cartezian.


Fie date mulimile X1, X2, X3, . . . , Xn. S se scrie un program n Pascal iC care
va calcula produsul cartezian al acestor mulimi.
Pascal
C
program pr_cart;
uses crt;
type vector = array[1..25] of
integer;
var st,nr:vector;
n:integer;
procedure init;
var i:integer;
begin
writeln('Introduceti numarul de
multimi: ');
readln(n);
for i:=1 to n do
st[i]:=0;

#include <iostream>
using namespace std;
int st[25],nr[25],n,i;
void init(){
int i;
cout<<"Introduceti numarul de
multimi: ";
cin>>n;
for(i=0;i<=n;i++)
st[i]=0;
for(i=1;i<=n;i++){
cout<<"Introduceti numarul
de elemente al multimii "<<i<<": ";
cin>>nr[i];

31

for i:=1 to n do
begin
writeln('Introduceti
numarul de elemente al multimii
',i,': ');
readln(nr[i]);
end;
writeln('Produsul cartezian al
acestor multimi este');
end;

}
cout<<"Produsul cartezian al
acestor multimi este"<<endl;
}

function valid(p:integer):boolean;
begin
valid:=true;
end;

bool valid(int p){


return true;
}

procedure tipar(p:integer);
var i:integer;
begin
for i:=1 to p do
write(st[i],' ');
writeln;
end;
procedure back(p:integer);
var pval:integer;
begin
if p=n+1 then tipar(p-1) else
for pval:=1 to nr[p] do
begin
st[p]:=pval;
if valid(p) then
back(p+1);
end;
end;
begin
clrscr;
init;
back(1);
readkey;
end.
Introduceti numarul de multimi:
2
Introduceti numarul de elemente al
multimii 1:
5
Introduceti numarul de elemente al
multimii 2:
4
Produsul cartezian al acestor
multimi este
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1

void tipar(int p){


for(i=1;i<=p;i++)
cout<<st[i]<<" ";
cout<<endl;
}

void back(int p){


int pval;
if(p==n+1) tipar(p-1);
else
for(pval=1;pval<=nr[p];pval++){
st[p]=pval;
if(valid(p)) back(p+1);
}
}
int main(){
init();
back(1);
return 0;
}

Introduceti numarul de multimi: 2


Introduceti numarul de elemente al
multimii 1: 5
Introduceti numarul de elemente al
multimii 2: 4
Produsul cartezian al acestor
multimi este
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4

32

3
3
3
4
4
4
4
5
5
5
5

2
3
4
1
2
3
4
1
2
3
4

4
4
4
4
5
5
5
5

1
2
3
4
1
2
3
4

Problema 27. Problema colorrii hrilor.


Fiind dat o hart cu n ri, se cer toate soluiile de colorare a hrii aplicnd cel
mult 4 culori, astfel nct dou ri cu frontiera comun s fie colorate diferit. S
se scrie un program n Pascal i n C.
Exemplu:
ara 1 este vecin cu rile 2 i 4.
ara 2 este vecin cu rile 1,3,4 i 5.
ara 3 este vecin cu rile 2,4 i 5.
ara 4 este vecin cu rile 2 i 5.
ara 5 este vecin cu rile 2,3 i 4.
O soluie ar fi
1 2 1 3 4
Pascal

program colorare_harta;
type stiva=array[1..100] of
integer;
var st: stiva;
i,j,n,k:integer;
es, ev:boolean;

#include <iostream>
using namespace std;
int st[10],a[20][20],n,k;
void init(){
st[k]=0;

33

a:array[1..20, 1..20] of
integer;
procedure init(k:integer; var
st:stiva);
begin
st[k]:=0;
end;
procedure succesor(var es: boolean;
var st:stiva; k:integer);
begin
if st[k]<4 then begin
st[k]:=st[k]+1;
es:=true
end
else es:=false
end;
procedure valid(var ev:boolean;
st:stiva; k:integer);
var i:integer;
begin
ev:=true;
for i:=1 to k-1 do
if (st[i]=st[k]) and
(a[i,k]=1) then ev:=false
end;
function
solutie(k:integer):boolean;
begin
solutie:=(k=n)
end;
procedure tipar;
var i:integer;
begin
for i:=1 to n do
writeln('Tara= ', i, ';
culoarea= ', st[i]);
writeln('-------------');
end;
BEGIN
write('Numarul de tari= ');
readln(n);
for i:=1 to n do
for j:=1 to i-1 do begin
write('a[',i,',',j,']=');
readln(a[i,j]);
a[j,i]:=a[i,j];
end;
k:=1;
init(k, st);
while k>0 do begin
repeat succesor(es, st,
k);
if es then
valid(ev, st, k);
until (not es) or (es
and ev);

}
int am_succesor(){
if (st[k]<4){
st[k]++;
return 1;
}
else return 0;
}
int e_valid(){
for (int i=1;i<=k-1;i++)
if (st[k]==st[i] && a[i]
[k]==1) return 0;
return 1;
}
int solutie(){
return k==n;
}
void tipar(){
for (int i=1;i<=n;i++)
cout<<"Tara "<<i<<",
culoarea "<<st[i]<<endl;
cout<<"------------------";
cout<<endl;
}
void back(){
int as;
k=1;
init();
while(k>0){
do{}
while((as=am_succesor()) && !
e_valid());
if (as)
if (solutie()) tipar();
else{
k++;
init();
}
else k--;
}
}
int main(){
int i,j;
cout<<"Introduceti numarul de
tari: ";
cin>>n;
for(i=1;i<=n;i++)
for(j=1;j<=(i-1);j++){
cout<<"a["<<i<<","<<j<<"]=";
cin>>a[i][j];
a[j][i]=a[i][j];
}
back();
}

34

then tipar

if es then if solutie(k)
else begin
k:=k+1;
init(k, st);
end
else k:=k-1;

end;
readln;
END.
Introduceti numarul de tari: 5
a[2,1]=1
a[3,1]=0
a[3,2]=1
a[4,1]=1
a[4,2]=1
a[4,3]=1
a[5,1]=0
a[5,2]=1
a[5,3]=1
a[5,4]=1
Tara 1, culoarea 1
Tara 2, culoarea 2
Tara 3, culoarea 1
Tara 4, culoarea 3
Tara 5, culoarea 4
-----------------Tara 1, culoarea 1
Tara 2, culoarea 2
Tara 3, culoarea 1
Tara 4, culoarea 4
Tara 5, culoarea 3
-----------------Tara 1, culoarea 1
Tara 2, culoarea 2
Tara 3, culoarea 3
Tara 4, culoarea 4
Tara 5, culoarea 1
-----------------Tara 1, culoarea 1
Tara 2, culoarea 2
Tara 3, culoarea 4
Tara 4, culoarea 3
Tara 5, culoarea 1
-----------------Tara 1, culoarea 1
Tara 2, culoarea 3
Tara 3, culoarea 1
Tara 4, culoarea 2
Tara 5, culoarea 4
-----------------Tara 1, culoarea 1
Tara 2, culoarea 3
Tara 3, culoarea 1
Tara 4, culoarea 4
Tara 5, culoarea 2
-----------------Tara 1, culoarea 1
Tara 2, culoarea 3
Tara 3, culoarea 2
Tara 4, culoarea 4

Introduceti numarul de tari: 5


a[2,1]=1
a[3,1]=0
a[3,2]=1
a[4,1]=1
a[4,2]=1
a[4,3]=1
a[5,1]=0
a[5,2]=1
a[5,3]=1
a[5,4]=1
Tara 1, culoarea 1
Tara 2, culoarea 2
Tara 3, culoarea 1
Tara 4, culoarea 3
Tara 5, culoarea 4
-----------------Tara 1, culoarea 1
Tara 2, culoarea 2
Tara 3, culoarea 1
Tara 4, culoarea 4
Tara 5, culoarea 3
-----------------Tara 1, culoarea 1
Tara 2, culoarea 2
Tara 3, culoarea 3
Tara 4, culoarea 4
Tara 5, culoarea 1
-----------------Tara 1, culoarea 1
Tara 2, culoarea 2
Tara 3, culoarea 4
Tara 4, culoarea 3
Tara 5, culoarea 1
-----------------Tara 1, culoarea 1
Tara 2, culoarea 3
Tara 3, culoarea 1
Tara 4, culoarea 2
Tara 5, culoarea 4
-----------------Tara 1, culoarea 1
Tara 2, culoarea 3
Tara 3, culoarea 1
Tara 4, culoarea 4
Tara 5, culoarea 2
-----------------Tara 1, culoarea 1
Tara 2, culoarea 3
Tara 3, culoarea 2
Tara 4, culoarea 4

35

Tara 5, culoarea 1
-----------------Tara 1, culoarea 1
Tara 2, culoarea 3
Tara 3, culoarea 4
Tara 4, culoarea 2
Tara 5, culoarea 1
-----------------Tara 1, culoarea 1
Tara 2, culoarea 4
Tara 3, culoarea 1
Tara 4, culoarea 2
Tara 5, culoarea 3
-----------------Tara 1, culoarea 1
Tara 2, culoarea 4
Tara 3, culoarea 1
Tara 4, culoarea 3
Tara 5, culoarea 2
------------------

Tara 5, culoarea 1
-----------------Tara 1, culoarea 1
Tara 2, culoarea 3
Tara 3, culoarea 4
Tara 4, culoarea 2
Tara 5, culoarea 1
-----------------Tara 1, culoarea 1
Tara 2, culoarea 4
Tara 3, culoarea 1
Tara 4, culoarea 2
Tara 5, culoarea 3
-----------------Tara 1, culoarea 1
Tara 2, culoarea 4
Tara 3, culoarea 1
Tara 4, culoarea 3
Tara 5, culoarea 2
------------------

Problema 28. Problema labirintului.


Se d un labirint sub form de matrice cu m linii i n coloane. Fiecare element
al matricei reprezint o camer a labirintului. ntr-una din camere, de
coordonate x i y se gsete un om. Se cere s se gseasc toate ieirile din
labirint.
Pascal
C
Program labirint;
type stiva=array[1..100,1..3] of
integer;
var st:stiva;
m,n,k,i,j,l,c,l1,c1:integer;
es,ev:boolean;
a:array[1..100,1..100] of
integer;
procedure init(var
st:stiva;k:integer);
begin
st[k,1]:=0;
if k=1 then begin
st[k,2]:=l;
st[k,3]:=c;
end
else begin

#include <iostream>
using namespace std;
int l[10][10],d[2]
[100],i,j,m,n,x,y,k;
void tipar(int k,int d[2][100]){
int i;
cout<<"---SOLUTIE----"<<endl;
for (i=1;i<=k;i++)
cout<<"l="<<d[0][i]<<"
"<<"c="<<d[1][i]<<endl;
}
void ies(int x, int y, int *k, int
l[10][10], int d[2][100]) {
int i,gasit;
if (l[x][y]==16){
tipar(*k, d);

36

st[k,2]:=l1;
st[k,3]:=c1;
end;end;
procedure succesor(var
es:boolean;var st:stiva;k:integer);
begin
if st[k,1]<4 then begin
st[k,1]:=st[k,1]+1;
es:=true;
end
else es:=false;
end;
procedure valid(var
ev:boolean;st:stiva;k:integer);
begin
l1:=st[k,2];
c1:=st[k,3];
case st[k,1] of
1: l1:=l1-1;
2: c1:=c1+1;
3: l1:=l1+1;
4: c1:=c1-1;
end;
ev:=true;
case st[k,1] of
1: if a[st[k,2],st[k,3]]
and 8<>0 then ev:=false;
2: if
a[st[k,2],st[k,3]] and 4<>0 then
ev:=false;
3: if
a[st[k,2],st[k,3]] and 2<>0 then
ev:=false;
4: if
a[st[k,2],st[k,3]] and 1<>0 then
ev:=false;
end;
for i:=1 to k-1 do
if (l1=st[i,2]) and
(c1=st[i,3]) then ev:=false;
end;
function
solutie(k:integer):boolean;
begin
solutie:=(l1<1) or (l1>m) or
(c1<1) or (c1>n);
end;
procedure tipar;
begin
for i:=1 to k do
write('(',st[i,2],',',st[i,3],')
'); writeln;
end;
begin
write('Introduceti numarul de
linii m: ');
readln(m);
write('Introduceti numarul de
coloane n: ');
readln(n);
for i:=1 to m do

}else{
(*k)++;
d[0][*k]=x;
d[1][*k]=y;
gasit=0;
for (i=1;i<(*k);i++)
if(d[0][i]==d[0]
[*k]&&d[1][*k]
==d[1][i]) gasit=1;
if (!gasit)
for(i=1;i<=4;i++){
switch(i) {
case
1: { if (l[x][y] & (int)8) {ies(x1,y,k,l,d);} break;}
case
2: { if (l[x][y] & (int)4)
{ies(x,y+1,k,l,d);} break;}
case
3: { if (l[x][y] & (int)2)
{ies(x+1,y,k,l,d);} break;}
case
4: { if (l[x][y] & (int)1)
{ies(x,y-1,k,l,d);} }
}
}
(*k)--;
}
}
int main(){
cout<<"n=";
cin>>n;
cout<<"m=";
cin>>m;
for (i=1;i<=m;i++)
for (j=1;j<=n;j++){

cout<<"l["<<i<<","<<j<<"]=";
cin>>l[i][j];
}
cout<<"x=";
cin>>x;
cout<<"y=";
cin>>y;
for (i=1;i<=n;i++){
l[0][i]=16;
l[i][n+1]=16;
l[i][0]=16;
l[n+1][i]=16;
}
k=0;
ies(x,y,&k,l,d);

37

for j:=1 to n do begin


write('a[',i,',',j,']=');
read(a[i,j]);
end;
writeln('introduceti
coordonatele de start.');
write('Linia: ');
readln(l);
write('Coloana: ');
readln(c);
k:=1;init(st,k);
while k>0 do begin
repeat
succesor(es,st,k);
if es then
valid(ev,st,k);
until (not es) or (es
and ev);
if es then if solutie(k) then
tipar
else begin
k:=k+1;
init(st,k);
end
else k:=k-1;
end;
end.
Introduceti numarul de linii m: 4
Introduceti numarul de coloane n: 4
a[1,1]=5
a[1,2]=7
a[1,3]=5
a[1,4]=13
a[2,1]=3
a[2,2]=8
a[2,3]=0
a[2,4]=4
a[3,1]=14
a[3,2]=5
a[3,3]=5
a[3,4]=7
a[4,1]=11
a[4,2]=2
a[4,3]=6
a[4,4]=15
introduceti coordonatele de start.
Linia: 3
Coloana: 4
(3,4) (2,4) (2,3) (1,3)
(3,4) (2,4) (2,3) (3,3) (4,3) (4,2)
(3,2) (2,2) (2,1) (1,1)
(3,4) (2,4) (2,3) (2,2) (2,1) (1,1)

n=4
m=4
l[1,1]=5
l[1,2]=7
l[1,3]=5
l[1,4]=13
l[2,1]=3
l[2,2]=8
l[2,3]=0
l[2,4]=4
l[3,1]=14
l[3,2]=5
l[3,3]=5
l[3,4]=7
l[4,1]=11
l[4,2]=2
l[4,3]=6
l[4,4]=15
x=3
y=4
----SOLUTIE---l=3 c=4
----SOLUTIE---l=3 c=4
l=4 c=4
----SOLUTIE---l=3 c=4
l=4 c=4
----SOLUTIE---l=3 c=4
l=4 c=4
l=4 c=3
----SOLUTIE----

38

l=3 c=4
l=3 c=3
l=3 c=2
l=3 c=1
l=2 c=1
----SOLUTIE---l=3 c=4
l=3 c=3
l=3 c=2
l=3 c=1
l=4 c=1
----SOLUTIE---l=3 c=4
l=3 c=3
l=3 c=2
l=3 c=1
l=4 c=1

Problema 29. Algoritmul de umplere a unei suprafee nchise (FILL).


Se d o matrice binar. Valorile 1 delimiteaz o anumit suprafa nchis n
cadrul matricei (elementele aparinnd acestei suprafee sunt marcate cu 0). Se
tiu de asemenea, coordonatele x i y ale unui element al matricei, semnificnd
un punct din interiorul acestei suprafee. S se scrie un program n Pascal i n C
care va umple o suprafa nchis fiind cunoscute coordonatele unui punct situat
n interiorul ei.
Pascal
C
program umplere;
uses crt;
type vector = array[0..10,0..10] of
integer;
var i,j,m,n,x,y:integer;
a: vector;
procedure
scriu(x:integer;y:integer;var
a:vector);
begin
if (a[x,y]=0) then
begin
a[x,y]:=1;
scriu(x+1,y,a);
scriu(x,y+1,a);
scriu(x-1,y,a);
scriu(x,y-1,a);
end;
end;

#include<iostream>
int a[10][10],i,j,m,n,x,y;
using namespace std;
void scriu(int x,int y,int a[10]
[10]){
if(!a[x][y]){
a[x][y]=1;
scriu(x+1,y,a);
scriu(x,y+1,a);
scriu(x-1,y,a);
scriu(x,y-1,a);
}
}
int main(){
cout<<"m=";
cin>>m;
cout<<"n=";
cin>>n;
for(i=1;i<=m;i++)

39

begin
clrscr;
write('m=');
readln(m);
write('n=');
readln(n);
for i:=1 to m do
for j:=1 to n do begin
write('a[',i,',',j,']=');
readln(a[i,j]);
end;
for i:=1 to n do begin
a[0,i]:=1;
a[m+1,i]:=1;
end;
for i:=1 to m do begin
a[i,0]:=1;
a[i,n+1]:=1;
end;
write('x=');
readln(x);
write('y=');
readln(y);
for i:=1 to m do begin
for j:=1 to n do
write(a[i,j],' ');
writeln;
end;
scriu(x,y,a);
writeln;
writeln;
for i:=1 to m do begin
for j:=1 to n do
write(a[i,j],' ');
writeln;
end;
readkey;
end.
m=4
n=4
a[1,1]=0
a[1,2]=1
a[1,3]=1
a[1,4]=0
a[2,1]=0
a[2,2]=0
a[2,3]=0
a[2,4]=1
a[3,1]=0
a[3,2]=1
a[3,3]=1
a[3,4]=1
a[4,1]=1
a[4,2]=0
a[4,3]=0
a[4,4]=0
x=2
y=3
0 1 1 0
0 0 0 1
0 1 1 1

{
";

for(j=1;j<=n;j++)
cout<<"a["<<i<<","<<j<<"]=
cin>>a[i][j];
}
for(i=1;i<=n;i++){
a[0][i]=1;
a[m+1][i]=1;
}
for(i=1;i<=m;i++){
a[i][0]=1;
a[i][n+1]=1;
}
cout<<"x=";
cin>>x;
cout<<"y=";
cin>>y;
for(i=1;i<=m;i++){
for(j=1;j<=n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
scriu(x,y,a);
cout<<endl<<endl;
for(i=1;i<=m;i++){
for(j=1;j<=n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}

m=4
n=4
a[1,1]=0
a[1,2]=1
a[1,3]=1
a[1,4]=0
a[2,1]=0
a[2,2]=0
a[2,3]=0
a[2,4]=1
a[3,1]=0
a[3,2]=1
a[3,3]=1
a[3,4]=1
a[4,1]=1
a[4,2]=0
a[4,3]=0
a[4,4]=0
x=2
y=3
0 1 1 0
0 0 0 1
0 1 1 1

40

1 0 0 0

1 0 0 0

1
1
1
1

1
1
1
1

1
1
1
0

1
1
1
0

0
1
1
0

1
1
1
0

1
1
1
0

0
1
1
0

Problema 30. Problema fotografiei (aplicaie FILL).


O fotografie alb negru este prezentat sub forma unei matrice binare. Ea
nfieaz unul sau mai multe obiecte. Poriunile corespunztoare obiectului n
matrice au valoarea 1. Se cere s se scrie un program Pascal i C care va
determina dac fotografia reprezint unul sau mai multe obiecte.
Exemple:
1
Douobiecte 0
1
1

1
0
1
1

0
0
1
1

0
0
Unobiect
0
1

1
0
1
0

0
1
1
0

1
0
1
0

0
1
1
1

Pascal

program foto;
uses crt;
type vector = array[0..10,0..10] of
integer;
var i,j,m,n,x,y,gasit:integer;
a:vector;
procedure compact(x:integer;
y:integer;var a:vector);

#include <iostream>
using namespace std;
int a[10][10],i,j,m,n,x,y,gasit;
void compact(int x,int y,int a[10]
[10]){
if (a[x][y]){
a[x][y]=0;
compact(x-1,y,a);

41

begin
if (a[x,y]=1) then
begin
a[x,y]:=0;
compact(x-1,y,a);
compact(x-1,y+1,a);
compact(x,y+1,a);
compact(x+1,y+1,a);
compact(x+1,y,a);
compact(x+1,y-1,a);
compact(x,y-1,a);
compact(x-1,y-1,a);
end;
end;
begin
clrscr;
write('m=');
readln(m);
write('n=');
readln(n);
for i:=1 to m do
for j:=1 to n do begin
write('a[',i,',',j,']=');
readln(a[i,j]);
end;
for i:=1 to n do begin
a[0,i]:=0;
a[m+1,i]:=0;
end;
for i:=1 to m do begin
a[i,0]:=0;
a[i,n+1]:=0;
end;
x:=0;
while ((x<>m) and
(a[x,y]<>1)) do begin
x:=x+1;
y:=0;
while((y<>n) and
(a[x,y]<>1))do
y:=y+1;
end;
compact(x,y,a);
gasit:=0;
for i:=1 to m do
for j:=1 to n do
if a[i,j]=1 then
gasit:=1;
if gasit=1 then writeln('Mai
multe obiecte') else
writeln('Un obiect');
readkey;
end.
m=4
n=4
a[1,1]=1
a[1,2]=1
a[1,3]=0
a[1,4]=0
a[2,1]=0
a[2,2]=0

compact(x-1,y+1,a);
compact(x,y+1,a);
compact(x+1,y+1,a);
compact(x+1,y,a);
compact(x+1,y-1,a);
compact(x,y-1,a);
compact(x-1,y-1,a);

}
}
int main(){
cout<<"m=";
cin>>m;
cout<<"n=";
cin>>n;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
cout<<"a["<<i<<","<<j<<"]=
";
cin>>a[i][j];
}
for(i=1;i<=n;i++){
a[0][i]=0;
a[m+1][i]=0;
}
for(i=1;i<=m;i++){
a[i][0]=0;
a[i][n+1]=0;
}
x=0;
do{
x++;
y=0;
do{
y++;
}
while(y!=n && a[x][y]!=1);
}
while((x!=m) && a[x][y]!=1);
compact(x,y,a);
gasit=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if (a[i][j]==1) gasit=1;
if (gasit) cout<<"Mai multe
obiecte";
else cout<<"Un obiect";
return 0;
}

m=4
n=4
a[1,1]=1
a[1,2]=1
a[1,3]=0
a[1,4]=0
a[2,1]=0
a[2,2]=0

42

a[2,3]=0
a[2,4]=1
a[3,1]=1
a[3,2]=1
a[3,3]=1
a[3,4]=1
a[4,1]=1
a[4,2]=1
a[4,3]=1
a[4,4]=1
Mai multe obiecte

a[2,3]=0
a[2,4]=1
a[3,1]=1
a[3,2]=1
a[3,3]=1
a[3,4]=1
a[4,1]=1
a[4,2]=1
a[4,3]=1
a[4,4]=1
Mai multe obiecte

43