Sunteți pe pagina 1din 23

Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

Problema 1: Numerele Fibonacci

S se elaboreze un program recursiv/nerecursiv care afieaz irul lui Fibonacci: 1,1,2,3,5,8,13,...

program p1_recursiv; program p1_nerecursiv;


uses crt; uses crt;
var n,k:integer; var a:array[1..100] of integer;
function f(n:integer):integer; n,k:integer;
begin begin
if (n=0) or (n=1) then f:=1 clrscr;
else f:=f(n-1)+f(n-2); write('dati n');
end; readln(n);
begin a[1]:=1;
clr scr; a[2]:=2;
write('dati n'); for k:=3 to n do
readln(n); a[k]:=a[k-1]+a[k-2];
for k:=0 to n do writeln('sirul fibonaci este',f(k)); for k:=1 to n do
readkey; writeln('sirul fibonaci este',a[k]);
end. readkey;
end.

Rezolvare: Rezolvare:
dati n6 dati n8
sirul fibonaci este1 sirul fibonaci este1
sirul fibonaci este1 sirul fibonaci este2
sirul fibonaci este2 sirul fibonaci este3
sirul fibonaci este3 sirul fibonaci este5
sirul fibonaci este5 sirul fibonaci este8
sirul fibonaci este8 sirul fibonaci este13
sirul fibonaci este13 sirul fibonaci este21
sirul fibonaci este34
Programul n Pascal

Programul n C
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<iostream.h> void main()
int fib(int k) {int f[10];
{if(k<2) return 1; int n,k;
return fib(k-1)+fib(k-2); clrscr();
} printf("dati n=");
void main() scanf("%d",&n);
{int n,k; f[0]=1;f[1]=1;
clrscr(); for(k=2;k<n;k++)
printf("datin n"); f[k]=f[k-1]+f[k-2];
scanf("%d",&n); printf('sirul fibonacci este");
printf("sirul fibonacci este"); for(k=0;k<n;k++)
for(k=0;k<n;k++) printf("\n%d",f[k]);
printf("\n%d",fib(k)); getch();
getch(); }
} Rezolvare:
Rezolvare: datin n=5
datin n5 sirul fibonacci este
sirul fibonacci este 1
1 1
1 2
2 3

1
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

3 5
5
Problema 2: Master 2012
S se elaboreze un program care afieaz urmtorul ir 1,1,2,3,7,25,

Programul in Pascal
program p2_recursiv; program p2_nerecursiv;
uses crt; uses crt;
var n,k:integer; var a:array[1..100] of integer;
function a(n:integer):integer; n,k:integer;
begin begin
if (n=0) or (n=1) then a:=1 clrscr;
else if (n=2) then a:=2 write('dati n');
else readln(n);
a:=a(n-1)*a(n-2)+a(n-3); a[1]:=1;
end; a[2]:=1;
begin a[3]:=2;
clrscr; for k:=4 to n do
write('dati n'); a[k]:=a[k-1]*a[k-2]+a[k-3];
readln(n); for k:=1 to n do
for k:=0 to n do writeln('sirul dat este',a[k]);
writeln('sirul este',a(k)); readkey;
readkey; end.
end. Rezolvare:
Rezolvare: dati n5
dati n6 sirul dat este1
sirul este1 sirul dat este1
sirul este1 sirul dat este2
sirul este2 sirul dat este3
sirul este3 sirul dat este7
sirul este7
sirul este23
sirul este164

Programul in C
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<iostream.h> #include<iostream.h>
int f(int n) void main()
{ {int a[20];
if(n<2) return 1; int n,k;
if(n==2) return 2; clrscr();
if(n>2) printf("dati n=");
return f(n-2)*f(n-1)+f(n-3); } scanf("%d",&n);
void main() a[0]=1; a[1]=1; a[2]=2;
{int n,k; for(k=3;k<n;k++)
clrscr(); a[k]=a[k-1]*a[k-2]+a[k-3];
printf("datin n"); printf("sirul dat este");
scanf("%d",&n); for(k=0;k<n;k++)
printf("sirul dat este"); printf("\n%d",a[k]);
for(k=0;k<n;k++) getch();
printf("\n%d",f(k)); }
getch(); Rezolvare:
} dati n=4
Rezolvare: sirul dat este
datin n4 1
sirul dat este 1
1 2
1 3
2
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

2
3
Problema 3: iruri de numere
S se calculeze suma elementelor din irul 1,5,9,13,17,21,,4n-3,

Program Pascal
program p3_recursiv; program p3_nerec;
uses crt; uses crt;
var n,k:integer; var s,n,k,t:integer;
function suma(n:integer):integer; begin
begin writeln(dati n=);
if(n=0) then suma:=0 readln(n);
else suma:=suma(n-1)+4*n-3; s:=0;
end; k:=1;
begin t:=4*n-3;
clrscr; while k<=t do begin
write(dati n); s:=s+k;
readln(n); k:=k+4;
writeln(suma este,suma(n)); end;
readkey; write(suma este=,s);
end. readkey;
Rezolvare: end.
dati n5 Rezolvare:
suma este45 dati n=3
suma este=15

Program C
#include<iostream.h> #include<stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h> void main()
int suma(int n) {
{if(n==0) return 0; int n,k,sum;
return (suma(n-1)+4*n-3); clrscr();
} printf("dati n=");
void main(); scanf("%d",&n);
{int n; sum=0;
clrscr(); if(n>=1)
printf("dati n"); for(k=1;k<=n;k++)
scanf("%d",&n); {sum=sum+(4*k-3);
printf("suma sirului este");
printf("\n%d",suma(n)); printf("suma sirului este");
getch(); printf("\n%d",sum);
} getch();
Rezolvare: }
dati n5 Rezolvare:
suma sirului este45 dati n=3
suma sirului este15

Problema 4:ir de numere 2


S se calculeze suma elementelor din irul 1,6,11,16,21, . S se alctuiasc formula de recuren.

Programul Pascal
program p4_rec; program p4_nerec;
uses crt; uses crt;
var n,k:integer; var s,n,k,t:integer;

3
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

function s(n:integer):integer; begin


begin writeln('dati n=');
if n=0 then s:=0 else readln(n);
s:=s(n-1)+5*n+1; s:=0;
end; k:=1;
begin t:=5*n-4;
clrscr; while k<=t do begin
write('dati n='); s:=s+k;
readln(n); k:=k+5;
writeln('suma este=',s(n)); end;
readkey; write('suma este=',s);
end. readkey;
Rezolvare: end.
dati n=4 Rezolvare:
suma este=34 dati n=5
suma este=55

Programul C
#include<iostream.h> #include<stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h> void main()
int suma(int n) {
{if(n==0) return 1; int n,k,sum;
return (suma(n-1)+5*n+1); clrscr();
} printf("dati n=");
void main(); scanf("%d",&n);
{int n; sum=0;
clrscr(); if(n>=1)
printf("dati n"); for(k=0;k<=n;k++)
scanf("%d",&n); {sum=sum+(5*k+1);
printf("suma sirului este"); printf("suma sirului este");
printf("\n%d",suma(n)); printf("\n%d",sum);
getch(); getch();
} }
Rezolvare: Rezolvare:
dati n4 dati n=5
suma sirului este34 suma sirului este55

Problema 5: Algoritmul lui Euclid pentru 2 numere


Fie dat 2 numere a i b ntregi. Se cere de aflat cel mai mare divisor comun a numerelor date aplicnd algoritmul lui
Euclid.

Programul Pascal
program p5_recursiv; program p5_nerec;
uses crt; uses crt;
var a,b:integer; var a,b,a1,b1:integer;
function cmmdc(a,b:integer):integer; begin
begin clrscr;
if a=b then cmmdc:=a write('dati a,b=');
else if a>b then cmmmdc:=cmmdc(a-b,b) read(a,b);
else if a<b then cmmdc:=cmmdc(a,b-a); a1:=a;
end; b1:=b;
begin while a<>b do
clrscr; if a>b then a:=a-b
write('dati a si b'); else b:=b-a;
read(a,b); writeln('cmmdc(',a1,';',b1,')=',a);

4
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

if (a>0) and (b>0) then readkey;


writeln('cmmdc(',a,';',b,')=',cmmdc(a,b)); end.
readkey;
end. Rezolvare:
Rezolvare: dati a,b=33
dati a si b45 27
15 cmmdc(33;27)=3
cmmdc(45;15)=15

Programul C
#include<conio.h> #include<conio.h>
#include<stdio.h> #include<stdio.h>
int cmmdc(int a,b) int a,b,a1,b1;
{ int main()
if(a==b) return a; {
else if(a>b) return cmmdc(a-b,b); clrscr();
else if(a<b) return cmmdc(a,b-a); cout << "introdu a:";
} cin >> a1; a=a1;
void main() cout << "introdu b:";
{ cin >> b1; b=b1;
int a,b; a1=a;
printf("dati 2 numere"); b1=b;
scanf("%d",&a); while (a!=b)
scanf("%d",&b); {if (a>b) a=a-b;
printf("cmmdc=%d",cmmdc(a,b)); else b=b-a;
getch(); }
} cout<<cmmdc este=<<a;
Rezolvare: getch();
dati 2 numere 45 }
15 Rezolvare:
cmmdc(a;b)=15 introdu a:45
introdu b:15
cmmdc este=15

Problema 6: Algoritmul lui Euclid pentru 3 numere

Fie date 5 numere a, b i c ntregi. Se cere de aflat cel mai mare divisor comun a numerelor date aplicnd algoritmul
lui Euclid.

program p6_recursiv; Program p6; {NEREC}


uses crt; uses crt;
var a,b,c:integer; var a,b,c,a1,b1,c1:integer;
function cmmdc(a,b:integer):integer; Begin
begin writeln('introdu a:'); read(a1); a:=a1;
if a=b then cmmdc:=a writeln('introdu b:'); read(b1); b:=b1;
else writeln('introdu c:'); read(c1); c:=c1;
if a>b then while a<>b do
cmmdc:=cmmdc(a-b,b) if a>b then a:=a-b
else else b:=b-a;
if a<b then while a<>c do
cmmdc:=cmmdc(a,b-a); if a>c then a:=a-c
end; else c:=c-a;
begin
clrscr; if (a>0) and (b>0) and (c>0) then
write('dati a ,b si c');
read(a,b,c); writeln('NOD(',a1,',',b1,',',c1,')=',a);
if (a>0) and (b>0) and (c>0) then readln;

5
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

writeln('cmmdc(cmmdc(',a,';',b,');',c,')=', ENd.
cmmdc(cmmdc(a,b),c)); Rezolvare:
readkey; introdu a:
end. 45
Rezolvare: introdu b:
dati a ,b si c 33
45 introdu c:
33 6
6 NOD(45,33,6)=3
cmmdc(cmmdc(45;33);6)=3

Programul C
#include<stdio.h> #include <iostream.h> // NEREC
#include<conio.h> #include <conio.h>
#include<iostream.h> #include <stdio.h>
int cmmdc(int a,int b) int a,b,c,a1,b1,c1;
{ if(a==b) return a; int main()
else if (a>b) return cmmdc(a-b,b); {
else return cmmdc(a,b-a); clrscr();
} cout << "introdu a:";
void main() cin >> a1; a=a1;
{ cout << "introdu b:";
int a,b,c; cin >> b1; b=b1;
clrscr(); cout << "introdu c:";
cout<<"dati 3 numere"; cin >> c1; c=c1;
cin>>a>>b>>c; while (a!=b)
cout<<"cmmdc este="<<cmmdc(cmmdc(a,b),c); {if (a>b) a=a-b;
getch(); else b=b-a;
} }
Rezolvare: while (a!=c)
dati 3 numere45 {if (a>c) a=a-c;
255 else c=c-a;
125 }
cmmdc este=5 if ((a>0) && (b>0) && (c>0))
cout<<"NOD("<<a1<<","<<b1<<","<<c1<<")="<<a;
return 0;
getch();
}
Rezolvare:
introdu a:2
introdu b:4
introdu c:6
NOD(2,4,6)=2

Problema 7: Recursivitatea indirect la calcularea funciilor f(x) i g(x)


S se alctuiasc un program utiliznd recursia indirect pentru calcularea funciilor:

g (x) 2, x 1 f (x) 1, x 0
f ( x) g ( x)
x 2, x 1 x , x 0
program recurs; #include<stdio.h>
uses crt; #include<conio.h>
var x:integer; int g(int);
function f(x:integer):integer; int f(int x);
forward; {if(x>1) return g(x)-2;
function g(x:integer):integer; else return x+2;
begin }
if (x>=0) then g:=(-x)+1 else g:=-x int g(int x)
end; {
6
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

function f; if(x>=0) return f(-x)+1;


begin else return -x;
if x<=1 then f:=x+2 else f:=g(x)-2; }
end; void main()
begin {int x;
clrscr; printf("dati x=");
writeln('dati x'); scanf("%d",&n);
readln(x); printf("f(%d)=%d\n",x,f(x));
writeln('f(x)=',f(x)); printf("g(%d)=%d\n",x,g(x));
writeln('g(x)=',g(x)); getch();
readkey; }
end. Rezolvare:
Rezolvare: dati x=5
dati x5 f(x)=-6
f(x)=-6 g(x)=-4
g(x)=-4

Problema 8: Recursivitatea indirect la calcularea funciilor a(x), b(x) i c(x)

S se alctuiasc un program utiliznd recursia indirect pentru calcularea funciilor:

x , x 3
2
( x 1) , x 0
2 3
x , x 0

a( x) c( x 2),3 x 6 b( x) c(1) 1, 0 x 5 c( x) b( x 1),0 x 5
b( x 2), x 6 a( x) 5, x 5 a( x 1), x 5

program recurs; #include<stdio.h>
uses crt; #include<conio.h>
var x:integer; int a(int);
function a(x:integer):integer; int b(int);
forward; int c(int x)
function b(x:integer):integer; {
forward; if (x<=0) return x*x*x;
function c(x:integer):integer; else if (x<5) return b(x+1);
begin else return a(x+1);
if (x<=0) then c:=x*x*x else }
if (x<5) then c:=b(x+1) else c:=a(x+1); int b(int x)
end; {
function b; if (x<=0) return (x+1)*(x+1);
begin else if (x<5) return c(-1)-1;
if x<=0 then b:=(x+1)*(x+1) else else return a(x)+5;
if (x<5) then b:=c(-1)-1 }
else b:=a(x)+5; int a(int x)
end; {
function a; if(x<=3) return x*x;
begin else if(x<6) return c(x-2);
if x<=3 then a:=x*x else
else return b(x-2);
if x<6 then a:=c(x-2) }
else a:=b(x-2); void main()
end; {
begin int x;
clrscr; clrscr();
writeln('dati x'); printf("introdu x");
readln(x); scanf("%d",&x);
writeln('a(x)=',a(x)); printf("a(%d)=%d \n",x,a(x));
writeln('b(x)=',b(x)); printf("b(%d)=%d \n",x,b(x));
writeln('c(x)=',c(x)); printf("c(%d)=%d \n",x,c(x));
readkey; getch();
end. }
7
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

Rezolvare: Rezolvare:
dati x introdu x3
6 a(3)=9
a(x)=-2 b(3)=-2
b(x)=3 c(3)=-2
c(x)=3

Problema 9: Recursivitatea indirect la calcularea mediei aritmetice i geometrice

S se alctuiasc un program utiliznd recursia indirect pentru calcularea mediilor:


a0 a, b0 b a, b 0
a bn1 bn
an n1 an1 * bn1
2
program pr9; #include<iostream.h>
uses crt; #include<conio.h>
var n,a0,b0:integer; #include<math.h>
function a(n:integer):real; #include<stdio.h>
forward; double a0,b0;
function b(n:integer):real; int n;
begin double b(int n);
if n=0 then b:=b0 double a(int n)
else b:=sqrt(a(n-1)*b(n-1)); {
end; if (n==0) return a0;
function a; else return (a(n-1)+b(n-1))/2;
begin }
if n=0 then a:=a0
else a:=(a(n-1)+b(n-1))/2; double b(int n)
end; {
begin if(n==0) return b0;
clrscr; else return sqrt(a(n-1)*b(n-1));
write('dati n='); }
readln(n); void main()
write('dati a0='); {
readln(a0); clrscr();
writeln('dati b0='); cout<<"dati n=";
readln(b0); cin>>n;
writeln('a(n)=',a(n)); cout<<"dati a0=";
writeln('b(n)=',b(n)); cin>>a0;
readkey; cout<<"dati b0=";
end. cin>>b0;
Rezolvare: cout<<"a("<<n<<")="<<a(n)<<endl;
dati n=1 cout<<"b(n)="<<b(n);
dati a0=2 getch();
dati b0= }
8 Rezolvare:
a(n)= 5.0000000000E+00 dati n=1
b(n)= 4.0000000000E+00 dati a0=2
dati b0=8
a(n)=5
b(n)=4

Problema 10: Funcia lui Ackermann


S se elaboreze un program care citete de la tastatur numerele naturale m, n i afieaz pe ecran valoarea funciei
lui Ackermann:

8
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

n 1, m 0

a(m, n) a(m 1,1), n 0
a(m 1, a(m, n 1)), m 0and n 0

program recurs; #include<stdio.h>
uses crt; #include<conio.h>
var n,m:integer; int ack(int m,int n)
function ack(m,n:integer):integer; {
begin if(m==0) return n+1;
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));
else ack:=ack(m-1,ack(m,n-1)); }
end; void main()
begin {
clrscr; int m;int n;
writeln('dati m,n'); printf("introdu m si n");
readln(m,n); scanf("%d,%d",&m,&n);
writeln('ack(m,n)=',ack(m,n)); printf("ack(%d,%d)=%d",m,n,ack(m,n));
readkey; getch();
end. }
Rezolvare: Rezolvare:
dati m,n introdu m si n2
21 1
ack(m,n)=5 ack(2,1)=5

Problema 11
S se elaboreze un program care citete de la tastatur numerele naturale m, n i afieaz pe ecran valoarea funciei
lui Hermite:

1, n 0

H n ( x ) x, n 1
H ( x) (n 1) H ( x), n 2
n1 n2
program p11; #include<stdio.h>
uses crt; #include<conio.h>
var n,x:integer; int h(int n,int x)
function h(n,x:integer):integer; {
begin if(n==0) return 1;
if n=0 then h:=1 else
else if n=1 then h:=x if(n==1) return x;
else h:=h(n-1,x)-(n-1)*h(n-2,x); else return h(n-1,x)-(n-1)*h(n-2,x);
end; }
begin void main()
clrscr; {
write('dati n si x'); int n;int x;
read(n,x); clrscr();
write('h(',n,';',x,')=',h(n,x)); printf("introdu n ");
readkey; scanf("%d",&n);
end. printf("introdu x ");
scanf("%d",&x);
Rezolvare: printf("h(%d,%d)=%d \n",n,x,h(n,x));
introdu n 3 getch();
introdu x 2 }
h(3,2)=-3 Rezolvare:
dati n si x3
9
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

2
h(3;2)=-3

Problema 12
S se elaboreze un program care citete de la tastatur numerele naturale m, n i afieaz pe ecran valoarea funciei
lui Cebev:

1, n 0

Tn ( x) 0, n 1
2 x T ( x) T ( x), n 2
n1 n2
program recurs; #include<stdio.h>
uses crt; #include<conio.h>
var n,x:integer; int T(int n,int x)
function T(n,x:integer):integer; {
begin if(n==0) return 1;
if n=0 then T:=1 if(n==1) return 0;
else if n=1 then T:=0 else return 2*x*T(n-1,x)-T(n-2,x);
else T:=2*x*T(n-1,x)-T(n-2,x); }
end; void main()
begin {
clrscr; int n;int x;
writeln('dati n,x'); clrscr();
readln(n,x); printf("introdu n");
writeln('T(n,x)=',T(n,x)); scanf("%d",&n);
readkey; printf("introdu x ");
end. scanf("%d",&x);
printf("T(%d,%d)=%d \n",n,x,T(n,x));
Rezolvare: getch();
dati n,x }
11 Rezolvare:
T(n,x)=0 introdu n1
introdu x 1
T(1,1)=0

Problema 13
S se elaboreze un program care citete de la tastatur numerele naturale m, n i afieaz pe ecran valoarea funciei
lui Manna Pnueli:

x 1, x 12
F ( x)
F (F (x 2)), x 12
program p13; #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 return f(f(x+2));
else
f:=f(f(x+2)); }
void main()
end; {
begin int x;
clrscr; clrscr();
write('dati x'); printf("introdu x ");
read(x); scanf("%d",&x);
10
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

write('f(',x,')=',f(x));
readkey; printf("f(%d)=%d \n",x,f(x));
end. getch();
Rezolvare: }
dati x13 Rezolvare:
f(13)=12 introdu x 13
f(13)=12

Problema 14: Manna Pnueli 2


S se elaboreze un program care citete de la tastatur numerele naturale m, n i afieaz pe ecran valoarea funciei
lui Manna Pnueli 2:

F ( x 1), x 5

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

program p14; #include<stdio.h>
uses crt; #include<conio.h>
var x:integer; int f(int x)
function f(x:integer):integer; {
begin if(x<5) return f(x+1);
if x<5 then f:=f(x+1) else if(x<12) return f(f(x+2));
else else return x-1;
if x<=12 then f:=f(f(x+2))
else }
f:=x-1; void main()
end; {
begin int x;
clrscr; clrscr();
write('dati x'); printf("introdu x ");
read(x); scanf("%d",&x);
write('f(',x,')=',f(x));
readkey; printf("f(%d)=%d \n",x,f(x));
end. getch();
}
Rezolvare: Rezolvare:
dati x13 introdu x 13
f(13)=12 f(13)=12

Problema 15: Scrierea unui numr zecimal n baza

Scrierea unui numr ntr-o baz. Se citete un numr natural nenul n. S se transforme numrul
n baza b, unde b=2..9.
program recurs; #include<iostream.h>
uses crt; #include<conio.h>
var n,b:integer; #include<stdio.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; r=n % b;
if(n>=b) then baza(n div b,b); if(n>=b)
writeln(r); baza(n / b,b);
end; cout<<r<<' ';
begin }
clrscr; void main()
writeln('dati n'); {

11
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

readln(n); clrscr();
repeat int n;int b;
writeln('dati b'); cout<<"n=";cin>>n;
readln(b); do {
until ((b>=2) and (b<10)); cout<<"b=";cin>>b;
baza(n,b); }
readkey; while(b<=2 && b>10);
end. cout<<n<<"->";
Rezolvare: baza(n,b);
dati n getch();
20 }
dati b Result:
2 n=7
1 b=2
0 7->1 1 1
1
0
0

Problema 16: Turnurile din Hanoi


Se dau 3 tije x,y,z i se dau n discuri situate pe tija x n ordine descresctoare a diametrelor formnd astfel un turn.
Se cere s se mute cele n discuri pe y, folosind tija intermediar z i respectnd urmtoarele reguli:
La fiecare micare se mut un singur disc
Un disc nu poate fi aezat pe unul de diametru mai mic

program p16; #include<iostream.h>


uses crt; #include <stdio.h>
var n:integer; #include <conio.h>
x,z,y:char; void Hanoi(int n,char x,char y,char z){
procedure Hanoi(n:integer;x,y,z:char); if (n==1) cout<<x<<y<<' ';
begin else {
if n=1 then Writeln(x,y) Hanoi(n-1,x,z,y);
else begin cout<<x<<y<<' ';
Hanoi(n-1,x,z,y); Hanoi(n-1,z,y,x);
writeln(x,y); }
Hanoi(n-1,z,y,x); }
end;end; void main()
begin {
clrscr; clrscr();
write('introdu n='); int n;char x,y,z;
readln(n); cout<<"dati n";
writeln('pasii '); cin>>n;
Hanoi(n,'x','y','z'); cout<<"pasii ";
readkey; Hanoi(n,'x','y','z');getch();
end. }
Rezolvare: Rezolvare:
introdu n=3 dati n1
pasii pasii xy
xy dati n2
xz pasii xz xy zy
yz dati n3
xy pasii xy xz yz xy zx zy xy
zx dati n4
zy pasii xz xy zy xz yx yz xz xy zy zx yx zy xz xy zy
xy

12
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

Problema 17 Compoziia a 2 funcii f(x) i g(x)


Se dau 2 funcii f(x) i g(x).

Se cere s se determine compoziia funciilor:


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

program p17; #include<iostream.h>


uses crt; #include <stdio.h>
var x:real; #include <conio.h>
function f(x:real):real; #include <math.h>
begin double x;
if x<5 then f:=2*x*x*x+1 double f(double x)
else if(x<8) then f:=sqr(x)*sqr(x)+2 {
else f:=3; if (x<5) return 2*pow(x,3)+1;
end; else if (x<8) return 2*pow(x,4)+2;
function g(x:real):real; else return 3;
begin }
if(x<=1) then g:= 5*sqr(x)-3*x+2 double g(double x){
else g:=x*x*x-x+5; if (x<=1) return 5*pow(x,2)-3*x+2;
end; else return pow(x,3)-x+5;
begin }
clrscr; void main()
writeln('x'); {
readln(x); clrscr();
writeln('f(g(f(x)))=',f(g(f(x))):4:2); cout<<"dati x=";
writeln('g(f(g(x)))=',g(f(g(x))):4:2); cin>>x;
readkey; cout<<"f(g(f(x)))= "<<f(g(f(x)))<<endl;
end. cout<<"g(f(g(x)))= "<<g(f(g(x)));
Rezolvare: getch();
x }
0 Rezolvare:
f(g(f(x)))=129.00 dati x=0
g(f(g(x)))=4901.00 f(g(f(x)))= 129
g(f(g(x)))= 4901

Problema 18 Descompunerea numrului n sume

Se d numrul natural n. S se afieze toate descompunerile posibile ale numrului n.

program pr18; #include<stdio.h>


uses crt; #include<conio.h>
var a:array[1..100] of integer; #include<math.h>
n,i,s,j:integer; #include<iostream.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 for (int i=1;i<=k;i++)
begin cout<<s[i];
for i:=1 to j-1 do cout<<endl;
write(a[i],' '); }
writeln(#8,' '); void part(int k,int v)
end else for i:=1 to s do begin {
a[j]:=i; int i;
13
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

desc(j+1,s-i,k); s[k]=v;
end; tipar(k);
end; for (i=1;i<=s[k]-1;i++)
begin {
clrscr; s[k]=s[k]-i;
write('scrie numarul='); part(k+1,i);
readln(n); s[k]=s[k]+i;
for i:=1 to n do }
desc(1,n,i); }
readkey; void main()
end. {
clrscr();
cout<<"n=";
Rezolvare: cin>>n;
scrie numarul=4 part(1,n);
4 getch();
13 }
22 Rezolvare:
31 n=4
112 4
121 31
211 22
1111 211
13
121
112
1111

Problema 19: Cmmdc a n- numere


S se elaboreze un algoritm pentru calcularea cmmdc a n numere intregi prin metoda Divide et Impera.

program p19; #include<iostream.h>


uses crt; #include<conio.h>
type vector=array[1..50] of integer; #include<stdio.h>
var x:vector; #include<math.h>
n,i:integer; int x[100];
function cmmdc(a,b:integer):longint; int n,i;
begin int cmmdc(int a,int b)
if a=b then cmmdc:=a {
else if a>b then cmmdc:=cmmdc(a-b,b) if (a==b) return a;
else cmmdc:=cmmdc(a,b-a); else if (a>b) return cmmdc(a-b,a);
end; else return cmmdc(a,b-a);
function divimp(p,q:integer):longint; }
var mij,d1,d2:integer; int divimp(int p,int q)
begin {
if abs(q-p)<=1 then divimp:=cmmdc(x[p],x[q]) int mij;int d1;int d2;
else if (abs(q-p)<=1) return cmmdc(x[p],x[q]);
begin else
mij:=(p+q)div 2; {mij=(p+q)/2;d1=divimp(p,mij);
d1:=divimp(p,mij); d2=divimp(mij+1,q);
d2:=divimp(mij+1,q); return cmmdc(d1,d2);
divimp:=cmmdc(d1,d2); }}
end; void main()
end; {
begin clrscr();
clrscr; cout<<"dati n";cin>>n;
write('dati n='); for(i=1;i<=n;i++)
readln(n); {
for i:=1 to n do begin cout<<"x["<<i<<"]=";
write('x[',i,']='); cin>>x[i]; }
readln(x[i]); cout<<"cmmdc="<<divimp(1,n);

14
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

end; getch();
writeln; }
writeln('cmmdc=',divimp(1,n));
readkey; Rezolvare:
end. dati n5
Rezolvare: x[1]=72
dati n=3 x[2]=84
x[1]=21 x[3]=202
x[2]=66 x[4]=348
x[3]=99 x[5]=526
cmmdc=3 cmmdc=2

Problema 20: Generarea permutrilor (Backtracking recursiv)

S se elaboreze un program care s genereze permutri de n elemente:

program permbackrec; #include<iostream.h>


uses crt; #include<conio.h>
type vector=array[1..25] of integer; #include<stdio.h>
var st,v:vector; int st[50],n;
n:integer; void init()
procedure initial; {
var i:integer; int i;
begin printf("n=");
writeln('dim multi n='); scanf("%d",&n);
readln(n); for(i=1;i<=n;i++)
for i:=1 to 25 do st[i]:=0; st[i]=0;
end; }
function valid(p:integer):boolean; void tipar(int p)
var ok :boolean; {
i:integer; int i;
begin for(i=1;i<=p;i++)
ok:=true; printf("%d ",st[i]);
for i:=1 to p-1 do printf("\n");
if st[p]=st[i] then ok:=false; }
valid:=ok; int valid(int p)
end; {int i,ok;
procedure tipar(p:integer); ok=1;
var i:integer; for(i=1;i<p;i++)
begin if(st[i]==st[p])
for i:=1 to p do ok=0;
write(st[i]:4); return ok;
writeln; }
end; void back(int p)
procedure back(p:integer); {int pv;
var pval:integer; for (pv=1;pv<=n;pv++)
begin {st[p]=pv;
for pval:=1 to n do if (valid(p)==1)
begin {
st[p]:=pval; if(p==n) tipar(p);
if valid(p) then if p=n then tipar(p) else back(p+1);
else back(p+1); }
end; else;
end; }}
begin void main()
initial; {
back(1); clrscr();
readln; init();
end. back(1);
Rezolvare: getch();
15
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

dim multi n= }
3 Rezolvare:
1 2 3 n=3
1 3 2 123
2 1 3 132
2 3 1 213
3 1 2 231
3 2 1 312
321

Problema 21: Generarea aranjamentelor (Backtracking recursiv)

S se elaboreze un program care s genereze aranjamente de n elemente luate cite k: Ank


program backtracking_aranjamente; #include<iostream.h>
uses crt; #include<conio.h>
type vector=array[1..25] of integer; #include<stdio.h>
var st:vector; int st[50],n,k;
n,k:integer; void init()
procedure initial; {
var i:integer; int i;
begin printf("n=");
textcolor (2); scanf("%d",&n);
writeln('Introdu n'); do {
readln(n); printf("k=");
writeln('Introdu k'); scanf("%d",&k);
readln(k); } while (n<k);
textcolor (4); for(i=1;i<=n;i++)
writeln('Aranjamente(',n,',',k,'): '); st[i]=0;
for i:=1 to n do }
st[i]:=0; void tipar(int p)
end; {
function valid(p:integer):boolean; int i;
var i:integer; for(i=1;i<=p;i++)
ok:boolean; printf("%d ",st[i]);
begin printf("\n");
ok:=true; }
for i:=1 to p-1 do int valid(int p)
if st[p]=st[i] then ok:=false; {int i,ok;
valid:=ok; ok=1;
end;
procedure tipar(p:integer); for(i=1;i<p;i++)
var i:integer; if(st[i]==st[p])
begin ok=0;
for i:=1 to p do return ok;
write(st[i]:4,' '); }
writeln; void back(int p)
end; {int pv;
procedure backtr(p:integer); for (pv=1;pv<=n;pv++)
var pval:integer; {
begin st[p]=pv;
for pval:=1 to n do if (valid(p)==1)
begin {
st[p]:=pval; if(k==p) tipar(p);
if valid(p) then else back(p+1);
if p=k then tipar(p) }
else backtr(p+1); end; else;
end; }
Begin }
clrscr; void main()
initial; {
backtr(1); clrscr();
16
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

readkey; init();
end. back(1);
Rezolvare: getch();
Introdu n }
3 Rezolvare:
Introdu k n=3
2 k=2
Aranjamente(3,2): 12
1 2 13
1 3 21
2 1 23
2 3 31
3 1 32
3 2

Problema 22: Generarea produsului cartezian (Backtracking recursiv)

Se dau n mulimi. S se afieze elementele produsului cartezian.

program #include<iostream.h>
backtracking_produsul_cartezian_al_multimilor; #include<conio.h>
uses crt; #include<stdio.h>
type vector=array[1..25] of integer; int st[50],n,nr[50];
var st,nr:vector; void init()
n:integer; {
procedure initial; int i;
var i:integer; printf("n=");
begin scanf("%d",&n);
writeln('Introdu n');
readln(n); for (i=1;i<=n;i++)
for i:=1 to n do {
st[i]:=0; printf("nr[%d]=",i);
for i:=1 to n do scanf("%d",&nr[i]);
begin }
writeln('Introdu numarul de elemente al multimii A',i); for(i=1;i<=n;i++)
readln(nr[i]); st[i]=0;
end; }
writeln('Produsul cartezian al acestor multimi :');
end; void tipar(int p)
function valid(p:integer):boolean; {
begin int i;
valid:=true; for(i=1;i<=p;i++)
end; printf("%d ",st[i]);
procedure tipar(p:integer); printf("\n");
var i:integer; }
begin void back(int p)
for i:=1 to p do {int pv;
write(st[i],' '); for (pv=1;pv<=nr[p];pv++)
writeln; {
end; st[p]=pv;
procedure backtr(p:integer); if(n==p) tipar(p);else back(p+1);
var pval:integer; }

17
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

begin }
if p=n+1 then tipar(p-1) void main()
else for pval:=1 to nr[p] do {
begin clrscr();
st[p]:=pval; init();
if valid(p) then backtr(p+1); back(1);
end; getch();
end; }
Begin Rezolvarea:
clrscr; n=2
initial; nr[1]=2
backtr(1); nr[2]=3
readkey; 11
end. 12
Rezolvare: 13
Introdu n 21
2 22
Introdu numarul de elemente al multimii A1 23
2
Introdu numarul de elemente al multimii A2
3
Produsul cartezian al acestor multimi :
1 1
1 2
1 3
2 1
2 2
2 3

Problema 23: Problema comis voiajorului


Un comis-voiajor trebuie s viziteze un numr de n orae. Iniial, acesta se afl ntr-unul dintre ele, notat cu 1.
Comis voiajorul dorete s nu treac de dou ori prin acelai ora, iar la ntoarcere s revin n oraul 1. Cunoscnd
legturile existente ntre orae, se cere s se tipreasc toate drumurile posibile pe care le poate efectua comis-
voiajorul.
Program comis; # include <stdio.h>
uses crt; # include <conio.h>
type vector=array[1..25] of integer; int st[50], a[50][50], n;
matrice=array[1..25,1..25] of 0..1; void init()
var st:vector; {int i,j;
a:matrice; printf("n="); scanf("%d",&n);
n,start:integer; for(i=1; i<=n; i++) st[i]=0;
procedure initial; for(i=1; i<=n; ++i)
var k,i,j:integer; for(j=1; j<=n; ++j)
begin if (i<j)
writeln('Introdu numarul de orase n'); { printf ("a[%d][%d]=",i,j);
readln(n); scanf ("%d",&a[i][j]);
writeln('Introdu orasul de start'); a[j][i]=a[i][j];}
readln(start); printf ("or.start"); scanf ("%d",& st[1]); }
for k:=1 to n do
st[k]:=0; void tipar (int p)
for i:=1 to n do {
a[i,i]:=0; int i;
for i:=1 to n-1 do for (i=1; i<=p; ++i)
for j:=i+1 to n do printf ("%d ", st[i]); printf ("\n");}
if i<j then begin int valid(int p)
write('a[',i,',',j,']= '); { int i, ok;
readln(a[i,j]); ok=1;
a[j,i]:=a[i,j]; if (a[st[p]][st[p-1]]==0) ok=0;
end; for (i=1; i<p; ++i)
writeln('Traseele pe care le poate urma comis if (st[i]==st[p]) ok=0;
voiajorul :'); return ok; }

18
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

st[1]:=start; void back(int p)


end; { int pv;
procedure tipar(p:integer); for (pv=1; pv<=n; pv++)
var i:integer; { st[p]=pv;
begin if(valid(p)==1)
for i:=1 to p do if (n==p)
write(st[i]:3,' '); {if (a[st[p]][st[1]]==1)
writeln; tipar(p);}
end; else back(p+1); }}
function valid(p:integer):boolean; void main()
var i:integer; {clrscr();
ok:boolean; init();
begin back(2);
ok:=true; getch();
for i:=1 to p-1 do }
if (st[p]=st[i]) or (a[st[p-1],st[p]]=0) then ok:=false; Rezolvare:
valid:=ok; n=6
end; a[1][2]=1
procedure backtr(p:integer); a[1][3]=0
var pval:integer; a[1][4]=0
begin a[1][5]=0
for pval:=1 to n do begin a[1][6]=1
st[p]:=pval; a[2][3]=0
if valid(p) then a[2][4]=1
if (p=n) and (a[st[p],start]=1) then tipar(p) a[2][5]=1
else backtr(p+1); a[2][6]=0
end; end; a[3][4]=1
Begin clrscr; a[3][5]=1
initial; backtr(2); readkey; a[3][6]=1
end. a[4][5]=1
Rezolvare: a[4][6]=0
Introdu numarul de orase n a[5][6]=0
6 or.start1
Introdu orasul de start 124536
1 125436
a[1,2]= 1 163452
a[1,3]= 0 163542
a[1,4]= 0
a[1,5]= 0
a[1,6]= 1
a[2,3]= 0
a[2,4]= 1
a[2,5]= 1
a[2,6]= 0
a[3,4]= 1
a[3,5]= 1
a[3,6]= 1
a[4,5]= 1
a[4,6]= 0
a[5,6]= 0
Traseele pe care le poate urma comis voiajorul :
1 2 4 5 3 6
1 2 5 4 3 6
1 6 3 4 5 2
1 6 3 5 4 2

Problema 24 Generarea combinrilor (Backtracking recursiv)


S se elaboreze un program care s genereze combinri de n elemente luate cite k.
n!
Cnk
k!(n k )!
program backtracking_combinari; #include<iostream.h>
19
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

{Calcularea combinarilor recursiv} #include<conio.h>


uses crt; #include<stdio.h>
type vector=array[1..25] of integer; int st[50],n,k;
var st:vector;
n,k:integer;
procedure initial; void init()
var i:integer; {
begin int i;
writeln('Introdu n'); printf("n=");
readln(n); scanf("%d",&n);
writeln('Introdu k'); do {
readln(k); printf("k=");
writeln('Combinari(',n,',',k,'): '); scanf("%d",&k);
for i:=1 to n do } while (n<k);
st[i]:=0; {initializeaza cu 0 toate elementele stivei} for(i=1;i<=n;i++)
end; st[i]=0;
function valid(p:integer):boolean; {testeaza daca valorile }
de pe stiva
sunt aranjate in ordine crescatoare} void tipar(int p)
var i:integer; {
ok:boolean; int i;
begin for(i=1;i<=p;i++)
ok:=true; printf("%d ",st[i]);
for i:=1 to p-1 do printf("\n");
if st[p]<=st[i] then ok:=false; }
valid:=ok;
end; int valid(int p)
procedure tipar(p:integer); {int i,ok;
var i:integer; ok=1;
begin
for i:=1 to p do {parcurge nivelele} for(i=1;i<p;i++)
write(st[i]:4,' '); if(st[i]>=st[p])
writeln; ok=0;
end; return ok;
procedure backtr(p:integer); }
var pval:integer;
begin void back(int p)
for pval:=1 to n do {int pv;
begin for (pv=1;pv<=n;pv++)
st[p]:=pval; {
if valid(p) then st[p]=pv;
if p=k then tipar(p) {se verifica daca solutia este if (valid(p)==1)
finala,atunci {
solutia se tipareste} if(k==p) tipar(p);
else backtr(p+1); {trece la urmatorul nivel} else back(p+1);
end; }
end; else;
Begin }
clrscr; }
initial;
backtr(1); void main()
readkey; {
end. clrscr();
Rezolvare: init();
Introdu n back(1);
4 getch();
Introdu k }
3 Rezolvare:
Combinari(4,3): n=4
1 2 3 k=3
1 2 4 123

20
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

1 3 4 124
2 3 4 134
234

Problema 25 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 camera, de coordinate x i y, se gsete un om. Se cere s se gseasc toate ieirile din
labirint.
program IES_labirint; #include<iostream.h>
uses crt; #include<stdio.h >
type labirint=array[0..9,0..9] of integer; #include<conio.h>
drum=array[1..2,1..100] of integer; int l[10][10],d[2][100],i,j,m,n,x,y,k;
var l:labirint; void tipar(int k,int d[2][100])
d:drum; {
i,j,m,n,x,y,k:integer; int i; cout<<"--------------"<<endl;
procedure tipar (k:integer; d:drum); for(i=1;i<=k;i++)
var i:integer; cout<<"l="<<d[10][i]<<' '<<"c"<<d[1][i]<<endl;
begin }
writeln ('---------------------'); void ies(int x,int y,int& k,int l[10][10],int d[2][100])
for i:=1 to k do {
writeln ('l=',d[1,i],' ,','c=',d[2,i]); int i,gasit;
end; if(l[x][y]==16) tipar (k,d);
else {
procedure IES (x,y:integer; var k:integer; var l:labirint; k++;
vard:drum); d[0][k]=x;
var gasit : boolean; d[1][k]=y;
i:integer; gasit=0;
begin for (i=1;i<=k-1;i++)
if l[x,y]=16 then tipar(k,d) if(d[0][i]==d[0][k] && d[1][k]) gasit=1;
else begin if(!gasit)
k:=k+1; d[1,k]:=x; for(i=1;i<=4;i++)
d[2,k]:=y; gasit:=false; switch(i)
for i:=1 to k-1 do {
if (d[1,i]=d[1,k]) and (d[i,2]=d[2,k]) case 1:{if(l[x][y] & (int)8) ies(x-1,y,k,l,d);break;}
then gasit:=true; case 2:{if(l[x][y] & (int)4) ies(x,y+1,k,l,d);break;}
if not gasit then case 3:{if(l[x][y] & (int)2) ies(x+1,y,k,l,d);break;}
for i:=1 to 4 do case 4:if(l[x][y] & (int)1) ies(x,y-1,k,l,d);
case i of }
1: if l[x,y] and 8<>0 then IES (x-1,y,k,l,d); k--;}}

21
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

2: if l[x,y] and 4<>0 then IES (x,y+1,k,l,d); void main()


3: if l[x,y] and 2<>0 then IES (x+1,y,k,l,d); {clrscr();
4: if l[x,y] and 1<>0 then IES (x,y-1,k,l,d); cout<<"n=";cin>>n;
end; k:=k-1; cout<<"m=";cin>>m;
end end; for(i=1;i<=m;i++)
BEGIN for(j=1;j<=n;j++)
clrscr; {
write ('n= '); readln (n); cout<<"l["<<i<<','<<j<<"]=";
write ('m= '); readln (m); cin>>l[i][j];
for i:=1 to m do }
for i:=1 to n do cout<<"x=";cin>>x;
begin cout<<"y=";cin>>y;
write ('l[',i,',',j,']='); for(i=1;i<=n;i++)
readln (l[i,j]); end; {
write ('x= '); readln (x); l[0][i]=16;
write ('y= '); readln (y); l[i][n+1]=16;
for i:=1 to n do begin }
l[0,i]:=16; l[m+1,i]:=16; k=0;
end; k:=0; ies(x,y,k,l,d);
IES (x,y,k,l,d); getch();
readln; }
end.

Problema 26: Problema fotografiei


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
determine dac fotografia reprezint unul sau mai multe obiecte.
#include <iostream>
#include <conio.h>
using namespace std;
int a[10][10],i,j,m,n,x,y;
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++)
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;}
22
Bor Constana grupa C2I2 2012-2013 Teoria recursiilor

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;
}
getch();
return 0;
}

23