ZOJ 3203 Light Bulb【三分】
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3366
题目大意:求人从左向右走动时,影子的长度L的最大值
解题思路:当人在最左端的时候影子的长度全部投在地上,渐渐向右走影子开始投入到墙上。当人走到最右端影子全部在墙上。函数式先增加后减小的,满足三分法求解;
double cal(Type a)
{
/* 根据题目的意思计算出来的公式 */
return D-x+H-(H-h)*D/x;
}
推导过程如下:(运用2次相似三角形)
1>k/(D+k) = z/H; ---> k = Dz/(H-z)
2>k/(y+k) = z/h; ---> k = zy/(h-z)
So D/(H-z) = y/(h-z) ----解出z----> z = H - (H-h)*D/x
L = z + y ---> L = D-x+H-(H-h)*D/x;
代码如下:
View Code
#include<stdio.h> double D, H, h; double cal(double x) { return D-x+H-(H-h)*D/x; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%lf%lf%lf", &H, &h, &D); double left=(H-h)*D/H, right=D, mid, midmid; while(left+1e-9<=right) //while(left+1e-8<=right) 就是wa { mid=(left+right)/2; midmid=(mid+right)/2; if(cal(mid)>=cal(midmid)) right=midmid; else left=mid; } printf("%.3lf\n", cal(mid)); } return 0; }