POJ 1654 Area(多边形面积)

博客原文地址:http://blog.youkuaiyun.com/xuechelingxiao/article/details/40618519


Area


题目大意:

给你一个数字组成的字符串,每一位都有1-9(除了5)8个数字,表示8个方向的移动,字符串以5结尾。问最后轨迹形成的多边形的面积是多少。


解题思路:

其实这个题本身并不是很难,关键是细节的处理比较蛋疼。。。首先100w的数组起初我用double开的时候MLE
了,然后看了一下,确实没必要用double,就改成int了。然后就是输出格式,刚开始我以为是SPJ,就直接用double写的,
WA了发现竟然不是SPJ,那怎么保留。。。原来是如果有小数就输出小数,没有就不输出,加个 %2 判断就好了。还有就是多边形面积可能会超int,用long long就可以了。

其他坑点应该就没什么了-。-


其他的详见代码吧:

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <algorithm>
#define LL long long
//#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define max3(a, b, c) (a>b?max(a, c):max(b, c))
#define min3(a, b, c) (a<b?min(a, c):min(b, c))
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define Read()  freopen("data.in", "r", stdin);
#define Write() freopen("data.out", "w", stdout);

using namespace std;

int dx[10] = {0, -1, 0, 1, -1, 0, 1, -1, 0, 1};
int dy[10] = {0, -1, -1, -1, 0, 0, 0, 1, 1, 1};

struct Point {
    int x, y;
} P[1000010];

int xmult(Point a, Point b, Point c) {
    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}

char s[1000010];

int main()
{
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%s", s);
        int len = strlen(s);
        P[0].x = 0;
        P[0].y = 0;
        for(int i = 0; i < len-1; ++i) {
            P[i+1].x = P[i].x+dx[s[i]-'0'];
            P[i+1].y = P[i].y+dy[s[i]-'0'];
        }
        LL area = 0;
        Point o = {0, 0};
        for(int i = 1; i < len-1; ++i) {
            area += xmult(P[i], P[i+1], o);
        }
        area = area > 0 ? area : -area;
        if(area % 2 == 0)
            printf("%lld\n", area/2);
        else 
            printf("%lld.5\n", area/2);
    }

    return 0;
}

/*test case*/
/*

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值