1062 - Crossed Ladders
InputA 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?
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each test case contains three positive floating point numbers giving the values of x, y, and c.
Output
For each case, output the case number and the width of the street in feet. Errors less than 10-6 will be ignored.
Sample Input |
Output for Sample Input |
430 40 1012.619429 8.163332 310 10 3
10 10 1 |
Case 1: 26.0328775442Case 2: 6.99999923Case 3: 8Case 4: 9.797958971 |
题目类型:二分
算法分析:直接手算得出求c的公式,然后二分枚举胡同的宽度即可,注意二分枚举的开始区间为[0, min (a,b)]!!!!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#include <iostream> #include <fstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; double x, y, c; bool BinSearch (double val) { double k = sqrt (x * x - val * val), q = sqrt (y * y - val * val); double ans = (k * q) / (k + q); if (ans < c) return false; return true; } int main() { // ifstream cin ("aaa.txt"); int t; cin >> t; int i; for (i = 1; i <= t; i++) { cin >> x >> y >> c; double L = 0, R = min (x, y), mid = 0; int cnt = 1; while (cnt <= 66) { mid = (L + R) / 2.0; if (BinSearch (mid)) L = mid; else R = mid; cnt++; } cout << "Case " << i << ": " << fixed << setprecision (6) << L << endl; } return 0; } |
- « 上一篇:lightoj1056
- lightoj1067:下一篇 »