uva 10341 Solve It(二分搜索)

题目连接:10341 Solve It


题目大意:根据给出的数学公式,在[0,1]上求解。


解题思路:二分搜索, 主要注意一下边界和精度就可以了。


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

double p, q, r, s, t, u, cur;
double count(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) == 6) {
	double a = 0, b = 1;
	if (count(a) * count(b) > 0)
	    printf("No solution\n");
	else if (fabs(count(0)) <= 0.0001)
	    printf("0.0000\n");
	else if (fabs(count(1)) <= 0.0001)
	    printf("1.0000\n");
	else {
	    if (count(0) > 0) {
		a = 1;
		b = 0;
	    }
	    else {
		a = 0;
		b = 1;
	    }
	    while (1) {
		cur = (a + b) / 2;
		if (fabs(count(cur)) <= 0.00000001)
		    break;
		else if(count(cur) > 0)
		    b = cur;
		else
		    a = cur;
	    }
	    printf("%.4lf\n", cur);
	}
    }
    return 0;
}


你可能感兴趣的:(uva 10341 Solve It(二分搜索))