水题,不过要注意二分时的精度要尽量取高一点(1e-7就不能过)。
代码如下:
<span style="font-size:18px;">#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
double p,q,r,s,t,u;
double f(double x)
{
return p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u;
}
int main()
{
while(scanf("%lf%lf%lf%lf%lf%lf",&p,&q,&r,&s,&t,&u)!=EOF)
{
double first=0,last=1.0;
if(f(0)<0||f(1)>0)
{
printf("No solution\n");
continue;
}
while(last-first>1e-8)
{
double mid=(first+last)/2;
if(f(mid)<0)
last=mid-1e-9;
else
first=mid+1e-9;
}
double ans=(last+first)/2;
printf("%.4lf\n",ans);
}
return 0;
}
</span>