HNUOJ

    xiaoxiao2021-08-22  156

    Crossed laddersTime Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KBTotal submit users: 374, Accepted users: 331Problem 10081 : No special judgementProblem descriptionA 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? InputEach line of input contains three positive floating point numbers giving the values of x, y, and c. OutputFor each line of input, output one line with a floating point number giving the width of the street in feet, with three decimal digits in the fraction. Sample Input 30 40 10 12.619429 8.163332 3 10 10 3 10 10 1 Sample Output 26.033 7.000 8.000 9.798

    解题思路

    几何问题,建立几何关系

    然后递归(二分法)求出中间值

    h1 = sqrt(x*x-d*d);

    h2 = sqrt(y*y-d*d);

    (h1-c)/h1 = d1/d = c/h2

    c = (h1*h2)/(h1+h2);

    通过二分法(逼近的思想)找到d即可

    代码:

    #include<iostream> #include<cstdio> #include<cmath> #define A 1e-5 //#define A 0.0000001 using namespace std; double x,y,c; double f(double d) { double h1,h2; double z; h1=sqrt(x*x-d*d); h2=sqrt(y*y-d*d); z=((h1*h2)/(h1+h2))-c; return z; } int main() { while(scanf("%lf%lf%lf",&x,&y,&c)==3) { //scanf("%lf%lf%lf",&x,&y,&c); double up=0,mid=0,down=0;//好坑啊,必须赋初值为0,否则无法AC x<y?up=y:up=x; while(up-down>A) { mid=(up+down)/2.0; if(f(mid)>0) { down=mid; } else { up=mid; } } printf("%0.3f\n",mid); } return 0; } 还有一个代码:

    #include<iostream> #include<stdio.h> #include<algorithm> #include<math.h> #include<string.h> #include<string> #include<stack> #include<vector> #include<map> using namespace std; #define N 2510 #define INF 0x3f3f3f3f #define met(a, b) memset(a, b, sizeof(a)) typedef long long LL; int main() { double x, y, c; while(scanf("%lf %lf %lf", &x, &y, &c)!=EOF) { double L = 0, R = min(x, y); while(L <= R) { double Mid = (L+R)/2; double h1 = sqrt(x*x-Mid*Mid); double h2 = sqrt(y*y-Mid*Mid); double temp = h1*h2/(h1+h2); if(fabs(temp - c)<1e-5) { printf("%.3f\n", Mid); break; } else if(temp > c) L = Mid; else R = Mid; } } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-676862.html

    最新回复(0)