uva10341-解方程

题目链接 http://acm.hust.edu.cn/vjudge/problem/19047

 

解题思路

方程左边为单调函数。二分。

 

代码

#include<stdio.h>
#include<math.h>
#define MIN 1e-10
//#define LOCAL
double p, q, r, s, t, u;
double fun(double x)
{
    return p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u;
}
int main()
{
    #ifdef LOCAL
        freopen("data.txt", "r", stdin);
        freopen("ans.txt", "w", stdout);
    #endif
    double x=0.0, y=1.0;
    double ans, mid;
    while(scanf("%lf%lf%lf%lf%lf%lf", &p, &q, &r, &s, &t, &u)==6) {
        if(fun(0.0)*fun(1.0)>0) { printf("No solution\n"); continue; }
        else while(fabs(y-x)>MIN) {
            mid = x+(y-x)/2.0;
            ans = fun(mid);
            if(ans<0) y = mid;
            else x = mid;
        }
        printf("%.4lf\n", x);
        x = 0.0; y=1.0;
    }
    return 0;
}

 

posted @ 2016-08-17 09:31  啊嘞  阅读(141)  评论(0编辑  收藏  举报