Area

题目大意:从原点开始,1-4分别代表,向右下走,向右走,向右上走,向下走,5代表回到原点,6-9代表,向上走,向左下走,向左走,向左上走。求出最后的多边形面积。

分析:这个多边形面积很明显是不规则的,可以使用向量积直接求出来面积即可。

代码如下:

-----------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<string.h>

const int MAXN = 1e6+7;

 ///1-4分别代表,向右下走,向右走,向右上走,向下走,5代表回到原点,6-9代表,向上走,向左下走,向左走,向左上走。
int dir[10][2] = { {},{1,-1},{1,0},{1,1},{0,-1},{0,0},{0,1},{-1,-1},{-1,0},{-1,1} };
char s[MAXN];
int main()
{
    int T;

    scanf("%d", &T);

    while(T--)
    {
        scanf("%s", s);
        long long ans=0, x=0, y=0, nx, ny;

        for(int i=0; s[i]; i++)
        {
            nx = x+dir[ s[i]-'0' ][0];
            ny = y+dir[ s[i]-'0' ][1];

            ans += (x*ny - y*nx);
            x=nx, y=ny;
        }

        if(ans < 0)ans *= -1;

        if(ans % 2 == 0)
            printf("%lld
", ans/2);
        else
            printf("%lld.5
", ans/2);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/liuxin13/p/4913247.html