POJ 1654 (平面几何 多边形面积 水~)

题目链接:点击这里

题意:求多边形的面积.

就输出处理一下就好了.

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

const int eps = 1e-8;
const int INF = 1e20;
const int pi = acos (-1.0);

int dcmp (int x) {
    if (fabs (x) < eps) return 0;
    return (x < 0 ? -1 : 1);
}
inline int sqr (int x) {return x*x;}

//*************点
struct Point {
    int x, y;
    Point (int _x = 0, int _y = 0):x(_x), y(_y) {}
    void input () {scanf ("%lf%lf", &x, &y);}
    void output () {printf ("%.2f %.2f\n", x, y);}
    bool operator == (const Point &b) const {
        return (dcmp (x-b.x) == 0 && dcmp (y-b.y) == 0);
    }
    bool operator < (const Point &b) const {
        return (dcmp (x-b.x) == 0 ? dcmp (y-b.y) < 0 : x < b.x);
    }
    Point operator + (const Point &b) const {
        return Point (x+b.x, y+b.y);
    }
    Point operator - (const Point &b) const {
        return Point (x-b.x, y-b.y);
    }
    Point operator * (int a) {
        return Point (x*a, y*a);
    }
    Point operator / (int a) {
        return Point (x/a, y/a);
    }
    int len2 () {//返回长度的平方
        return sqr (x) + sqr (y);
    }
    int len () {//返回长度
        return sqrt (len2 ());
    }
};

int cross (Point a, Point b) {//叉积
    return 1LL*a.x*b.y-1LL*a.y*b.x;
}
//*************多边形

double polygon_area (Point *p, int n) {//多边形的有向面积,加上绝对值就是面积
    //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;
}

#define maxn 1000005
Point p[maxn];
int n;
char s[maxn];

int main () {
    int t;
    cin >> t;
    while (t--) {
        scanf ("%s", s);
        n = strlen (s); n--;
        if (n == 0 || n == 2) {
            printf ("0\n");
            continue;
        }
        n--;
        int len = 0;
        p[len++] = Point (0, 0);
        for (int i = 0; i < n; i++) {
            if (s[i] == '8') p[len++] = p[len-1]+Point (0, 1);
            if (s[i] == '7') p[len++] = p[len-1]+Point (-1, 1);
            if (s[i] == '9') p[len++] = p[len-1]+Point (1, 1);
            if (s[i] == '4') p[len++] = p[len-1]+Point (-1, 0);
            if (s[i] == '6') p[len++] = p[len-1]+Point (1, 0);
            if (s[i] == '1') p[len++] = p[len-1]+Point (-1, -1);
            if (s[i] == '2') p[len++] = p[len-1]+Point (0, -1);
            if (s[i] == '3') p[len++] = p[len-1]+Point (1, -1);
        }
        double ans = fabs (polygon_area (p, len))*2+eps;
        long long res = (long long)ans;
        printf ("%lld%s\n", res/2, (res&1 ? ".5" : ""));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值