1. 程式人生 > >二分答案法、三分法

二分答案法、三分法

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define clr( a , x ) memset ( a , x , sizeof (a) );
#define RE freopen("1.in","r",stdin);
#define WE freopen("1.out","w",stdout);
#define SpeedUp std::cout.sync_with_stdio(false);
const int maxn = 1e5+5;
const int inf = 0x3f3f3f3f;
double a,b,c,x,y;

double val(double X){
    return sqrt((X-x)*(X-x)+(a*X*X+b*X+c-y)*(a*X*X+b*X+c-y));
}

double solve(double l,double r){
    double eps = 1e-5;
    while(l+eps<r){
        double lmid = l + (r-l)/3,rmid = r - (r-l)/3;
        if(val(lmid) < val(rmid)){
            r = rmid;
        }else{
            l = lmid;
        }
    }
    return val(l);
}
int main(){
    // RE
    while(cin>>a>>b>>c>>x>>y){
        printf("%.3f\n", solve(-200.0,200.0));
    }
    return 0;
}