lightoj 1062 二分法

来源:互联网 发布:mac怎样隐藏文件 编辑:程序博客网 时间:2024/06/06 07:44

设宽为a,那么可以求得

c/sqrt( y^2 - a ^2 ) + c/sqrt( x^2 - a ^2 )  = 1

又 a的范围是   大于0 小于 min(x,y)

二分搜索即可

AC代码如下:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;double x, y, c;bool judge( double a ){//    double temp1 = c * ( sqrt( y * y - a * a ) + sqrt( x * x - a * a ) );//    double temp2 = sqrt( y * y - a * a ) * sqrt( x * x - a * a );    if( c * ( sqrt( y * y - a * a ) + sqrt( x * x - a * a ) ) >= sqrt( y * y - a * a ) * sqrt( x * x - a * a ) ){        return true;    }else{        return false;    }}int main(){    int T, Case = 1;    cin >> T;    while( T-- ){        cin >> x >> y >> c;        double l = 0, r = min( x, y ), mid;        while( r - l > 1e-6 ){            mid = ( l + r ) / 2;            if( judge( mid ) ){                r = mid;            }else{                l = mid;            }        }        printf( "Case %d: %.7lf\n", Case++, l );    }    return 0;}


0 0
原创粉丝点击