Jump to content

Recursivitate


skyler_sdf

Recommended Posts

140-sirul lui fibonacci

144-cel mai mare divizor comun

145-suma cifrelor unui nr

146-inversarea unui cuvant

147-numere cu maxim de cifre distincte

140.

#include<iostream.h>

#include<conio.h>

int F[20], n;

long fib(int k)

{

 if(k==1 || k==2)

	return 1;

	else

	 return fib(k-1)+fib(k-2);

	 }

	void main()

	{

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

	 if(n>0)

		cout<<endl<<"termenul nr. "<< n<<"este "<<fib(n);

		}
144.
#include<iostream.h>

int a, b;

int cmmdc(int a, int b)

{

 if(a==b)

	return a;

 else

	if(a>b)

	 return cmmdc(a-b, b);

	else

	 return cmmdc(a, b-a);

	 }

		void main()

		{

		cout<<"dati numerele: "; cin>>a>>b;

		if(a>0 && b>0)

		 cout<<"cmmdc= "<<cmmdc(a, b);

		else

		 cout<<"nu pot calcula cmmdc!!!"; }
145.
#include<iostream.h>

int suma(long x)

{

if(x==0)

 return 0;

 else

	return x%10+suma(x/10);

	}

 void main()

 {  int x;

 cout<<endl<<"dati nr: "; cin>>x;

 cout<<endl<<"suma cifrelor lui"<< x <<" este "<<suma(x);

 }
146.
#include<iostream.h>

#include<stdio.h>

#include<conio.h>

void inv()

{

 char c;

 scanf("%c", &c);

 if(c!=' ')

	inv();

	printf("%c", c);

	}

void main()

{

 inv();

 getch();

 }
147.
#include<iostream.h>

#include<conio.h>

int a[20], max, k;

int test(int c, int a[20], int k)

{

 int i, gasit=0;

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

	if(c==a[i])

	 gasit=1;

	 return gasit;

	 }

 int nr_cif(int x, int a[20], int &k)

 {

	int c;

	if(!x) return 0;

	else

	{

	 c=x%10;

	 if(test(c, a, k))

		return nr_cif(x/10, a, k);

	 else

		 {

		 k++;

			a[k]=c;

			return 1+nr_cif(x/10, a, k);

			}

			}

	 }

 void citire()

 {

	int x, n, i; cin>>x;

	if(x)

	{

	 k=0;

	 for(i=1; i<=20; i++)

		n=nr_cif(x, a, k);

		if(n>max)

		 max=n;

			citire();

	 if(n==max)

		cout<<x<<" ";

	 }

	}

	 void main()

	 {

		cout<<endl;

		max=0;

		 cout<<endl<<"dati numerele separate prin 0(incheiati cu enter)"<<endl;

			citire();

			getch();

			}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.