hdu2036 (计算多边形的面积)

Input
输入数据包含多个测试实例,每个测试实例占一行,每行的开始是一个整数n(3<=n<=100),它表示多边形的边数(当然也是顶点数),然后是按照逆时针顺序给出的n个顶点的坐标(x1, y1, x2, y2... xn, yn),为了简化问题,这里的所有坐标都用整数表示。
输入数据中所有的整数都在32位整数范围内,n=0表示数据的结束,不做处理。
 

 

Output
对于每个测试实例,请输出对应的多边形面积,结果精确到小数点后一位小数。
每个实例的输出占一行。
 

 

Sample Input
3 0 0 1 0 0 1 4 1 0 0 1 -1 0 0 -1 0
 

 

Sample Output
0.5 2.0


模板~

 

#include <stdio.h>

#include <string.h>



struct Point

{

    double x, y;

    Point(double x=0, double y=0):x(x), y(y) {}

};

typedef Point Vector;

Point xy[110];

//叉积等于向量A和B组成的三角形的有向面积的两倍。

double Cross(Vector A, Vector B) {return A.x*B.y - A.y*B.x; }

//点 - 点 = 向量

Vector operator - (Point A, Point B) {return Vector(A.x-B.x, A.y-B.y); }

//多边形的有向面积

double PloygonArea(Point* p, int n)

{

    double  area = 0;

    for(int i=1; i<n-1; i++)

        area  += Cross(p[i]-p[0], p[i+1] - p[0]);

    return area / 2;

}



int main()

{

    int n, i;

    while(scanf("%d",&n),n)

    {

        for(i=0; i<n; i++)

            scanf("%lf%lf",&xy[i].x,&xy[i].y);

        double sum = PloygonArea(xy,n);

        printf("%.1lf\n",sum);

    }

    return 0;

}






 





 

你可能感兴趣的:(HDU)