二分法:单调增加或单调减少
mid=(LR )/2;
mid=(midr )/2;
//bulbcomparedtowildleopard ' swealthiness,yjdssmildleopardisratherpoor.hishouseisnarrowandhehasonlyonelightbulinhishoushishoushoustbbineshorathistthoronesthisttthoororestt heiswanderinginhisincommodioushouse,thinkingofhowtoearnmoremoney.one day, hefoundthatthelengthofhisshadowwaschangingfromtimetotimewhilewalkingbetweenthelightbulbandthewallofhishouse.asuddenthought rarad
输入
thefirstlineoftheinputcontainsanintegert (t=100 ),indicating the number of cases。
eachtestcasecontainsthreerealnumbersh、 handdinoneline.histheheightofthelightbulbwhilehistheheightofmildleopard.disdistancebetweenthelightbulbandthewall.alllnu.alllllnu mbmbmbund
Output
For each test case,outputthemaximumlengthofmildleopard ' sshadowinoneline,accurate up to three decimal places .
样品输入
3210.52 0.5 3434示例输出
1.0000.7504.000解决问题:由勤快八宝粥/p计算出的该公式,可以直接分三份求出极值点,同样,由该公式用O(1)的方法求解勤快八宝粥/p注:浮点数不能直接比较大小
定义精度通常必须小于10或10
#define eps 1e-8
# include iostream # include algorithm # include cstdio # include cstdlib # include cstring # include cmath # define EPS1e-8 usingnanage doubleans(doubley ) { double x; x=d-yh-(h-h ) *D/y; 返回x; }doubles(doublel,double r ) doublemid、mmid、left、right; left=l; right=r; wile(leftEPSright ) mid=) leftright )/2; mid=(midright )/2; if(ans(mid )=ans (mmid ) ) right=mmid; else left=mid; }returnans(right ); (}int main ) ) { int t; scanf('%d ',t ); while(t-- ) scanf ) ' %lf%lf ) lf ',h,h,d ); double l=D-D*h/H,r=D; doublef=s(L,r ); printf('%.3f(n ),f ); } return 0; }