您的位置:首页 > 其它

poj 1654 area 简单多边形面积(简单题)

2016-07-24 20:29 399 查看
题目和描述都挺简单,注意long long 即可

//此题注意连x,y都要用long long 不然在+运算的时候会溢出
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
int dx[10] = {0, 1, 1, 1, 0, 0, 0,-1,-1,-1};
int dy[10] = {0,-1, 0, 1,-1, 0, 1,-1, 0, 1};

ll cross(ll x1,ll y1,ll x2,ll y2){
return x1*y2 - x2*y1;
}
char s[1001000];

int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--){
scanf(" %s",s);
ll ans ;
int len = strlen(s);
if(len < 3) ans = 0;
else{
ll S = 0;
ll tx = 0,ty = 0;
for(int i =0;i<len-1;i++){
int index = s[i] - '0';
ll x = tx + dx[index],y = ty + dy[index];
S += cross(tx,ty,x,y);//叉积计算面积
tx = x,ty = y;
}
ans = llabs(S);
}
if(ans % 2 == 0) printf("%I64d\n",ans/2);
else printf("%I64d.5\n",ans/2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: