UVA10341:Solve It(二分+math.h库)

题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/E

题目要求:p*e-xq*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0,求出x的值;

where 0 <= x <= 1.

题目解析:

首先要学会观察题目,因为p,r>=0,q,s,t<=0,对上面方程求导发现导数<=0,所以原方程单调递减,(满足使用二分的条件)然后假如方程有答案,则可以利用二分来查找满足条件的解,注意二分的条件边界(!!!),其次上面值都可以通过调用math.h库来实现,具体实现请看代码。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#define eps 1e-9
using namespace std;
double p, q, r, s, t, u;
double findx(double x)
{
    return (p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x*x+u);//exp(x)为e^x
}
int main()
{
    while(scanf("%lf%lf%lf%lf%lf%lf",&p,&q,&r,&s,&t,&u)!=EOF)
    {
        double s1 = findx(0), s2 = findx(1.0);
        if(s1*s2 > 0)//说明方程没解
        {
            printf("No solution\n");
            continue;
        }
        double lf = 0.0, rf = 1.0, sum, m;
        while(rf-lf > eps)
        {
            m = (rf+lf)/2.0;
            sum = findx(m);
            if(sum < 0) rf = m;
            else lf = m;
        }
        printf("%.4lf\n", rf);
    }
    return 0;
}

 

posted @ 2015-02-03 16:37  人艰不拆_zmc  阅读(225)  评论(0编辑  收藏  举报