Sunteți pe pagina 1din 49

Universitatea de Sat Tiraspol

CatedraInformatic i TehnologiiInformaionale

Aelaborat:
Polevoi Denis
A verificat:
LiubomirChiriac,
dr. hab. prof. univ.

Chisinu, 2012
Cuprins
1
1. Numerele Fibonacci (Pascal i C).

2. Master 2011(Pascal i C).

3. Suma irului de numere 1.

4. Suma irului de numere 2.

5. Celmaimare divizorcomun a 3 numere.

6. Algoritmullui Euclid.

7. Calculareavalorilor a 2 funcii.

8. Recursivitatea indirect a 3 funcii.

9. Mediiartimeticeigeometrice.

10. Funcia Ackerman.

11. FunciaHermite.

12. FunciaCebev.

13.Funcia Manna-Pnuelle

14. Funcia Manna-Pnuelle2.

15. Scrierea unui numar intr-o baza

16. Turnuri din Hanoi

17. Compunerea Funciilor.

18. Descompunerea numarului

19.Cel mai mare divisor comun a n numere

20.Generarea Permutarilor

21.Generarea Aranjamentelor

22.Generarea produsului cartezian

23.Problema comis-voiajorului

24. Problema labirintului

25.Problema fotogtafiei(FILL)

26.Problema individuala

27.Genararea combinarilor

2
Rezolvarea problemelor n varianta recursiv i nerecursiv.

1. Se cere s se afle primele n numere fibonaci, unde n ia valori de la 1..n. S se scrie varianta
recursiv i nerecursiv n Pascal i C.

Sa se scrie varianta recursiva si nerecursiva in Pascal si C.

Varianta recursiv Pascal : Variantanerecursiv Pascal

program fib_rec; Program Fibonacci_Nerecursiv;


uses crt; var f:array[1..100] of integer;
varn,k:integer; n,k:integer;
function fib (k:integer):integer; begin
begin write('Introdu n:');
if (k=1)or (k=2) then fib:=1 else fib:=fib(k- readln(n);
1)+fib(k-2); Writeln('SirulFobonaccieste:');
end; f[1]:=1; f[2]:=1;
begin for k:=3 to n do
write('introdu n=') ; f[k]:=f[k-1]+f[k-2];
readln(n); for k:=1 to n do
Writeln('Sirul Fibonacci este:'); write(f[k],' ');
for k:=1 to n do readln;
write(fib(k),' '); end.
readkey;
end.

Rezultatul: Rezultatul:

3
Variantarecursiv C Variantanerecursiv C

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
int main() int main()
{ {
clrscr(); clrscr();
int f[20]; int f[20];
int n=0; int n=0;
int k; int k;
printf("introdu n="); printf("Introdu n=");
scanf("%d",&n); scanf("%d",&n);
f[0]=1; f[1]=1; f[0]=1; f[1]=1;
for(k=2; k<n; k++) for(k=2; k<n; k++)
f[k]=f[k-1]+f[k-2]; f[k]=f[k-1]+f[k-2];
printf("SirulFibonacieste:"); printf("SirulFibonacieste:");
for ( k=0; k<n; k++) for ( k=0; k<n; k++)
printf("\n%d",f[k]); printf("\n%d",f[k]);
getch(); getch();
return 0; } return 0; }
Rezultatul: Rezultatul:

4
2. Sa se afle primelen numere Master 2012, unde 1..n. S se scrie varianta recursiv i
nerecursiv n Pascal i C. irul de numere Masterat 2011 se definete 1,1,1,3,5,9,17,31,

Variantarecursiv Pascal Varianta nerecursiv Pascal

Program Master2012_REC; Program Master2012_NEREC;


uses crt; uses crt;
varn,k:integer; var f:array[1..100] of integer;
function a(n:integer):integer; n,k:integer;
begin Begin
if (n=0) or (n=1) then a:=1 write('Introdu n=');
if n=2 then a:=2 else readln(n);
a:=a(n-2)*a(n-1)+a(n-3); writeln('Sirul Master este:');
end; f[1]:=1; f[2]:=1; f[3]:=2;
Begin for k:=4 to n do
Clrscr; f[k]:=f[k-1]*f[k-2]+f[k-3];
write('Introdu n='); for k:=1 to n do
readln(n); write(f[k], ' ');
for k:=0 to n do readkey;
writeln('Master',a(k)); end.
readkey; Rezultatul:
end.
Rezultatul:

Variantarecursiv C Varianta nerecursiv C


5
# include <stdio.h> # include <stdio.h>
# include <conio.h> # include <conio.h>
Int n void main()
int fib(intn) {
{ int f[10],n,k;
if(n<2)return 1; clrscr();
if (n==2) return 2; printf ("Introduvaloarealui n=");
if (n>2) return scanf ("%d", &n);
fib(n-2)*fib(n-1)+fib(n-3); f[0]=1;f[1]=1;f[2]=2;
return 0; for(k=3;k<n;k++)
} f[k]=f[k-2]*f[k-1]+f[k-3];
void main() printf("Numerele Master sunt:");
{ for(k=0;k<n;k++)
intn,k; printf("\n%d",f[k]);
cout<<n=; getch();
cin>>; }
cout<<Rez<<fib(n);
getch();
}
Rezultatul: Rezultatul:

6
3. Sa se calculeze suma urmtorului ir de numere: 1, 5, 9, 13, 17, 4n-3, unde n=1..n. S se
scrie varianta recursiv i nerecursiv n Pascal i C

Variantarecursiv Pascal Variantanerecursiv Pascal

program suma_recurs; program Suma_nerec;


uses crt; uses crt;
varn,k:integer; varn,k,s,t:integer;
function S(k:integer):longint; begin
begin clrscr;
if k=0 then S:=0 else S:=S(k-1)+(4*k-3); write('n=');
end; readln(n);
begin s:=0;
clrscr; k:=1;
write('n=');readln(n); t:=4*n-3;
writeln('Suma siruluieste=',S(n)); while k<=t do
readkey; begin
end. s:=s+k;
k:=k+4;
end;
writeln('Suma siruluieste: ', S);
Rezultatul: readkey;
end.
Rezultatul:

7
Variantarecursiv C Varianata nerecursiv C

# include <stdio.h> # include <stdio.h>


# include <conio.h> # include <conio.h>
int sum(int n) void main()
{ {
if(n==0)return 0; inti,n,s;
return sum(n-1)+(4*n-3); clrscr();
} printf ("Introduvaloarealui n=");
void main() scanf ("%d", &n);
{ s=0;i=1;
int n; while (i<4*n)
clrscr(); {
printf ("Introduvaloarealui n="); s+=i;
scanf ("%d", &n); i+=4;
printf("Suma este %d",sum(n)); }
getch(); printf("Valoareasumeieste=%d",s);
} getch();
}
Rezultatul:
Rezultatul:
4. Sa se calculeze suma urmtorului ir de numere: 1, 6, 11, 16, 21, 5n-4, unde n=1..n. S
se scrie varianta recursiv i nerecursiv n Pascal i C

Variantarecursiv Pascal Variantanerecursiv Pascal

program suma_recurs; program Suma_nerec;


uses crt; uses crt;
varn,k:integer; varn,k,s,t:integer;
function S(k:integer):longint; begin
begin clrscr;
if k=0 then S:=0 else S:=S(k-1)+(5*k-4); write('n=');
end; readln(n);
begin s:=0;
clrscr; k:=1;
write('n=');readln(n); t:=5*n-4;
writeln('Suma siruluieste=',S(n)); while k<=t do
readkey; begin
end. s:=s+k;
k:=k+5;
end;
writeln('Suma siruluieste: ', S);
Rezultatul: readkey;
end.
Rezultatul:
Variantarecursiv C Variantanerecursiv C

# include <stdio.h> # include <stdio.h>


# include <conio.h> # include <conio.h>
int sum(int n) void main()
{ {
if(n==0)return 0; inti,n,s;
return sum(n-1)+(5*n-4); clrscr();
} printf ("Introduvaloarealui n=");
void main() scanf ("%d", &n);
{ s=0;i=1;
int n; while (i<5*n)
clrscr(); {
printf ("Introduvaloarealui n="); s+=i;
scanf ("%d", &n); i+=5;
printf("Suma este %d",sum(n)); }
getch(); printf("Valoareasumeieste=%d",s);
} getch();
}

Rezultatul:
Rezultatul:
5. Sa se determine cmmdc a 2 numere naturale prin algoritmul lui Euclid.

varianatarecursiv Pascal Variantanerecursiv Pascal

program euclid_recursiv; program euclid_nerec;


uses crt; uses crt;
vara,b:integer; vara,b:integer;
function cmmdc(a,b:integer):longint; begin
begin clrscr;
if a=b then cmmdc:=a else write('Introduvaloareaprimuluinumar a=');
if a>b then cmmdc:=cmmdc(a-b,b) else readln(a);
cmmdc:=cmmdc(a,b-a); write('Introduvaloareanumarului al doilea b=');
end; readln(b);
begin while a<>b do
clrscr; if a>b then a:=a-b else b:=b-a;
write('Introduvaloareaprimuluinumar a='); writeln('celmai mare divizorcomuneste:',a);
readln(a); readkey;
write('Introduvaloareanumarului al doilea b='); end.
readln(b);
if (a>0) and (b>0)then
writeln('cmmdceste:',cmmdc(a,b))
else writeln('nu calculeazacmmdc');
readkey;
end.
Rezultatul:
Rezultatul:

Variantarecursiv C Variantanerecursiv C

#include<stdio.h> #include <stdio.h>


#include<conio.h> #include <conio.h>
int CMMDC(inta,int b) inta,b;
{ void main()
if(a==b) return a; {
else clrscr();
if(a>b) return CMMDC(a-b,b); printf("Introduvaloarealui a=");
return CMMDC(a,b-a); scanf("\n%d",&a);
} printf("Introduvaloarealui b=");
void main() scanf("\n%d",&b);
{ do
inta,b; {
clrscr(); if (a>b) a=a-b; else b=b-a;
printf("Introduvaloarealui a="); }
scanf("%d",&a); while (a!=b);
printf("Introduvaloarealui b="); printf("cmmdceste:%d", a);
scanf("%d",&b); getch();
printf("CMMDC este:%d",CMMDC(a,b)); }
getch();
} Rezultatul:
Rezultatul:

6. Sunt date 3 numere naturale.S se determine cmmdc a celor3 numere.


Varianatarecursiv Pascal Variantanerecursiv Pascal

program euclid_recursiv; program ppp;


uses crt; uses crt;
varc,a,b:integer; vari,k,a,b,c:integer;
function cmmdc(a,c,b:integer):longint; begin
begin clrscr;
if a=b then cmmdc:=a else writeln('Introdua,b,c');
if a>b then cmmdc:=cmmdc(a-b,b,c) else readln(a,b,c);
cmmdc:=cmmdc(a,c,b-a); if(a>=b)and(a>=c)then k:=a;
if a=c then cmmdc:=a else if(b>=a)and(b>=c)then k:=b;
if a>c then cmmdc:=cmmdc(a-c,c,b) else if(c>=a)and(c>=b)then k:=c;
cmmdc:=cmmdc(a,b,c-a); for i:=k downto 1 do
end; if((a mod i=0)and(b mod i=0)and(c mod
begin i=0))then
clrscr; begin
write('a='); writeln('cmmdc',i);
readln(a); break;
write('b='); end;
readln(b); readkey;
write('c='); end.
readln(c);
if (a>0) and (b>0)then
writeln('cmmdc=',cmmdc(a,b,c))
else writeln('nu calculeazacmmdc');
readkey;
end.

Rezultatul:
Rezultatul:

Variantarecursiv C Variantanerecursiv C

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<iostream.h> #include<iostream.h>
intcmmdc(inta,intb,int c){ inta,b,c,i,k;
if (a==b) return a; void main(){
else if(a>b) return cmmdc(a-b,b,c); clrscr();
else return cmmdc(a,c,b-a); printf("Introdu a sib,c");
if (a==c) return a; scanf("%d%d%d",&a,&b,&c);
else if(a>c) return cmmdc(a-c,c,b); if((a>=b)&&(a>=c))k=a;
else return cmmdc(a,b,c-a);} if((b>=a)&&(b>=c))k=b;
void main(){ if((c>=b)&&(c>=a))k=c;
clrscr(); for(i=k;i>=1;i--)
intb,a,c; if((a%i==0)&&(b%i==0)&&(c%i==0)){
printf("Introdu a sib,c"); printf("cmmdc=%d",i);
scanf("%d%d%d",&a,&b,&c); break;}
printf("cmmdcdintre a si b si c getch();}
%d",cmmdc(a,b,c));
getch();}

Rezultatul:

Rezultatul:

Recursivitatea indirect.

7. Sa se alcatuiasc un program utilizand recursia indirect, calculnd valorile funciilor.


Variantarecursiv Pascal Variantarecursiv C

Program Recursiv_Indirecta1; #include<stdio.h>


uses crt; #include<conio.h>
var x:integer; int g(int x);
function f(x:integer):integer; int f(int x)
forward; {
function g(x:integer):integer; if (x>1) return g(x-2); return x+2;
begin }
if x>=0 then int g(int x)
g:=f(-x)+1 else {
g:=-x if (x>=0) return f(-x)+1; return -x;
end; }
function f; int main()
begin {
if x>1 then clrscr();
f:=g(x-2) else int x;
f:=x+2 printf("Introducetivaloarealui x:");
end; scanf("%d",&x);
Begin printf("g(x)=%d\n",g(x));
writeln('Introdu x:'); printf("f(x)=%d\n",f(x));
readln(x); getch();
writeln('f(x)=',f(x)); }
writeln('g(x)=',g(x)); Rezultatul:
readln;
readkey;
end.
Rezultatul:

8. Elaborati un program utilizand recursia indirecta pentru calcularea urmtoarelor functii:


Sa se scrie varianta recursiva in Pascal si C.

Variantarecursiv Pascal Variantarecursiv C

Program Recursiv_Indirecta3; #include<stdio.h>


uses crt; #include<conio.h>
var x:integer; int a(int x);
function a(x:integer):integer; int b(int x);
forward; int c(int x)
function b(x:integer):integer; {
forward; if(x<=0) return x*x*x; else
function c(x:integer):integer; if (x>=5) return a(x+1); return b(x+1);
begin }
if (x<=0) then int b(int x){
c:=(x*x*x) else if (x>=5) return a(x+5); else
if x<5 then if (x<=0) return (x+1)*(x+1); return c(-1)-1; }
c:=b(x+1) else int a(int x){
c:=a(x)+1; if (x<=3) return x*x; else
end; if (x>=6) return b(x-2); return c(x-1); }
function a; int main(){
begin clrscr();
if x<=3 then a:=(x*x) else int x;
if x<6 then a:=c(x-1) else printf("dati x=");
a:=b(x-2); scanf("%d",&x);
end; printf("\A=%d,",a(x));
function b; printf("\B=%d,",b(x));
begin printf("\C=%d",c(x));
if x<=0 then getch();
b:=(x+1)*(x+1) else return 0;}
if x<5 then Rezultatul:
b:=c(-1)-1 else
b:=a(x)+5;
end;
Begin
writeln('Introdu x:');
readln(x);
writeln('a(x)=',a(x));
writeln('b(x)=',b(x));
writeln('c(x)=',c(x));
readln;
readkey;
end.
Rezultatul:
9. Se consider irul definit curent a0=a, b0=b, a,b>0
S se scrie un program Pascal i C care sciteasca,bi n. S se calculeze ani bn.

Variantarecursiv Pascal Varianta recursiv C

program p1; #include<iostream.h>


uses crt; #include<math.h>
var a,b,n:integer; #include<stdio.h>
function bn(n:integer):integer; forward; #include<conio.h>
function an(n:integer):integer; forward; double a,b;
function bn(n:integer):integer; int n;
begin double bn(int n);
if(n=0)then bn:=b else double an(int n)
bn:=sqrt(an(n-1)*bn(n-1));end; {
function an(n:integer):integer; if (!n) return a;
begin else return (an(n-1)+bn(n-1))/2;
if(n=0)then an:=a else }
an:=an(n-1)+bn(n-1)/2;end; double bn(int n)
begin {
clrscr; if (!n) return b;
write('a=');readln(a); write('b=');readln(b); else return sqrt(an(n-1)*bn(n-1));
write('n=');readln(n); }
writeln('an=',an(n):5:3); void main()
write('bn=',bn(n):5:3); {
readkey; clrscr();
end. cout<<"Introdu a=";cin>>a;
cout<<"Introdu b=";cin>>b;
cout<<"Introdu n=";cin>>n;
Rezultatul: cout<<an(n)<<' '<<bn(n);
getch();
}
Rezultatul:

Calcularea unor functii.

10. Sa se alcatuiasc un program utilizand recursia pentru calcularea formulei Ackerman:


Sa se scrie varianta recursiva in Pascal si C.

Variantarecursiv Pascal Variantarecursiv C

program Ackerman; # include <stdio.h>


uses crt; #include<conio.h>
varm,n:integer; intack(intm,int n)
function Ack(m,n:integer):integer; {
begin if (m==0) return n+1;else
if m=0 then ack:=n+1 else if (n==0) return ack(m-1,1); else
if n=0 then ack:=ack(m-1,1) else return ack(m-1,ack(m,n-1));
ack:=ack(m-1,ack(m,n-1)); }
end; void main()
begin {
clrscr; intm,n;
write('Introducetivaloarealui m='); clrscr();
readln(m); printf("Introducetivaloarealui m=");
write('Introducetivaloarealui n='); scanf("\n%d",&m);
readln(n); printf("Introducetivaloarealui n=");
writeln('Ackerman(',m,',',n,')=',ack(m,n)); scanf("\n%d",&n);
readkey; printf("Rezultatuleste=%d",ack(m,n));
end. getch();
m n Ack(m,n) }
Rezultatul:
0 0 1

0 1 2

1 1 3

1 2 4

2 1 5

2 2 7

3 0 5

3 1 13

3 2 29

3 3 61

3 4 125

4 0 13
Rezultatul:

11. Sa se alcatuiasca un program utilizand recursia pentru calcularea formulei Hermite:

Sa se scrie varianta recursiva in Pascal si C.


Variantarecursiv Pascal Varianta nerecursiv C

program Hermite; #include<stdio.h>


uses crt; #include<conio.h>
varx,n:integer; int H(int x, int n)
function H(n,x:integer):integer; {
begin if (n==0) return 1; else
if n=0 then H:=1 else if (n==1) return 2*x;
if n=1 then H:=2*x else return 2*x*H(n-1,x) - 2*(n-1)*H(n-2,x);
H:=2*x*H(n-1,x)-2*(n-1)*H(n-2,x); }
end; int main ()
begin {
clrscr; clrscr();
write('Introducetivaloarealui n='); int n,x;
readln(n); printf("Introduceti valoarea lui n=");
write('Introducetivaloarealui x='); scanf ("%d", &n);
readln(x); printf("Introduceti valoarea lui x=");
writeln('Hermite(',n,',',x,')=',H(n,x)); scanf ("%d", &x);
readkey; printf("Valoarea polinomului Hermite este=
end. %d",H(n,x));
n x Hermite(m,x) getch ();
}
0 0 1

0 1 1
Rezultatul:
1 0 0

1 1 2

2 1 2

1 2 4

2 2 14

3 1 -4

1 3 6

3 2 40

2 3 34

3 3 180

5 7 25032

Rezultatul:
12. Sa se alcatuiasc un program utiliznd recursia pentru calcularea formulei Cebisev:

Sa se scrie varianta recursiva in Pascal si C.

Variantarecursiv Pascal Varianta recursiv C

program Cebisev; #include<stdio.h>


uses crt; #include<conio.h>
varx,n:integer; int t (int n, int x)
function T(n,x:integer):integer; {
begin if (n==0) return 1; else if (n==1) return 0;
if n=0 then T:=1 else return (2*x*t(n-1,x)-t(n-2,x));
if n=1 then T:=0 else }
T:=2*x*T(n-1,x)-T(n-2,x); int main ()
end; {
begin clrscr;
clrscr; int n, x;
write('n='); printf ("Introduceti n= ");
readln(n); scanf ("%d",&n);
write('x='); printf (" Introduceti x= ");
readln(x); scanf ("%d", &x);
writeln('Cebisev(',n,',',x,')=',T(n,x)); printf ("%d", t(n,x));
readkey; getch();
end. return 0;
n x Cebisev(m,x) }
Rezultatul:
0 0 1

0 1 1

1 0 0

1 1 0

2 1 -1

1 2 0

2 2 -1

3 1 -2

1 3 0

3 2 -4

2 3 -1

3 3 -6

5 7 -2716

Rezultatul:
13. S se alcatuiasc un program utiliznd recursia pentru calcularea funciei Manna Pnueli

Sa se scrie varianta recursiva in Pascal si C.

Variantarecursiv Pascal Varianta recursiv C

program Manna_Pnueli; # include <stdio.h>


uses crt; # include <conio.h>
var x:integer; int f(int x)
function F(x:integer):integer; {
begin if (x>=12) return x-1;
if x>=12 then F:=x-1 else F:=F(F(x+2)); return f(x+2) ;
end; }
begin void main()
clrscr; {
Write('x='); int x;
readln(x); clrscr();
writeln('Manna_Pnueli(',x,')=',F(x)); printf("intr x");
readkey; scanf("\n%d",&x);
end. printf("Rezultatul este= %d",f(x));
Rezultatul: getch();
}
Rezultatul:

14. S se alcatuiasc un program utiliznd recursia pentru calcularea funciei Manna Pnueli2

Sa se scrie varianta recursiva in Pascal si C.

Varianta recursiv Pascal Varianta recursiv C


Program Mana_Pnueli2; #include<conio.h>
var x:integer; #include<stdio.h>
function F(x:integer):integer; int f(int x) {
begin if (x<=5) return x+1; else
if x>=11 then F:=x-1 else (x<=12);
if x<12 then F:=F(F(x+2)); return(x-1);}
end; int main(){
Begin int x;
writeln('Introdu x:'); clrscr();
readln(x); printf("dati x=");
writeln('Mana-Pnueli=',F(x)); scanf("%d",&x);
readln; printf("Manapnueli=%d\n",f(x));
End. getch();
Rezultatul: return 0;}
Rezultatul:

15. Scrierea unui numar intr-o baza : Se citeste un numar natural nenul n,sa se transforme in baza

b, b=2..9

Varianta recursiv Pascal Varianta recursiv C

program recurs; # include <iostream.h>


uses crt; # include <stdio.h>
var n,b:integer; # include <conio.h>
procedure baza(n,b:integer); #include<math.h>
var r:integer; void baza(int n,int b)
begin {int r;
r:=n mod b;
if(n>=b) then baza(n div b,b); r=n % b;
writeln(r); if(n>=b) baza(n/ b,b);
end; cout<<r<<' ';
begin }
clrscr; void main()
writeln('dati n'); {
readln(n); clrscr(); int n;int b;
repeat
writeln('dati b'); cout<<"enter n";
readln(b); cin>>n;
until ((b>=2) and (b<10)); do
baza(n,b); {
readkey; cout<<"enter b";
end. cin>>b;
Rezultatul: }
while(b<=2 && b>10);

cout<<n<<"->";
baza(n,b);
getch();
}
Rezultatul:

16. Turnurile Hanoi.

Se dau 3 tije x, y, z pe tija x se gsesc discuri de diametre diferite aezate n ordine


descresctoare a diametrelor privite de jos n sus se cere s se mute discurile de pe tija x pe
tija y utiliznd tija intermediar z, respectnd urmtoarele reguli:
1) La fiecare pas se mut un singur disc;
2) Nu este permis s se aeze un disc cu diametrul mai mare peste un disc cu diametrul mai
mic.
Exemplu:
Dac n=1, xz;
Dac n=2, xy, xy, zy.

Sa se scrie varianta recursiva in Pascal si C.

Varianta recursiv Pascal Varianta recursiv C


program Turnul_Hanoi; #include<iostream.h>
uses crt; #include<stdio.h>
var x,y,z:char; #include<conio.h>
n:integer; char x,y,z;
procedure Hanoi(n:integer;x,y,z:char); int n;
begin void Hanoi(int n, char x, char y, char z)
if n=1 then writeln(x,y) else {
begin if (n==1) cout<<x<<y<<endl; else
Hanoi(n-1,x,z,y); {
writeln(x,y); Hanoi(n-1,x,z,y);
Hanoi(n-1,z,y,x); cout<<x<<y<<endl;
end; Hanoi(n-1,z,y,x);
end; }
begin }
clrscr; void main()
write('Introdu nr de discuri n='); {
readln(n); clrscr();
Hanoi(n,'x','y','z'); cout<<"Introdu nr de discuri n=";
readkey; cin>>n;
end. x='x';
Rezultatul: y='y';
z='z';
Hanoi(n,x,y,z);
getch();
}
Rezultatul:
17. Compunerea functiilor.

Se dau dou funcii f(x) i g(x) se cere s se determine compunerea funciilor:


1) f(g(f(x))) i 2) g(f(g(x)))

Sa se scrie varianta recursiva in Pascal si C.

Varianta recursiv Pascal Varianta recursiv C

Program functii_compuse; #include<stdio.h>


uses crt; #include<conio.h>
var x:real; #include<math.h>
int f(int x)
function g(x:real):real;
{
begin if (x<5) return 2*pow(x,3)+1;
if (x<=1) then g:=5*x*x-3*x+2 else if (x<8) return pow(x,4)+2;
g:=x*x*x-x+5; return 3;
end; }
function f(x:real):real; int g(int x)
begin {
if (x<5) then f:=2*x*x*x+1 else if (x<=1) return 5*x*x-3*x+2;
if (x<8) then f:=x*x*x*x+2 else f:=3; return pow(x,3)-x+5;
}
end;
void main()
begin {
clrscr; int x;
writeln('Introdu x:'); clrscr();
readln(x); printf("Introdu x=");
writeln('rezultatele sunt: ',f(g(f(x))):4:2); scanf("%d",&x);
write('rezultatele sunt: ', g(f(g(x))):4:2); printf("Rez este=%d",f(g(f(x))));
readln; printf("\nRez este=
end. %d",g(f(g(x))));
getch();
}
x F(g(f(x))) G(f(g(x)))

0 129 4901

1 3 -16123

2 3 29

3 31771 29

4 5883 29

5 3 29

Rezultatul:
18. Se da un numar natural n. Sa se determine toate descompunerele numarului n.

Sa se scrie varianta recursiva in Pascal si C.

Varianta recursiv Pascal Varianta recursiv C

program p_18; # include <iostream.h>


uses crt; # include <conio.h>
var a:array[1..100] of integer; # include <stdio.h>
n,i,s,j:integer; # include <math.h>
procedure desc(j,s,k:integer); int s[20],n;
var i:integer; void tipar(int k)
begin {
if (s=0) and (j=k+1) then begin for (int i=1;i<=k;i++)
for i:=1 to n do cout<<s[i];
j-1 do cout<<endl;
write(a[i],' '); }
writeln(#8,' ') void part(int k,int v)
end {
else int i;
for i:=1 to s do begin s[k]=v;
a[j]:=i; tipar(k);
Desc(j+1,s-i,k); for (i=1;i<=s[k]-1;i++)
end; {
end; s[k]=s[k]-i;
begin part(k+1,i);
clrscr; s[k]=s[k]+i;
write('dati numarul'); }}
readln(n); void main()
for i:=1 to n do {
Desc (1,n,i); clrscr();
readkey; cout<<"n=";
end. cin>>n;
Rezultatul: part(1,n);
getch();
}
Rezultatul:
19. Sunt date n numere naturale. Se cere de determinat cel mai mare divizor comun a n numere
natural nenule introduse de la tastatura

Sa se scrie varianta recursiva in Pascal si C.

Varianta Pascal Varianta recursiv C

program divide_et_impera; #include <stdio.h>


{Se cere de determinat cel mai mare divizor comun a n #include <iostream.h>
numere naturale #include <conio.h>
nenule introduse de la tastatura.} #include <math.h>
uses crt; int x[100],n,i;
type vector=array[1..50] of integer; int cmmdc(int a,int b )
var x:vector; {
n,i:integer; if(a==b)return a;
function cmmdc(a,b:integer):longint; else if(a>b)return cmmdc(a-b,b);
begin else return cmmdc(a,b-a);
if a=b then cmmdc:=a }
else if a>b then cmmdc:=cmmdc(a-b,b) int div(int p,int q)
else cmmdc:=cmmdc(a,b-a); {
end; int m,d1,d2;
function divimp(p,q:integer):longint; if (abs(q-p)>=1)return
var mij,d1,d2:integer; cmmdc(x[p],x[q]);
begin else
if abs(q-p)<=1 then divimp:=cmmdc(x[p],x[q]) {
else m=(p+q)/2;
begin return
mij:=(p+q) div 2; cmmdc(div(p,m),div(m+1,q)) ;
d1:=divimp(p,mij); }
d2:=divimp(mij+1,q); }
divimp:=cmmdc(d1,d2);
end; void main()
end; {
begin clrscr();
clrscr; printf("introdu n");
write('Introdu numarul de elemente, n= '); scanf("%d",&n);
readln(n);
for i:=1 to n do for (i+1;i<=n;i++)
begin {
write('x[',i,']= '); printf("x[%d]=",i);
readln(x[i]); scanf("%d",&x[i]);
end; }
writeln; printf("\n cmmdc=%d",div(1,n));
writeln('Cel mai mare divizor comun :'); getch();
writeln('cmmdc= ',divimp(1,n)); }
readkey;
end.
Rezultatul:
20. Sa se genereza recursiv permutarile

Sa se scrie varianta recursiva in Pascal si C.

Varianta Pascal Varianta recursiv C

program perm_back26; #include <stdio.h>


uses crt; #include <conio.h>
type vector=array[1..25] of integer; int st[50],n;
var st,v:vector;
n:integer; void init()
{
procedure init; int i;
var i:integer; printf("n=");
begin scanf("%d",&n);
write('n='); for (i=1;i<=n;++i) st[i]=0;
readln(n); }
for i:=1 to 25 do st[i]:=0;
end; void tipar(int p)
{
procedure tipar(p:integer); int i;
var i:integer; for (i=1;i<=p;i++) printf("%d ",st[i]);
begin printf("\n");
for i:=1 to p do write(st[i]:4,' '); }
writeln;
end; int valid(int p)
function valid(p:integer):boolean; {
var i:integer; int i,a;
ok:boolean; a=0;
begin for (i=1;i<p;++i)
ok:=true; {
for i:=1 to p-1 do if (st[p]==st[i])
if st[p]=st[i] then {
valid:=ok; a=1;
end; }
}
procedure back; return a;
var p:integer; }
begin
p:=1; void back(int p)
st[p]:=0; {
while p>0 do int pv;
begin for (pv=1;pv<=n;++pv)
if st[p]<n then {
begin st[p]=pv;
st[p]:=st[p]+1; if (valid(p)==0)
if p=n then tipar(p) {
else if (n==p) tipar(p);
begin else back(p+1);
p:=p+1; }
st[p]:=0; }
end; }
end
else void main()
p:=p-1; {
end; clrscr();
end; init();
back(1);
BEGIN getch();
clrscr; }
init;
back;
readln;
END.
Rezultatul :
21. Calcularea aranjamentelor.
Se citesc n, k (k<n) numere natural, se consider c o mulime A are elementele {1,2,,n} se cere s se
afieze toate aranjamentele de n luate cite k, dac k este 1 se va tipri {1}, {2},,{n}.

Sa se scrie varianta recursiva in Pascal si C

Varianta recursiv Pascal Varianta recursiv C

program backtracking_aranjamente; #include <iostream.h>


{Calcularea aranjamentelor}{recursiv} #include<conio.h>
uses crt; #include<stdio.h>
type vector=array[1..25] of integer; int c[10],n,k;
var st:vector; void tipar ()
n,k:integer; { for (int i=1; i<=k; i++) cout <<c[i];
procedure initial; cout <<endl;}
var i:integer; void aranj (int c[10] ,int k, int pas)
begin {
textcolor (2); int i, j,max, m;
write('Introdu n='); if (pas ==k+1)tipar();
readln(n); else
write('Introdu k='); if (pas==1)
readln(k); for(i=1; i<=n; i++)
textcolor (4); { c [pas] =i;
writeln('Aranjamente(',n,',',k,'): '); aranj(c,k, pas+1);
for i:=1 to n do }
st[i]:=0; {initializeaza cu 0 toate elementele stivei} else
end; {max=c[1] ;
function valid(p:integer):boolean; for (i=2; i<=pas-1; i++)
var i:integer; if (c[i]>max) max=c[i];
ok:boolean; for (i=max+1; i<=n; i++)
begin { c[pas]=i;
ok:=true; for (j=1; j<=pas; j++)
for i:=1 to p-1 do { m=c[pas]; c[pas]=c[j]; c[j]=m;
if st[p]=st[i] then ok:=false; aranj(c,k,pas+1);
valid:=ok; m=c[pas]; c[pas]=c[j]; c[j]=m;
end; }}}}
procedure tipar(p:integer); void main ()
var i:integer; {
begin clrscr();
for i:=1 to p do {parcurge nivelele} cout <<"n="; cin>>n;
write(st[i]:4,' '); cout <<"k="; cin>>k;
writeln; aranj (c,k,1);
end; getch();
procedure backtr(p:integer); }
var pval:integer; Rezultatul :
begin
for pval:=1 to n do
begin
st[p]:=pval;
if valid(p) then
if p=k then tipar(p) {se verifica daca solutia este
finala,atunci
solutia se tipareste}
else backtr(p+1); {trece la urmatorul nivel}
end;
end;
Begin
clrscr;
initial;
backtr(1);
readkey;
end.
Rezultatul:
22. Generarea recursiv a produsului cartezian.

Se citete n numrul natural care are semnificaie de numr de mulimi, Se citesc de asemenea
numerele natural n1, n2,,nn unde ni reprezint numrul de elemente Ai. Se cere s se calculeze
produsul cartezian al mulimilor A1xA2xxAn. Se consider c elementele mulimii i sunt i {1,2,
,n}.

Sa se scrie varianta recursiva in Pascal si C

Varianta recursiv Pascal Varianta recursiv C

program backt_prod_cartezian_mult_rec; #include <iostream.h>


uses crt; #include <conio.h>
type vector=array[1..25] of integer; #include <stdio.h>
var st,nr:vector; int p[9], pc[9],n;
n:integer; void tipar()
procedure initial; { for (int i=1; i<=n; i++) cout <<pc[i];
var i:integer; cout<<endl;
begin }
writeln('Introdu n'); void pcart(int pc[9], int pas, int n)
readln(n); { if (pas==n+1) tipar();
for i:=1 to n do else
st[i]:=0; {initializeaza cu 0 toate elementele for (int i=1; i<=p[pas]; i++)
stivei} {pc[pas]=i;
for i:=1 to n do pcart(pc,pas+1,n);
begin }
writeln('Introdu numarul de elemente al multimii }
A',i); void main()
readln(nr[i]); {nr[i] reprezinta numarul de {
elemente al multimii Ai } clrscr();
end; cout <<"numar de multimi= "; cin >>n;
writeln('Produsul cartezian al acestor multimi :'); for (int i=1; i<=n; i++)
end; {cout<<"numar elemente multimea '<<i<< ' ";
function valid(p:integer):boolean; {de fapt nu cin>>p[i];
avem nici o conditie} pcart(pc,1,n);
begin }
valid:=true; getch();
end; }
procedure tipar(p:integer); Rezultatul:
var i:integer;
begin
for i:=1 to p do {parcurge nivelele}
write(st[i],' ');
writeln;
end;
procedure backtr(p:integer);
var pval:integer;
begin
if p=n+1 then tipar(p-1) {daca p a depasit nivelul
de solutii finale
atunci tiparim solutia}
else for pval:=1 to nr[p] do
begin
st[p]:=pval;
if valid(p) then backtr(p+1);
end;
end;
Begin
clrscr;
initial;
backtr(1);
readkey;
end.

Rezultatul:

23. Problema comis-voiajorului: {Se considera n orase numerotate 1,2,,n un


comisvoiajor,trebuie sa-si prezinte produsele in toate cele n orase.Plecind dintr-un oras de
stat trebuind prin fiecare oras exact o singura data si revenind in orasul din care a plecat.Se
stie ca intre unele orase exista drum,iar intre altele nu.Sa se afiseze toate traseele pe care le
poate urma comisvoiajorul}

Sa se scrie varianta recursiva in Pascal si C

Varianta recursiv Pascal Varianta recursiv C

program comis_voiajor; # include<stdio.h>


uses crt; # include<conio.h>
type vector=array[1..25] of integer;
matrice=array[1..25,1..25] of 0..1; int st[50],a[50][50],n;
var st:vector;
a:matrice; void init()
n,start:integer; {n -numarul de orase;start { int i,j;
-orasul de plecare} printf("n="); scanf("%d",&n);
procedure initial;
var k,i,j:integer; for (i=1;i<=n;i++) st[i]=0;
begin
write('Introdu numarul de orase n='); for (i=1;i<=n;++i)
readln(n); for (j=1;j<=n;++j)
write('Introdu orasul de start='); if(i<j)
readln(start); {
for k:=1 to n do printf("a[%d][%d]=",i,j);
st[k]:=0; scanf("%d",&a[i][j]);
for i:=1 to n do a[j][i]=a[i][j];}
a[i,i]:=0; {initializeaza cu 0 diagonala matricei A} printf("or.start");
for i:=1 to n-1 do scanf("%d",&st[1]);
for j:=i+1 to n do }
if i<j then {mai sus de diagonala}
begin void tipar(int p)
write('a[',i,',',j,']= '); {de la tastatura se introduc {int i;
elementele for (i=1;i<=p;++i)
matricei A, formate din 0 si 1 } printf("%d ",st[i]);
readln(a[i,j]); printf("\n");}
a[j,i]:=a[i,j]; {se completeaza elementele mai jos
de diagonala} int valid(int p)
end; { int i,ok;
writeln('Traseele pe care le poate urma comis ok=1;
voiajorul :'); if (a[st[p]][st[p-1]]==0 ) ok=0;
st[1]:=start; {pe primul nivel al stivei se pune for(i=1;i<p;++i)
orasul de start} if (st[i]==st[p]) ok=0;
end; if ((n==p) && (a[st[p]][st[1]]==0)) ok=0;
procedure tipar(p:integer); return ok;}
var i:integer;
begin void back(int p)
for i:=1 to p do {int pv;
write(st[i]:3,' ');
writeln; for (pv=1;pv<=n;pv++)
end; {
function valid(p:integer):boolean; st[p]=pv;
var i:integer; if(valid(p)==1)
ok:boolean; if (n==p)
begin tipar(p); else back(p+1);
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; void main ()
end; {clrscr();
procedure backtr(p:integer); init();
var pval:integer; back(2);
begin getch();
for pval:=1 to n do begin }
st[p]:=pval; {punem un nou oras pe nivelul p} Rezultatul :
if valid(p) then
if (p=n) and (a[st[p],start]=1) then tipar(p)
else backtr(p+1);
end; end;
Begin clrscr;
initial;
backtr(2);
readkey;
end.
Rezultatul:

24. Problema labirintului (backtracking n plan).

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.
Sa se scrie varianta recursiva in Pascal si C

Varianta recursiv Pascal Varianta recursiv C


program labirint; #include<iostream.h>
uses crt; #include<stdio.h>
var st:array[0..100,1..2] of integer; #include<conio.h>
a:array[1..50,1..50] of integer; int l[10] [10], d[2]
n,m,i,j:integer; [100],i,j,m,n,x,y,k;
vecin:array[1..4,1..2] of integer; void tipar(int k, int d[2][100])
Procedure initial; {
begin int i;
writeln('Introdu numarul de linii n'); cout<<"--------------"<<endl;
readln(n); for (i=1; i<=k; i++)
writeln('Introdu numarul de coloane m'); cout<<"l="<<d[10][i]<<'
readln(m); '<<"c="<<d[1][i] <<endl;
for i:=1 to n do }
begin void ies(int x, int y, int& k, int
for j:=1 to m do
l[10][10], int d[2][100])
begin {
write('a[',i,',',j,']= ');
int i, gasit;
readln(a[i,j]); if (l[x][y]==16) tipar(k,d);
end;
else
end; {
for i:=1 to n do begin
k++; d[0] [k]=x; d[1] [k]=y;
for j:=1 to m do gasit=0;
write(a[i,j]:2);
for (i=1; i<=k-1; i++)
writeln; end; if (d[0] [i]==d[0] [k] && d[1]
vecin[1,1]:=-1;vecin[1,2]:=0;
vecin[2,1]:=1;vecin[2,2]:=0; [k]) gasit=1;
vecin[3,1]:=0;vecin[3,2]:=1; if (!gasit)
vecin[4,1]:=0;vecin[4,2]:=-1; for(i=1; i<=4; i++)
{writeln('vecinii:'); switch(i)
for i:=1 to 4 do begin {
case 1: { if (l[x][y] & (int)8)
write('v[',i,1,']=');readln(vecin[i,1]); ies(x-1, y,k,l,d); break;}
case 2: { if (l[x][y] & (int)4)
write('v[',i,2,']=');readln(vecin[i,2]); ies(x,y+1,k,l,d); break;}
end; } case 3: { if (l[x][y] & (int)2)
st[0,1]:=3; {coordonatele initiale ale ies(x+1, y,k,l,d); break;}
persoanei} case 4: if (l[x][y] & (int)1)
st[0,2]:=3; ies(x,y-1,k,l,d);
end; }
function valid(p:integer):boolean; k--;
var k:integer; }
begin }
valid:=false;
if a[st[p,1],st[p,2]]=1 then void main()
begin {
valid:=true; clrscr();
for k:=0 to p-2 do cout<<"n="; cin>>n;
if (st[p,1]=st[k,1]) and cout<<"m="; cin>>m;
(st[p,2]=st[k,2]) then valid:=false; for (i=1; i<=m; i++)
end; for (j=1; j<=n; j++)
end; {
function final(p:integer):boolean; cout <<"l["<<i<<","<<j<<"]=";
begin cin>>l[i] [j];}
final:=false; cout<<"x="; cin>>x;
cout<<"y="; cin>>y;
if (st[p,1]=1) or (st[p,1]=n) or for (i=1; i<=n; i++)
(st[p,2]=1) or (st[p,2]=m) then {
final:=true; l[0] [i]=16;
{daca am iesit pe o linie sau coloana l[i] [n+1]=16;
}
externa,inseamna ca am iesit
k=0;
din labirint} end;
ies(x,y,k,l,d);
procedure tipar(p:integer);
getch();
var k:integer;
}
begin
for k:=0 to p do Rezultatul:
write(st[k,1],', ',st[k,2],' ');
writeln;
end;
procedure backtr(p:integer);
var pval:integer;
begin
for pval:=1 to 4 do
begin
st[p,1]:=st[p-1,1]+vecin[pval,1];
st[p,2]:=st[p-1,2]+vecin[pval,2];
if valid(p) then
if final(p) then tipar(p)
else backtr(p+1);
end;
end;
Begin
clrscr;
initial;
backtr(1);
readkey;
End.
Rezultatul:
25. Problema fotografiei (aplicaie FILL)

O fotografie alb-negru este prezentat sub forma unei matrice binare. Ea nfiseaz unul sau mai multe
obiecte. Poriunile corespunztoare obiectului n matrice au valoarea 1. Se cere s se determine dac
fotografia reprezint unul sau mai multe obiecte.

Varianta recursiv C
#include <iostream.h>
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);
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);}}
void main(){
cout<<"m="; cin>>m;
cout<<"n="; cin>>n;
for (i=1; i<=m;i++)
for (j=1; i<=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";}
Rezultatul:
26. Problem individual

S se scrie un ir de numer din 5 n 5, la valoarea lu n indina numarul de numere

Varianta recursiv Pascal Varianta recursiv C


Program Master2012; # include <stdio.h>
uses crt; # include <conio.h>
var n,k:integer; int fib(int k)
function a(n:integer):integer; {
begin if(k<1)return 5; else
if (n=0) then a:=5 else return fib(k-1)+5;
if n=0 then a:=5 else }
a:=a(n-1)+5; void main()
end; {
Begin int n,k;
clrscr; clrscr();
write('Introdu n='); printf ("Introdu valoarea lui n=");
readln(n); scanf ("%d", &n);
for k:=0 to n do printf("Numerele Master sunt:");
writeln('Nr. este ',a(k)); for(k=0;k<n;k++)
readkey; printf("\n%d",fib(k));
end. getch();
Rezultatul: }

Rezultatul:

Varianta nerecursiv Pascal


Varianta nerecursiv C
Program Master2012_NEREC;
uses crt; # include <stdio.h>
var f:array[1..100] of integer; # include <conio.h>
n,k:integer; void main()
Begin {
clrscr; int f[10],n,k;
write('Introdu n='); clrscr();
readln(n); printf ("Introdu valoarea lui n=");
writeln('Sirul din 5 in 5 este:'); scanf ("%d", &n);
f[1]:=5; f[2]:=10; f[3]:=15; f[0]=5;f[1]=10;f[2]=15;
for k:=4 to n do for(k=2;k<n;k++)
f[k]:=f[k-1]+5; f[k]=f[k-1]+5;
for k:=1 to n do printf("Numerele Master sunt:");
write(f[k], ' '); for(k=0;k<n;k++)
readkey; printf("\n%d",f[k]);
end. getch();
}
Rezultatul:
27. Generarea recursiv a combinrilor.

Se citesc 2 numere naturale n i k k<n se consider o mulime cu elemente 1, 2,...,n i se cere s


se tipreasc toate submulimile sale care au k-elemente .

Sa se scrie varianta recursiva in Pascal si C

Varianta recursiv Pascal Varianta recursiv C

program back_combinari_rec; #include <iostream.h>


uses crt; int c[10],n,k;
type vector=array[1..25] of integer; void tipar()
var st:vector; {
n,k:integer; for (int i=1; i<=k; i++) cout <<c[i];
procedure initial; cout<<endl;
var i:integer; }
begin void combin(int c[10],int k, int pas)
writeln('Introdu n'); {
readln(n); int i;
writeln('Introdu k'); if (pas==k+1) tipar();
readln(k); else
writeln('Combinari(',n,',',k,'): '); if (pas==1)
for i:=1 to n do for (i=1; i<=n; i++)
st[i]:=0; {initializeaza cu 0 toate elementele stivei} {
end; c[pas]=i;
function valid(p:integer):boolean; {testeaza daca combin(c,k,pas+1);
valorile de pe stiva }
sunt aranjate in ordine crescatoare} else
var i:integer; for (i=c[pas-1]+1;i<=n; i++)
ok:boolean; {
begin c[pas]=i;
ok:=true; combin(c,k,pas+1);
for i:=1 to p-1 do }
if st[p]<=st[i] then ok:=false; }
valid:=ok; void main()
end; {
procedure tipar(p:integer); cout<<"n= "; cin>>n;
var i:integer; cout<<"k= "; cin>>k;
begin combin(c,k,1);
for i:=1 to p do {parcurge nivelele} }
write(st[i]:4,' '); Rezultatul:
writeln;
end;
procedure backtr(p:integer);
var pval:integer;
begin
for pval:=1 to n do
begin
st[p]:=pval;
if valid(p) then
if p=k then tipar(p) {se verifica daca solutia este
finala,atunci
solutia se tipareste}
else backtr(p+1); {trece la urmatorul nivel}
end;
end;
Begin
clrscr;
initial;
backtr(1);
readkey;
end.
Rezultatul: