1.Se citesc doua numere, n si x, n natural si x real pozitiv . Fara a folosi functia pow, extrageti cu 3 zecimale exacte radicalul de ordinul n din x.
#include<iostream>
#include<cmath>
using namespace std;
float f(float y, int n, float x)
{
float p=1;
for(int i=1;i<=n;i++) p=p*y;
return p-x;
}
float DEI(float s, float d, int n, float x)
{
if(d-s<=0.0001) return s;
else
{ float
m=(s+d)/2;
if(f(m,n,x)==0) return m;
else
if(f(m,n,x)<0) return DEI(m,d,n,x);
else
return DEI(s,m,n,x);
}
}
int main()
{
int n;
float x;
cin>>n>>x;
cout<<DEI(0,sqrt(x),n,x);
return 0;
}
2.Sa se rezolve ecuatia x^3+x-1=0 pe intervalul [0,1] folosind metoda divide et impera.
#include<iostream>
using namespace std;
float f(float x)
{ return x*x*x+x-1;
}
float DEI(float s, float d)
{
if(d-s<=0.0001) return s;
else
{ float
m=(s+d)/2;
if(f(m)==0)
return m;
else
if(f(m)<0)
return DEI(m,d);
else
return DEI(s,m);
}
}
int main()
{ cout<<DEI(0,1);
return 0;
}
Niciun comentariu:
Trimiteți un comentariu