Crossed Ladders
Input: Standard Input
Output: Standard Output
Time Limit: 1 Second
A narrow street is lined with tall buildings. An x foot long ladder is rested at the base of the building on the right side of the street and leans on the building on the left side. A y foot long ladder is rested at the base of the building on the left side of the street and leans on the building on the right side. The point where the two ladders cross is exactly c feet from the ground. How wide is the street?
Each line of input contains three positive floating point numbers giving the values of x, y, and c.<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+Rm9yIGVhY2ggbGluZSBvZiBpbnB1dCwgb3V0cHV0IG9uZSBsaW5lIHdpdGggYSBmbG9hdGluZyBwb2ludCBudW1iZXIgZ2l2aW5nIHRoZSB3aWR0aCBvZiB0aGUgc3RyZWV0IGluIGZlZXQsIHdpdGggdGhyZWUgZGVjaW1hbCBkaWdpdHMgaW4gdGhlIGZyYWN0aW9uLjwvcD4KPGgxIGFsaWduPQ=="left">Sample Input Output for Sample Input
30 40 10
12.619429 8.163332 3
10 10 3
10 10 1
26.033
7.000
8.000
9.798
題意:兩棟樓之間有兩個梯子,如圖中的虛線所示,一個梯子的長度為x,另一個梯子的長度為y,兩個梯子的交點離地面的高度為c,問兩棟樓之間的距離。
這是一個幾何題。設寬度為w,交點距左樓距離為a,則根據三角形相似可以推出:
把第二個式子代入第一個式子可以得出一個等式,把方程左邊寫成關於w的函數,求導可得是減函數。因為w一定小於x,y中的最小值(三角形的直角邊小於斜邊),二分求出答案即可。
#include#include #include #define Min(a,b) a <= b ? a : b; using namespace std; double x, y, c; double get_ans(double w) { return 1 - c / sqrt(x * x - w * w) - c / sqrt(y * y - w * w); } int main() { while(~scanf("%lf%lf%lf",&x,&y,&c)) { double l = 0, mid, r = Min(x,y); while(r - l > 1e-8) { mid = (l + r) / 2; if(get_ans(mid) > 0) l = mid; else r = mid; } printf("%.3lf\n",mid); } return 0; }