0% found this document useful (0 votes)
72 views5 pages

Probleme Info

The document contains 5 problems and their solutions using algorithms like backtracking, recursion, and divide and conquer. The problems include generating all combinations of n binary digits without consecutive 1s, generating numbers with n digits containing exactly k 1s, generating all numbers with 5 distinct odd digits, calculating the cube root of a number using divide and conquer, and calculating the sum of elements in an array using divide and conquer. The solutions use techniques like backtracking, recursion, and dividing the problems into subproblems.

Uploaded by

garssás dadad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views5 pages

Probleme Info

The document contains 5 problems and their solutions using algorithms like backtracking, recursion, and divide and conquer. The problems include generating all combinations of n binary digits without consecutive 1s, generating numbers with n digits containing exactly k 1s, generating all numbers with 5 distinct odd digits, calculating the cube root of a number using divide and conquer, and calculating the sum of elements in an array using divide and conquer. The solutions use techniques like backtracking, recursion, and dividing the problems into subproblems.

Uploaded by

garssás dadad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. Se citeste un numar natural n.

Generati si afisati toate combinatiile de cate n cifre binare care nu


au doua cifre de 1 alaturate.

REZOLVARE:

#include <fstream>
using namespace std;
ifstream is("date.in");
ofstream os("date.out");

int x[15],n;

void afis()
{
for(int i=1;i<=n;i++)
os<<x[i]<<" ";
os<<endl;
}

void back01(int k)
{
for(int i=0;i<=1;i++)
{
x[k]=i;
if(k>1) if(x[k]*x[k-1]!=1)
if(k==n) afis();
else back01(k+1);
else ;
else back01(k+1);
}
}

int main()
{
is>>n;
back01(1);
is.close();
os.close();
return 0;
}
2. Se citesc doua numere naturale n si k. Generati si afisati toate toate numerele naturale formate
din n cifre care contin exact k cifre de 1.

REZOLVARE:

#include <iostream>
using namespace std;
int x[50],n,k;

void afis()
{
for(int i=1;i<=n;i++)
cout<<x[i];
cout<<endl;
}

int valid(int pas)


{
int c=0;
for(int i=1;i<=pas;i++)
if(x[i]==1) c++;
if(c>k) return 0;
if(pas==n && c!=k) return 0;
if(x[1]==0) return 0;
return 1;
}

void back(int pas)


{
for(int i=0;i<=9;i++)
{
x[pas]=i;
if(valid(pas)) if(pas==n) afis();
else back(pas+1);
}
}
int main()
{
cin>>n>>k;
back(1);
return 0;
}
3. Sa se genereze toate numerele formate din 5 cifre impare distincte.

REZOLVARE:

#include<iostream>

using namespace std;

int x[100],pus[100],n,nr=0;

void Write()
{ for(int i=1;i<=n;i++) cout<<x[i];
cout<<endl;
nr++;
}
void Perm(int k)
{ for(int i=1;i<=9;i=i+2)
if(!pus[i])
{ x[k]=i;
pus[i]=1;
if(k==n) Write();
else Perm(k+1);
pus[i]=0;
}
}

int main()
{ n=5;
Perm(1);
cout<<nr;
system("pause");
return 0;
}
4. Se citeste un numar real x. Sa se calculeze radical de ordinul 3 din x folosind un algoritm de tip
Divide et impera.

REZOLVARE:

#include<iostream>
using namespace std;
double r3(double x, double s, double d)
{
if(d-s<=0.0001) return d;
else
{ double m=(s+d)/2;
if(m*m*m<x) return r3(x,m,d);
else return r3(x,s,m);
}
}

int main()
{ double x;
cin>>x;
if(x>0) if(x<1) cout<<r3(x,0,1);
else cout<<r3(x,0,x);
else if(x>-1) cout<<r3(x,-1,0);
else cout<<r3(x,x,0);
system("pause");
return 0;
}
5. Se citeste un vector cu n elemente numere naturale. Sa se calculeze suma elementelor vectorului
folosind divide et impera.

REZOLVARE:

#include<iostream>

using namespace std;

int suma(int a[100], int s, int d)


{ if(s==d) return a[s];
else return suma(a,s,(s+d)/2)+suma(a,(s+d)/2+1,d);
}

int main()
{
int a[100],n,i;
cin>>n;
for(i=1;i<=n;i++) cin>>a[i];
cout<<suma(a,1,n);
system("pause");
return 0;
}

You might also like