#include<iostream>
using namespace std;
void afisaren1(int n)
{
cout<<n<<" ";
n--;
if(n!=0)
afisaren1(n--);
}
void afisare1n(int i, int n)
{
if(i<=n){
cout<<i<<" ";
afisare1n(i+1,n);}
}
int factorial(int n)
{
if(n==0)
return 1;
else return n*factorial(n-1);
}
int suma(int n)
{
if(n==0)
return 0;
else return n+suma(n-1);
}
int produs(int a, int b)
{int i=1;
if(b==0)
return 0;
else return a+produs(a,b-1);
}
int putere(int a, int b)
{ int i=0,bb;
bb=b;
if(b==0)
return 1;
if(i<=bb)
{bb--;
return a*putere(a,bb);
}
}
void impartire(int a, int b, int &rest, int &c)
{
if(a-b>0)
{c++;
impartire(a-b,b,rest,c);}
rest=a-(c*b);
if(rest==b)
{rest=0;c++;}
}
void descompunere(int n, int i, int j)
{
cout<<i<<"+"<<j<<endl;
if(i<n-1)
descompunere(n,i+1,j-1);
}
int combinari(int n, int k)
{
if(k==0)
return 1;
else return (n*combinari(n-1,k-1))/k;
}
int combinari2(int n, int k)
{if(k==0 || k==n)
return 1;
else return combinari2(n-1,k)+combinari2(n-1,k-1);
}
void cifre(int n)
{if(n!=0)
cout<<n%10<<endl;
if(n!=0)
cifre(n/10);
}
int sumacifre(int n)
{if(n==0)
return 0;
else return n%10+suma(n/10);
}
int baza(int n, int b)
{
if(n==0)
return 0;
else return (n%b)+baza(n/b,b)*10;
}
int main()
{int n,a,b,rest,c=0,i=1,j,beta=1,k,bb;
cout<<"baza";
cin>>bb;
cout<<"n=";
cin>>n;
j=n-1;
cout<<"k=";
cin>>k;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<"Afisare de la 1 la "<<n<<endl;
afisare1n(i,n);
cout<<endl;
cout<<"Afisare de la "<<n<<" la 1"<<endl;
afisaren1(n);
cout<<endl;
cout<<"Afisare "<<n<<" factorial"<<endl;
cout<<factorial(n)<<endl;
cout<<"Suma primelor "<<n<<" numere naturale"<<endl;
cout<<suma(n)<<endl;
cout<<"Produs("<<a<<","<<b<<")"<<endl;
cout<<produs(a,b)<<endl;
cout<<"Putere "<<a<<" la "<<b<<endl;
cout<<putere(a,b)<<endl;
cout<<"Impartire "<<a<<" la "<<b<<endl;
impartire(a,b,rest,c);
cout<<"cat="<<c<<" "<<"rest="<<rest<<endl;
cout<<"DESCOMPUNERE "<<n<<endl;
descompunere(n,i,j);
cout<<"Combinari("<<n<<","<<k<<")"<<endl;
cout<<combinari(n,k)<<endl;
cout<<"Combinari2("<<n<<","<<k<<")"<<endl;
cout<<combinari2(n,k)<<endl;
cout<<"Afisare cifre"<<endl;
cifre(n);
cout<<"Suma cifrelor"<<endl;
cout<<sumacifre(n)<<endl;
cout<<"Baza 2"<<endl<<n<<"(10)="<<baza(n,bb)<<"(2)"<<endl;
}
Orar semigroup #2
Fişă Funcţii Recursive - 21 martie 2011 (1-10, 14)
joi, 24 martie 2011
by DlMuresan
Categories:
functii,
info,
recursivitate,
subprograme
|
Leave a comment