#include #include double func(double x){ // return tan(x) - 4.0/3.0; return x*x+2.0*x+0.999999; } int step = 0; /* Find x | func(x)=0, x1 < x < x2 by bisection_method. func(x) must be monotonic increasing in interval [x1, x2] and satisfies func(x1) < 0 and func(x2) > 0. acc is accuracy of the solution. */ double bisection_method(double x1, double x2, double acc){ double mid = (x1+x2)/2.0; double fmid = func(mid); step++; printf("step:%d %lf %lf\n", step, x1, x2); if(fabs(fmid) < acc) return mid; else if(fmid < 0) return bisection_method(mid, x2, acc); else return bisection_method(x1, mid, acc); } int main(){ double x1, x2; scanf("%lf %lf", &x1, &x2); printf("%lf\n", bisection_method(x1, x2, 0.000001)); return 0; }