Posted in 面试题 onJuly 04, 2015
//f(x)=RMAX-R+(R-r)*double (cos(0.5*x))+double (sqrt(r*r-(R-r)*(R-r)* sin(0.500*x)* sin(0.500*x)))
请用二分法或其他方法求出解x.要求精度esp=1e-5
其中RMAX,R,r从键盘输入。
#include
#include
void get(double RMAX,double R,double r,double x,double y)
{
double m,n,c,p;
double esp=1e-5;
do
{
m=RMAX-R+(R-r)*double (cos(0.5*x))+double (sqrt(r*r-(R-r)*(R-r)* sin(0.500*x)* sin(0.500*x)));
n=RMAX-R+(R-r)*double (cos(0.500*y))+double (sqrt(r*r-(R-r)*(R-r)* sin(0.500*y)*sin(0.500*y)));
c=0.500*(x+y);
p=RMAX-R+(R-r)*double (cos(0.500*c))+double (sqrt(r*r-(R-r)*(R-r)* sin(0.500*c)*sin(0.500*c)));
if(fabs(p)
break;
else if(m*p {
x=x;
y=c;
}
else if (m*p>0)
{
y=y;
x=c;
}
}while(fabs(y-x)
cout cout
}
void main()
{
double RMAX,R,r,x,y;
cout
cin>>RMAX;
cin>>R;
cin>>r;
cin>>x;
cin>>y;
get(RMAX,R,r,x,y);
}
请用二分法或其他方法求出解x.要求精度esp=1e-5
其中RMAX,R,r从键盘输入。
#include
#include
void get(double RMAX,double R,double r,double x,double y)
{
double m,n,c,p;
double esp=1e-5;
do
{
m=RMAX-R+(R-r)*double (cos(0.5*x))+double (sqrt(r*r-(R-r)*(R-r)* sin(0.500*x)* sin(0.500*x)));
n=RMAX-R+(R-r)*double (cos(0.500*y))+double (sqrt(r*r-(R-r)*(R-r)* sin(0.500*y)*sin(0.500*y)));
c=0.500*(x+y);
p=RMAX-R+(R-r)*double (cos(0.500*c))+double (sqrt(r*r-(R-r)*(R-r)* sin(0.500*c)*sin(0.500*c)));
if(fabs(p)
break;
else if(m*p {
x=x;
y=c;
}
else if (m*p>0)
{
y=y;
x=c;
}
}while(fabs(y-x)
cout cout
}
void main()
{
double RMAX,R,r,x,y;
cout
cin>>RMAX;
cin>>R;
cin>>r;
cin>>x;
cin>>y;
get(RMAX,R,r,x,y);
}
某IT外企面试题-二分法求方程!看看大家的C++功底
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@