Sunteți pe pagina 1din 81

CMMDC RECURSIV

Prob4 nerecursiv

#include <stdio.h>

#include <iostream.h>

#include <math.h>

int n,k,sum;

int main()

cout <<"n=";

cin >>n;

if (n>=1)

for (k=1;k<=n; k++)

sum=sum+(4*k-3);

cout << 4*k-3<< "\t*** suma="

<<sum<< endl;

return 0;

}
Prob4 recursiva

#include <stdio.h>

#include <iostream.h>

#include <math.h>

int n,k,s;

int suma (int n)

if (n==0) return 0;

else return suma(n-1)+(4*n-3);

int main()

cout <<"n=";

cin >>n;

for (k=1;k<=n; k++)

cout << 4*k-3<< "\t*** suma="

<<suma (k)<< endl;

return 0;

}
Prob5 recursiva

#include <stdio.h>

#include <iostream.h>

#include <math.h>

int n,k,s;
int suma (int n)

if (n==0) return 0;

else return suma(n-1)+(5*n-4);

int main()

cout <<"n=";

cin >>n;

for (k=1;k<=n; k++)

cout << 5*k-4<< "\t*** suma="

<<suma (k)<< endl;

return 0;

}
Prob5 nerecursiv

#include <stdio.h>

#include <iostream.h>

#include <math.h>

int n,k,sum;

int main()

cout <<"n=";

cin >>n;

if (n>=1)

for (k=1;k<=n; k++)

{
sum=sum+(5*k-4);

cout << 5*k-4<< "\t*** suma="

<<sum<< endl;

return 0;

}
Prob6 recursiv

#include <stdio.h>

#include <iostream.h>

#include <math.h>

#include <conio.h>

int a,b;

int CMMDC (int a,int b)

if (a==b)return a;

if (a>b)return CMMDC (a-b,b);

if (b>a)return CMMDC (a,b-a);

int main()

{
cout <<"a=";

cin>>a;

cout<<"b=";

cin>>b;

cout<<"CMMDC("<<a<<","<<b<<")="

<<CMMDC (a,b);

getch ();

return 0;

}
Prob6 nerecursiv

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

int a,b,a1,b1;

int main()

cout <<"Dati a1=";

cin>>a1;

a=a1;

cout<<"Dati b1=";

cin>>b1;

b=b1;

while (a!=b)

{
if (a>b) a=a-b;

if (b>a) b=b-a;

if (a==b)

cout<<"CMMDC("<<a1<<","<<b1<<")="<<a;

getch ();

return 0;

Prob7

#include <stdio.h>

#include <conio.h>
#include <iostream.h>

int a,b,c;

int CMMDC (int a,int b)

if (a==b)return a;

if (a>b) return CMMDC(a-b,b);

if (b>a) return CMMDC(a,b-a);

int main()

cout <<"Dati a=";

cin>>a;

cout<<"Dati b=";

cin>>b;

cout<<"Dati c=";

cin>>c;

if ((a>0)&&(b>0)&&(c>0))

cout <<"CMMDC("<<a<<","<<b<<","<<c<<")="<<CMMDC(CMMDC (a,b),c);

getch ();

return 0;

}
Nerecursiv

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

int a,b,c,a1,b1,c1;

int main()

cout <<"Dati a1=";

cin>>a1;

a=a1;

cout<<"Dati b1=";

cin>>b1;

b=b1;

cout<<"Dati c1=";

cin>>c1;

c=c1;
while (a!=b)

if (a>b) a=a-b;

if (b>a) b=b-a;

while (a!=c)

if (a>c) a=a-c;

if (c>a) c=c-a;

while (c!=b)

if (b>c) b=b-c;

if (c>b) c=c-b;

if ((a>0)&&(b>0)&&(c>0));

if ((a==b)&& (a==c))

cout<<"CMMDC("<<a1<<","<<b1<<","<<c1<<")="<<a;

getch ();

return 0;

}
Prob8 recursivitatea indirecta

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

int x;

int g(int x);

int f(int x)

if (x>1)return g(x)-2;

if (x<=1)return x+2;

int g(int x)

{
if (x<0)return -x;

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

int main()

cout <<"dati x=";

cin >>x;

cout <<"f(x)="<<f(x)<<endl;

cout <<"g(x)="<<g(x)<<endl;

getch ();

return 0;

}
Modernizat

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

int x;

int g(int x);

int f(int x)

if (x>1)return g(x)-2;

if (x<=1)return x+2;

int g(int x)

if (x<0)return -x;

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

int main()

cout <<"dati x=";


cin >>x;

cout <<"f("<<x<<")="<<f(x)<<endl;

cout <<"g("<<x<<")="<<g(x)<<endl;

getch ();

return 0;

}
Prob 10

#include <stdio.h>

#include <conio.h>

#include <iostream.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);

if (x<5)return b(x+1);

if (x>=5)return a(x)+1;

}
int b(int x)

if (x<=0)return pow (x+1,2);

if (x<5)return c(-1)-1;

if (x>=5)return a(x)+5;

int a(int x)

if (x<=3)return pow (x,2);

if (x<6)return c(x-1);

if (x>=6)return b(x-2);

int main()

cout <<"dati x=";

cin >>x;

cout <<"a("<<x<<")="<<a(x)<<endl;

cout <<"b("<<x<<")="<<b(x)<<endl;

cout <<"c("<<x<<")="<<c(x)<<endl;

getch ();

return 0;

}
Prob11

#include <stdio.h>

#include <conio.h>

#include <iostream.h>
#include <math.h>

int n;

double a0,b0;

double a (int n);

double b (int n)

if (n==0) return b0;

else return sqrt(a(n-1)*b(n-1));

double a (int n)

if (n==0)return a0;

else return (a(n-1)+b(n-1))/2;

int main ()

cout <<"dati n=";

cin >>n;

cout <<"dati a0=";

cin>>a0;

cout <<"dati b0=";

cin>>b0;

if (n>=0)cout <<"a("<<n<<")="<<a(n)<<endl;

if (n>=0)cout <<"b("<<n<<")="<<b(n)<<endl;

getch ();
return 0;

Prob12

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

#include <iostream.h>

#include <math.h>

int n;

double a0,b0,c0;

double a (int n);

double b (int n);

double c (int n)

if (n==0) return c0;

else return sqrt(a(n-1))+sqrt(b(n-1))+sqrt(c(n-1));

double a (int n)

if (n==0)return a0;

else return (a(n-1)+b(n-1)+c(n-1))/3;

double b (int n)

if (n==0)return b0;

else return sqrt(a(n-1)*b(n-1)*c(n-1));

int main ()

cout <<"dati n=";


cin >>n;

cout <<"dati a0=";

cin>>a0;

cout <<"dati b0=";

cin>>b0;

cout <<"dati c0=";

cin>>c0;

if (n>=0)cout <<"a("<<n<<")="<<a(n)<<endl;

if (n>=0)cout <<"b("<<n<<")="<<b(n)<<endl;

if (n>=0)cout <<"c("<<n<<")="<<c(n)<<endl;

getch ();

return 0;

}
Prob13 recursiv

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

#include <math.h>

int m,n;

int ack(int m, int n)

if(m==0) return n+1;

if (n==0) return ack (m-1,1);

if ((m>0) && (n>0)) return ack (m-1, ack(m,n-1));

int main ()

cout<<"Dati m=";

cin>>m;
cout<<"Dati n=";

cin>>n;

cout<<"Ack("<<m<<","<<n<<")="<<ack(m,n);

getch(); return 0;

}
Prob 14 recursiv

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

#include <math.h>

int x;
int F(int x)

if (x<5) return F(x+1);

if ((x>=5)&&(x<=12)) return F(F(x+2));

else return x-1;

int main ()

cout <<"Dati x="<<endl;

cin >>x;

cout<<"F("<<x<<")="<<F(x);

getch ();

return 0;

}
Prob15 HERMITE

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

#include <math.h>

double x;

int n;

double H1(double x, int n)

if (n==0)return 1; else

if (n==1)return x;

if(n>1)

return H1(x,n-1)-(n-1)*H1(x,n-2);

int main()

{
cout <<"Dati x="<<endl;

cin >>x;

cout <<"Dati n="<<endl;

cin >>n;

cout<<"H("<<x<<","<<n<<")="<<H1(x,n);

getch ();

return 0;

}
Prob16

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

#include <math.h>

double x;

int n;

double T1(double x, int n)

if (n==0)return 1; else

if (n==1)return 0;

if(n>1)

return 2*x*T1(x,n-1)-T1(x,n-2);

int main()

{
cout <<"Dati x="<<endl;

cin >>x;

cout <<"Dati n="<<endl;

cin >>n;

cout<<"T("<<x<<","<<n<<")="<<T1(x,n);

getch ();

return 0;

}
Prob16 ROLLY1

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

#include <math.h>

double x;

int n;

double R1(double x, int n)

if (n==0)return 1;

if (n==1)return 0;

if (n==2)return 2;else

if(n>=3)

return 3*x*R1(x,n-1)-R1(x,n-2)-0.01*R1(x,n-3);

int main()
{

cout <<"Dati x="<<endl;

cin >>x;

cout <<"Dati n="<<endl;

cin >>n;

cout<<"R("<<x<<","<<n<<")="<<R1(x,n);

getch ();

return 0;

}
Prob18 REULY1

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

#include <math.h>

double x;

int n;

double Z1(double x, int n)

if (n==0)return 1;

if (n==1)return 0;

if ((n>=2)&&(n<=10))return 2*x*Z1(x,n-1)- (n-1)*Z1(x,n-2);else

if(n>10)

return 3*x*Z1(x,n-1)-(n-1)*Z1(x,n-2)-(n-2)*Z1(x,n-3);

int main()
{

cout <<"Dati x="<<endl;

cin >>x;

cout <<"Dati n="<<endl;

cin >>n;

cout<<"Z("<<x<<","<<n<<")="<<Z1(x,n);

getch ();

return 0;

}
Prob19 Manna-Pneuli2

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

#include <math.h>

int x;

int F(int x)

if (x<5) return F(x+1);

if ((x>=5)&&(x<=12)) return F(F(x+2));

else return x-1;

int main ()

cout <<"Dati x="<<endl;

cin >>x;
cout<<"F("<<x<<")="<<F(x);

getch ();

return 0;

Prob20 turnul din Hanoi


#include <stdio.h>

#include <conio.h>

#include <iostream.h>

#include <math.h>

void H (int n, char X,char Y,char Z)

if (n==1)

{printf ("%c%c\n",X,Y);

else

H (n-1,X,Z,Y);

printf("%c%c\n",X,Y);

H (n-1,Z,Y,X);

int main ()

int n;

printf ("dati n=");

scanf("%d",&n);

H(n,'X','Y','Z');

getch ();

}
Program in C++

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

#include <iostream.h>

#include <math.h>

int H (int n, char X,char Y,char Z)

if (n==1)

cout <<X<<Y<<endl;

else

H (n-1,X,Z,Y);

cout <<X<<Y<<endl;

H (n-1,Z,Y,X);

int main ()

int n;

cout <<"dati n=";

cin >>n;

H(n,'X','Y','Z');

getch ();

return 0;

}
Prob 21

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

#include <math.h>

double g(double x);

double f(double x)

if (x<5)return 2*pow(x,2)+1;

if ((x>=5)&&(x<8))return pow(x,4)+2;

if (x>=8)return 3;

double g(double x)

if (x<=1)return 5*pow(x,2)-3*x+2;

else return pow(x,2)-x+5;}

int main ()

{double x;

cout <<"dati x=";

cin >>x;

cout <<"f(g(f("<<x<<")))="<<f(g(f(x)))<<endl;

cout <<"g(f(g("<<x<<")))="<<g(f(g(x)))<<endl;

cout <<"f(g(f(g(f("<<x<<")))))="<<f(g(f(g(f(x)))))<<endl;

cout <<"f(g(f(f("<<x<<"))))="<<f(g(f(f(x))))<<endl;
cout <<"f(g(g(f(f("<<x<<")))))="<<f(g(g(f(f(x)))))<<endl;

getch ();

return 0;

}
Problema 25

//curba Sierpinski scurta

#include <graphics.h>

#include <conio.h>
#include <stdlib.h>

#include <stdio.h>

#include <dos.h>

const n=6, Cadru=2048, SizeWin=460;

int Gd, Gm, ErrCode;

int i,h,x,y;

double k;

void A(int i);

void B(int i);

void C(int i);

void D(int i);

void A(int i){

if(i>0) {

delay(10);

A(i-1);x=x+h;y=y-h;lineto((double)x*k,(double)y*k);

B(i-1);x=x+h;x=x+h;lineto((double)x*k,(double)y*k);

D(i-1);x=x+h;y=y+h;lineto((double)x*k,(double)y*k);

A(i-1);

};

void B(int i){

if(i>0) {

B(i-1);x=x-h;y=y-h;lineto((double)x*k,(double)y*k);

C(i-1);y=y-h;y=y-h;lineto((double)x*k,(double)y*k);

A(i-1);x=x+h;y=y-h;lineto((double)x*k,(double)y*k);
B(i-1);

void C(int i){

if(i>0) {

C(i-1);x=x-h;y=y+h;lineto((double)x*k,(double)y*k);

D(i-1);x=x-h;x=x-h;lineto((double)x*k,(double)y*k);

B(i-1);x=x-h;y=y-h;lineto((double)x*k,(double)y*k);

C(i-1);

void D(int i){

if(i>0) {

D(i-1);x=x+h;y=y+h;lineto((double)x*k,(double)y*k);

A(i-1);y=y+h;y=y+h;lineto((double)x*k,(double)y*k);

C(i-1);x=x-h;y=y+h;lineto((double)x*k,(double)y*k);

D(i-1);

void main(void)

clrscr();

int gdriver = DETECT, gmode, errorcode;

initgraph(&gdriver, &gmode, "c:\\tcpp\\bgi");

errorcode = graphresult();
if (errorcode != grOk){

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

}else{

setbkcolor(15);

i=0; h=div(Cadru, 4).quot; x=3*h; y=3*h; k=(double)SizeWin/Cadru;

do{

i=i+1; x=x-h; h=div(h, 2).quot; y=y+h;

setcolor(i); setlinestyle(SOLID_LINE, 1, 1);

moveto((double)x*k,(double)y*k);

A(i);x=x+h;y=y-h;lineto((double)x*k,(double)y*k);

B(i);x=x-h;y=y-h;lineto((double)x*k,(double)y*k);

C(i);x=x-h;y=y+h;lineto((double)x*k,(double)y*k);

D(i);x=x+h;y=y+h;lineto((double)x*k,(double)y*k);

}while (!(i==n));

getch();

closegraph();

}
N=1 n=2

n=3

N=5
N=8

Prob26
//covorul Sierpinski

#include <graphics.h>

#include <conio.h>

#include <stdlib.h>

#include <stdio.h>

#include <dos.h>

int n=100;

void SerpCov(int x, int y, int r)

delay(1);

if(r>0){// 4 colturi

SerpCov(x-r, y+r, r/2);

SerpCov(x-r, y-r, r/2);

SerpCov(x+r, y-r, r/2);

SerpCov(x+r, y+r, r/2);

//fillellipse(x, y, r/2, r/2);

bar(x-r/2, y-r/2, x+r/2, y+r/2);//peste patrat

}
}

void main(void)

clrscr();

int gdriver = DETECT, gmode, errorcode;

initgraph(&gdriver, &gmode, "c:\\TurboC3\\bgi");

errorcode = graphresult();

if (errorcode != grOk){

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

}else{

setbkcolor(15);

setfillstyle(SOLID_FILL,1);

SerpCov(getmaxx()/2, getmaxy()/2, n);

getch();

closegraph();

}
Prob28

#include <graphics.h>
#include <iostream.h>

#include <stdlib.h>

#include <conio.h>

#include <stdio.h>

#include <math.h>

int gdriver, gmode, errorcode, ls,L;

void rotplan (int xc, int yc,

int x1, int y1, int &x,

int &y, float unghi)

{ x=ceil (xc+(x1-xc)*cos(unghi)-(y1-yc)*sin(unghi));

y= ceil (yc+(x1-xc)*sin(unghi)+(y1-yc)*cos(unghi));

void desenez (int x1, int y1, int x2, int y2, int n, int ls)

{int x,y;

if (n<=ls)

setcolor (l+random(14));

setcolor (1);

moveto (x1, y1);

lineto (x2, y2);

rotplan (x2,y2, div(3*x2-x1,2).quot, div (3*y2-y1,2).quot,x,y,M_PI/4);

desenez (x2, y2, x, y, n+1,ls);

rotplan (x2, y2, div(3*x2-x1,2).quot, div(3*y2-y1,2).quot, x,y,-M_PI/4);

desenez (x2,y2,x,y,n+1,ls);

}
}

void main()

{randomize(); clrscr();

cout<<"ls="; cin>>ls;

gdriver= DETECT, gmode;

initgraph(&gdriver, &gmode, "c:\\tcpp\\bgi");

errorcode = graphresult();

if (errorcode!=grOk){

printf("Graphics error: %s\n",grapherrormsg (errorcode));

printf ("Press any key to halt:");

getch();

}else{

setbkcolor (15);

setlinestyle (SOLID_LINE,1, 1);

desenez (div(getmaxx(),2).quot, getmaxy(), div(getmaxx(),2).quot,

getmaxy()-250,1, ls);

getch();

closegraph();

N=4
N=8

N=10
Prob27

//Curba lui Koch pentru un triunghi echilateral

#include <graphics.h>

#include <iostream.h>

#include <stdlib.h>

#include <conio.h>

#include <stdio.h>

#include <math.h>

int gdriver,gmode, errorcode,ls,L;

void rotplan(int xc,int yc,

int x1, int y1,int &x,

int &y,float unghi)

{ x = ceil(xc+(x1-xc)*cos(unghi)-

(y1-yc)*sin(unghi));

y = ceil(yc+(x1-xc)*sin(unghi)+
(y1-yc)*cos(unghi));

void desenez(int x1,int y1,

int x2,int y2,int x3,int y3)

{ moveto(x1,y1);

lineto(div((2*x1+x2),3).quot,

div((2*y1+y2),3).quot);

lineto(x3,y3);

lineto(div((x1+2*x2),3).quot,

div((y1+2*y2),3).quot);

lineto(x2,y2);

void generator(int x1,int y1,

int x2,int y2,int n, int ls)

{ int x,y;

rotplan(div((2*x1+x2),3).quot,

div((2*y1+y2),3).quot,

div((x1+2*x2),3).quot,

div((y1+2*y2),3).quot,

x,y,M_PI/3);

if (n<ls)

{generator(x1,y1,div((2*x1+x2),

3).quot,div((2*y1+y2),

3).quot,n+1,ls);

generator(div((2*x1+x2),
3).quot,div((2*y1+y2),

3).quot,x,y,n+1,ls);

generator(x,y,div((x1+2*x2),

3).quot,div((y1+2*y2),

3).quot,n+1,ls);

generator(div((x1+2*x2),

3).quot,div((y1+2*y2),

3).quot,x2,y2,n+1,ls);

else desenez(x1,y1,x2,y2,x,y);

void main()

{ cout<<"ls= "; cin>>ls;

clrscr();

gdriver = DETECT, gmode;

initgraph(&gdriver, &gmode, "c:\\TurboC3\\bgi");

errorcode = graphresult();

if (errorcode != grOk){

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

}else{

setbkcolor(15);

setfillstyle(SOLID_FILL,1);

setcolor(RED);
setlinestyle(SOLID_LINE, 0, 3);

L = getmaxx()-320;

generator(160,getmaxy()-150,

160+L,getmaxy()-150,1,ls);

generator(160+L,getmaxy()-

150,160+div(L,2).quot,

getmaxy()-150-

ceil(L*(sqrt(3)/2)),1,ls);

generator(160+div(L,2).quot,

getmaxy()-150-

ceil(L*(sqrt(3)/2)),160,

getmaxy()-150,1,ls);

setfillstyle(1,GREEN);

floodfill(div(getmaxx(),2)

.quot,div(getmaxx(),

2).quot,RED);

getch();

closegraph();

}
N=4

N=6
N=8

N=10

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