装水问题
有一个侧面看去是半圆的储水装置,该圆的半径为R,要求往里面装入高度为h的水,使其在侧面看区的面积S1与半圆面积S2的比例恰好为r。现给定R和r,求高度h。
#include<iostream>
#include<cmath>
using namespace std;
const double PI = acos(-1.0); // PI
const double eps = 1e-6;
double f(double R,double h)
{
double alpha = 2*acos((R-h)/R);
double L=2*sqrt(R*R-(R-h)*(R-h));
double S1= alpha * R * R /2 - L * (R-h) /2;
double S2= PI *R *R / 2; //扇形面积公式S=R*R*θ/2;
return S1/S2;
}
double solve(double R,double r)
{
double left=0,right=R,mid;
while(right-left>eps)
{
mid = (left + right)/2;
if(f(R,mid)>r) right=mid;
else left=mid;
}
return mid;
}
int main()
{
double R,r;
cin >> R >> r;
printf("%.4f\n",solve(R,r));
return 0;
}
来源:CSDN
作者:one joker
链接:https://blog.csdn.net/zhongxinyun/article/details/104113015