题意:
解方程: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]范围内二分求解。
代码如下:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cmath> 5 using namespace std; 6 const double eps = 1e-14; 7 double p, q, r, s, t, u; 8 double F(double x) 9 { 10 return p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u; 11 } 12 int main() 13 { 14 15 while(scanf("%lf%lf%lf%lf%lf%lf", &p, &q, &r, &s, &t, &u) == 6) 16 { 17 double f0 = F(0), f1 = F(1); 18 if(f1 > eps || f0 < -eps) printf("No solution\n"); 19 else 20 { 21 double L = 0, R = 1, M; 22 while(L < R) 23 { 24 M = L+(R-L)/2; 25 if(fabs(F(M)) < eps) break; 26 if(F(M) < 0) R = M; 27 else L = M; 28 } 29 30 printf("%.4lf\n", M); 31 } 32 } 33 return 0; 34 }
原文:http://www.cnblogs.com/LLGemini/p/4366551.html