题意:
解方程:p ∗ e^(−x) + q ∗ sin(x) + r ∗ cos(x) + s ∗ tan(x) + t ∗ x^2 + u = 0 (0 <= x <= 1);
其中0 ≤ p, r ≤ 20 , −20 ≤ q, s, t ≤ 0。(一开始没看见q,s,t<=0, 卡了半天...)
根据上面的条件,设F(x) = p ∗ e^(−x) + q ∗ sin(x) + r ∗ cos(x) + s ∗ tan(x) + t ∗ x^2 + u ;即求0 <= x <= 1时与x轴是否有交点。
可以看出F(x)在该区间内为减函数,判断有无解则只需判断F(0)>=0&&F(1)<=0即可。若有解,则在[0,1]范围内二分求解。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
const double eps = 1e-;
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) == )
{
double f0 = F(), f1 = F();
if(f1 > eps || f0 < -eps) printf("No solution\n");
else
{
double L = , R = , M;
while(L < R)
{
M = L+(R-L)/;
if(fabs(F(M)) < eps) break;
if(F(M) < ) R = M;
else L = M;
} printf("%.4lf\n", M);
}
}
return ;
}