uva10341

题意:

给出一个方程,求解X;

思路:

因为方程是单调递减的,所以二分求解;

代碼:

#include
#include
#include
#include
#include
#include
using namespace std;
double p, q, r, s, t, u;

double solve(double a) {
        return p * exp(-a) + q * sin(a) + r * cos(a) + s * tan(a) + t * a * a + u;
}

int main() {
    double ll,rr,cur,ans;
    while(scanf("%lf%lf%lf%lf%lf%lf", &p, &q, &r, &s, &t, &u) != EOF) {
        ll=0.0, rr=1.0;
        if(solve(ll)*solve(rr) > 0) {
              printf("No solution\n");
              continue;
        }

        while(rr-ll>1e-10) {
            ans = ll+(rr-ll)/2.0;
            cur = solve(ans);
            if(cur>0) {
                ll=ans;
            }else 
                    rr=ans;
        }

        printf("%.4lf\n",ans);
    }
    return 0;
}

你可能感兴趣的:(暴力枚举)