POJ 1654 Area (有向面积求多边形面积)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    //向量
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    //叉积
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    //点积
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
    void input()
    {
        scanf("%lf%lf",&x,&y);
    }
};

const int maxn = 1100000;

char cmd[maxn];

int main()
{
    int t;

    scanf("%d", &t);
    while (t--) {
        scanf("%s", cmd);
        int len = strlen(cmd);
        Point cur(0,0), next;
        double ans = 0;
        for (int i = 0; i < len; i++) {
            if (cmd[i] == '8') {
                next.x = cur.x;
                next.y = cur.y + 1;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '2') {
                next.x = cur.x;
                next.y = cur.y - 1;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '6') {
                next.x = cur.x + 1;
                next.y = cur.y;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '4') {
                next.x = cur.x - 1;
                next.y = cur.y;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '1') {
                next.x = cur.x - 1;
                next.y = cur.y - 1;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '3') {
                next.x = cur.x + 1;
                next.y = cur.y - 1;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '7') {
                next.x = cur.x - 1;
                next.y = cur.y + 1;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '9') {
                next.x = cur.x + 1;
                next.y = cur.y + 1;
                ans += cur ^ next;
                cur = next;
            }
        }
        ans = fabs(ans) / 2.0;
        if (fabs(floor(ans) - ans) < 1e-8)
            printf("%.0lf\n", ans);
        else
            printf("%.1lf\n", ans);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值