Area in Triangle
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1513 Accepted: 774
Description
Given a triangle field and a rope of a certain length (Figure-1), you are required to use the rope to enclose a region within the field and make the region as large as possible.
Input
The input has several sets of test data. Each set is one line containing four numbers separated by a space. The first three indicate the lengths of the edges of the triangle field, and the fourth is the length of the rope. Each of the four numbers have exactly four digits after the decimal point. The line containing four zeros ends the input and should not be processed. You can assume each of the edges are not longer than 100.0000 and the length of the rope is not longer than the perimeter of the field.
题解
三种情况。
撑开的绳子有三种形状:l>=a+b+c时,是三角形;l<=内切圆周长时,是圆形;其余都是圆角三角形。
(因为同样的周长圆的面积最大~)
我们发现三个角上的不规则图形刚好可以拼起来变成一个与大三角形相似的小三角形-与大圆相似的小圆,其中两图形的相似比相同。所以我们可以列方程求解小圆半径。
设大圆半径R,小圆半径r,三角形三边a,b,c和a,b,c,那么我们可以用海伦公式求出大三角形面积S。
然后,因为2*S=(A+B+C)*R,所以我们又可以求出来R。
然后列方程:
(A+B+C)(R-r)/R+2π*r=l
解方程求得r,最后的结果即为s-s*r*r/R/R+r*r*π~
注意第二种情况答案不是内切圆的面积!
代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
double solve(double a,double b,double c,double d)
{
double L,Pi,cosA,S,r;
Pi=acos(-1.0);
L=a+b+c;
cosA=(b*b+c*c-a*a)/(2*b*c);
S=0.5*b*c*(sqrt(1-cosA*cosA));
r=S*2/L;
if (d>L) return S;
else if (2*Pi*r>=d) return d*d/(4*Pi);
else
{
double t,rr;
t=(L-d)/(L-2*Pi*r);
rr=r*t;
return S-S*t*t+Pi*rr*rr;
}
}
int main()
{
double a,b,c,d;
int cas=0;
while(scanf("%lf%lf%lf%lf",&a,&b,&c,&d)!=EOF)
{
if (a==0&&b==0&&c==0&&d==0) break;
cas++;
printf("Case %d:%0.2lf\n",cas,solve(a,b,c,d));
}
return 0;
}