poj1654

题意:计算一个多边形的面积。

分析:题目中的答案要么是整数要么就是小数部分为0.5的小数,计算出中间结果之后判断能否被2整除,能就输出除以2得到的答案,不能就在这个答案后再加.5。总的面积要用64位int来保存。

#include <cstdio>
#include <cmath>
#define vector point
struct point
{
    int x,y;
    point(int xx = 0,int yy = 0)
    {
        x = xx;
        y = yy;
    }
    point operator - (const point& s)
    {
        return point(x - s.x, y - s.y);
    }
};
int cross_product(vector v1,vector v2)
{
    return v1.x * v2.y - v1.y * v2.x;
}
const int dir[10][2] = {{0,0},{-1,-1},{0,-1},{1,-1},{-1,0},{0,0},{1,0},{-1,1},{0,1},{1,1}};
void move(point p1,point& p2,int x)
{
    p2.x = p1.x + dir[x][0];
    p2.y = p1.y + dir[x][1];
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        __int64 s = 0;
        int x;
        point p1,p2;
        bool flag = false;
        p1 = point(0,0);
        while(scanf("%1d",&x) && x != 5)
        {
            move(p1,p2,x);
            s += cross_product(vector(p2),vector(p1));
            p1 = p2;
        }
        if(s < 0)
            s = -s;
        if(s % 2 == 0)
            printf("%I64d\n",s / 2);
        else
            printf("%I64d.5\n",s / 2);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ZShogg/p/3061440.html