通过三角形相似的性质,设要求的边为w,推出以下方程 c * sqrt(x * x - w * w) + c * sqrt(y*y -w*w) - sqrt(x * x - w * w) * sqrt(y * y - w * w)
w的上届是x,y中较小值(斜边大于直角边),然后二分该方程。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
inline double cal(double x,double y,double c,double w)
{
return c * sqrt(x * x - w * w) + c * sqrt(y*y -w*w) - sqrt(x * x - w * w) * sqrt(y * y - w * w);
}
int main()
{
double x,y,c;
double up;
double down;
double ans,mid;
while(scanf("%lf%lf%lf",&x,&y,&c)!=EOF)
{
down = 0;
up = x < y ? x : y;
while(true)
{
mid = (up+down)/2.0;
ans = cal(x,y,c,mid);
if(fabs(ans)<=0.000001)
{
break;
}
else if(ans>0)
{
up = mid;
}
else
{
down = mid;
}
}
printf("%.3f\n",mid);
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
inline double cal(double x,double y,double c,double w)
{
return c * sqrt(x * x - w * w) + c * sqrt(y*y -w*w) - sqrt(x * x - w * w) * sqrt(y * y - w * w);
}
int main()
{
double x,y,c;
double up;
double down;
double ans,mid;
while(scanf("%lf%lf%lf",&x,&y,&c)!=EOF)
{
down = 0;
up = x < y ? x : y;
while(true)
{
mid = (up+down)/2.0;
ans = cal(x,y,c,mid);
if(fabs(ans)<=0.000001)
{
break;
}
else if(ans>0)
{
up = mid;
}
else
{
down = mid;
}
}
printf("%.3f\n",mid);
}
return 0;
}