UVa 10341 - Solve It

  题目:给一个方程,求解方程的解。已给出解的范围,并且可知方程等号左侧的函数是递减的,可用二分法进行试探,直到得出给定误差范围内的解。

 1 #include <cstdio>
 2 #include <cmath>
 3 #define EPSILON 1e-9
 4 
 5 int p, q, r, s, t, u;
 6 
 7 double f(double x)
 8 {
 9     return p*exp(-1.0*x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x*x + u;
10 }
11 
12 int main()
13 {
14 #ifdef LOCAL
15     freopen("in", "r", stdin);
16 #endif
17     while (scanf("%d%d%d%d%d%d", &p, &q, &r, &s, &t, &u) != EOF)
18     {
19         if (f(0) < 0 || f(1) > 0)
20         {
21             printf("No solution\n");
22             continue;
23         }
24         double x = 0, y = 1;
25         while (y - x > EPSILON)
26         {
27             double m = (x+y)/2;
28             if (f(m) < 0)   y = m;
29             else x = m;
30         }
31         printf("%.4lf\n", x);
32     }
33     return 0;
34 }
View Code

  开始的时候WA了一次,后来把EPSILON的值从1e-6调成1e-9就好了,又是这个问题...

posted @ 2013-07-24 19:00  xiaobaibuhei  阅读(154)  评论(0编辑  收藏  举报