您的位置:首页 > 其它

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

2016-06-22 12:37 513 查看

题目链接:点击这里

题意:求多边形的面积.

就输出处理一下就好了.

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: