Rancher's Gift

Description

Download as PDF

Rancher Joel has a tract of land in the shape of a convex quadrilateral that he wants to divide among his sons Al, Bob, Chas and Dave, who wish to continue ranching on their portions, and his daughter Emily, who wishes to grow vegetables on her portion.

The center of the tract is most suitable for vegetable farming so Joel decides to divide the land by drawing lines from each corner (A, B, C, D in counter clockwise order) to the center of an opposing side (respectively A', B', C' and D' ). Each son would receive one of the triangular sections and Emily would receive the central quadrilateral section. As shown in the figure, Al's tract is to be bounded by the line from A to B, the line from A to the midpoint of BC and the line from B to the midpoint of CD; Bob's tract is to be bounded by the line from B to C, the line from B to the midpoint of CD and the line from C to the midpoint of DA, and so on.

Your job is to write a program that will help Rancher Joel determine the area of each child's tract and the length of the fence he will have to put around Emily's parcel to keep her brothers' cows out of her crops.

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.

Input

The first line of input contains a single integer P, ( 1P1000), which is the number of data sets that follow. Each data set is a single line that contains of a decimal integer followed by five (5) space separated floating-point values. The first (integer) value is the data set number, N. The floating-point values are B.xC.xC.yD.x and D.y in that order (where V.x indicates the x coordinate of V and V.yindicates the y coordinate of V). Recall that the y coordinate of B is always zero (0).

The supplied coordinates will always specify a valid convex quadrilateral.

Output

For each data set there is a single line of output. It contains the data set number, N, followed by a single space followed by five (5) space separated Itoating point values to three (3) decimal place accuracy, followed by a single space and a decimal integer. The floating-point values are the areas in acres of the properties of Al, Bob, Chas, Dave, and Emily respectively. The final integer is the length of fence infeet required to fence in Emily's property (rounded up to the next foot).

Sample Input

3
1 200 250 150 -50 200
2 200 200 100 0 100
3 201.5 157.3 115.71 -44.2 115.71

Sample Output

1 35.000 54.136 75.469 54.167 54.666 6382
2 25.000 25.000 25.000 25.000 25.000 4589
3 29.144 29.144 29.144 29.144 29.144 4937


此题看起来比较复杂,但纯数学问题。在当时做的时候被题目的长度下倒,没有动手做。在一次无聊时看了一遍题目发现很简单,于是就敲出了代码。对此题无力吐槽。没有一点算法上的知识,也许初中生就能搞定的问题。

题目大意:

就是把一个四边形分成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;
}


你可能感兴趣的:(数学问题)