此题看起来比较复杂,但纯数学问题。在当时做的时候被题目的长度下倒,没有动手做。在一次无聊时看了一遍题目发现很简单,于是就敲出了代码。对此题无力吐槽。没有一点算法上的知识,也许初中生就能搞定的问题。
题目大意:
就是把一个四边形分成5块。分别算出这5块的面积,和求出中间那个四边形的周长。注意这段话(For this problem, A will always be at (0, 0) and B will always be at (x, 0). Coordinates will be in rods (a rod is 16.5 feet). The returned areas should be in acres to 3 decimal places (an acre is 160 square rods) and the length of the fence should be in feet, rounded up to the next foot.)要注意单位的换算。别的就没什么的了,就直接求出每个点的坐标,这样比较直接明了。
直接看代码吧
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
double juli(double x1,double y1,double x2,double y2)//两点间的距离
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
//两条直线的交点 X
double X(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
return ((y2-y3)*(x1-x2)*(x3-x4)+(y3-y4)*(x1-x2)*x3-(y1-y2)*(x3-x4)*x2)/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4));
}
//两条直线的交点 Y
double Y(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
return y3+(y3-y4)/(x3-x4)*(X(x1,y1,x2,y2,x3,y3,x4,y4)-x3);
}
//海伦公式求面积
double S(double l1,double l2,double l3)
{
double p=(l1+l2+l3)/2;
return sqrt(p*(p-l1)*(p-l2)*(p-l3));
}
//算中点
double X0(double x1,double x2)
{
return (x1+x2)/2;
}
double Y0(double y1,double y2)
{
return (y1+y2)/2;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
double bx,cx,cy,dx,dy;
scanf("%d%lf%lf%lf%lf%lf",&n,&bx,&cx,&cy,&dx,&dy);
printf("%d ",n);
double l1,l2,l3,l4,l5;
l1=abs(bx);
l2=juli(bx,0,cx,cy);
l3=juli(cx,cy,dx,dy);
l4=juli(0,0,dx,dy);
l5=juli(0,0,cx,cy);
double s11=S(l1,l2,l5);
double s22=S(l3,l4,l5);
double SS=s11+s22;//求出总面积
double axx,ayy,bxx,byy,cxx,cyy,dxx,dyy;
double aax,aay,bbx,bby,ccx,ccy,ddx,ddy;
//求出中点坐标
dxx=X0(0,bx);
dyy=Y0(0,0);
axx=X0(bx,cx);
ayy=Y0(0,cy);
bxx=X0(cx,dx);
byy=Y0(cy,dy);
cxx=X0(dx,0);
cyy=Y0(dy,0);
//求出每个交点坐标
aax=X(dxx,dyy,dx,dy,0,0,axx,ayy);
aay=Y(dxx,dyy,dx,dy,0,0,axx,ayy);
bbx=X(bx,0,bxx,byy,axx,ayy,0,0);
bby=Y(bx,0,bxx,byy,axx,ayy,0,0);
ccx=X(bx,0,bxx,byy,cx,cy,cxx,cyy);
ccy=Y(bx,0,bxx,byy,cx,cy,cxx,cyy);
ddx=X(cx,cy,cxx,cyy,dx,dy,dxx,dyy);
ddy=Y(cx,cy,cxx,cyy,dx,dy,dxx,dyy);
//分别求出四个三角形的面积
double s1,s2,s3,s4,s5,l;
l1=juli(0,0,bx,0);
l2=juli(bx,0,bbx,bby);
l3=juli(bbx,bby,0,0);
s1=S(l1,l2,l3);
l1=juli(bx,0,cx,cy);
l2=juli(cx,cy,ccx,ccy);
l3=juli(ccx,ccy,bx,0);
s2=S(l1,l2,l3);
l1=juli(cx,cy,dx,dy);
l2=juli(dx,dy,ddx,ddy);
l3=juli(ddx,ddy,cx,cy);
s3=S(l1,l2,l3);
l1=juli(dx,dy,0,0);
l2=juli(0,0,aax,aay);
l3=juli(aax,aay,dx,dy);
s4=S(l1,l2,l3);
//求中间四边形的边长以及周长,周长要转化成 int
l1=juli(aax,aay,bbx,bby);
l2=juli(bbx,bby,ccx,ccy);
l3=juli(ccx,ccy,ddx,ddy);
l4=juli(ddx,ddy,aax,aay);
int d=(int)((l1+l2+l3+l4)*16.5);
printf("%.3f %.3f %.3f %.3f %.3f %d\n",s1/160,s2/160,s3/160,s4/160,(SS-s1-s2-s3-s4)/160,d+1);
}
return 0;
}