uva10341

题目大意:
求出满足等式的x

思路:
如果formula(a) * formula(b) < 0 那么就是有解的。利用二分进行查找

代码:

#include <iostream>
using namespace std;
#include <cstring>
#include <stdio.h>
#include <cmath>
#define MIN (10e-8)
double p,q,r,s,t,u;

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

int main() {
    while(scanf("%lf%lf%lf%lf%lf%lf",&p,&q,&r,&s,&t,&u)!=EOF) {
    double left = 0 ;
    double right = 1,mid;
    if(fomula(left)*fomula(right) > 0) {
        printf("No solution\n");
        continue;
    }
    while(right - left > MIN) {
        mid = (right + left) /2;
        if(fomula(mid) * fomula(left) > 0) left = mid;
        else right = mid;
        }
    printf("%.4f\n",mid);
    }
    return 0;
}

你可能感兴趣的:(uva10341)