用一次导数求极值,但是还是犯了错误,要判断边界条件,就是墙上投影值小于0和大于h的时候。
//-------common header--------------- #include <stdio.h> #include <vector> #include <stack> #include <math.h> #include <algorithm> typedef unsigned long u32; using namespace std; int main() { int caseNum = 0; scanf("%d",&caseNum); while(caseNum--) { double H,h,D; scanf("%lf %lf %lf",&H,&h,&D); double result = 0.0; if(H-h>=D) { result = h; } else if(H*H<=D*(H-h)) { result = h*D/H; } else { double a = -sqrt((H-h)*D)+H; result = D*(h-a)/(H-a)+a; } printf("%.3f/n",(float)result); } return 1; }