UVa 10341 (二分求根) Solve It

很水的一道题,因为你发现这个函数是单调递减的,所以二分法求出函数的根即可。

 1 #include <cstdio>
 2 #include <cmath>
 3 //using namespace std;
 4 
 5 const double e = 1e-14;
 6 double p, q, r, s, t, u;
 7 
 8 inline double f(double x)
 9 { return p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x*x + u; }
10 
11 int main()
12 {
13     //freopen("in.txt", "r", stdin);
14 
15     while(scanf("%lf%lf%lf%lf%lf%lf", &p, &q, &r, &s, &t, &u) == 6)
16     {
17         if(f(0)<-e || f(1)>e) { puts("No solution"); continue; }
18         double L = 0, R = 1, m;
19         for(int i = 0; i < 30; i++)
20         {
21             m = (L+R)/2;
22             if(f(m) < 0) R = m;
23             else L = m;
24         }
25         printf("%.4f\n", m);
26     }
27 
28     return 0;
29 }
代码君

 

posted @ 2015-03-14 22:00  AOQNRMGYXLMV  阅读(185)  评论(0)    收藏  举报