Ball Dropping
题目描述
给定一个上宽下窄的梯形,中间放一个半径为\(r\)的球,上底长为\(a\),下底长为\(b\),高为\(h\),求球心到下底的距离。
范围
\(r,a,b,h \leq 1000,a > b\)
题解
思路1:二分一个长度带入验证
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
double Fup (double a,double b) {
return sqrt(a * a + b * b);
}
double Fdown(double a,double b) {
return sqrt(a * a - b * b);
}
double c;//xiebian
double r,a,b,h;
bool check(double x) {
double rem = h - x;
double kx = Fup(a / 2,rem);
double ky = Fup(b / 2,x);
double some = Fdown(ky,r);
double all = Fup((a - b) / 2,h);
double other = Fdown(kx,r);
if(all - (some + other) < eps) return 1;
else return 0;
}
int main () {
cin >> r >> a >> b >> h;
if(2 * r < b) {
puts("Drop");
return 0;
}
double l = 0;double r = 2 * h + 1;
while (r - l > eps) {
double mid = (l + r) / 2;
if(check(mid)) r = mid;
else l = mid;
}
puts("Stuck");
printf("%.10lf\n",r);
return 0;
}
思路2:推公式
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
double r,a,b,h;
scanf("%lf %lf %lf %lf",&r,&a,&b,&h);
if(2.0*r<b) puts("Drop");
else {
puts("Stuck");
double A=(a-b)/2.0,C=sqrt(h*h+A*A);
printf("%.10lf",r*A/C+(r*h/C-b/2.0)*h/A);
}
}