标签:int cst lin operator eps write mes code href
题意:按逆时针或顺时针给出一个多边形,求面积。
解法:直接套用公式:\(S = \frac{1}{2}|\sum _ {i = 1} ^ {n} {v_i \times v_{i + 1}}|\)
别忘了POJ实数输出的时候必须%\(f\),不能%\(lf\)……
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(‘ ‘)
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e3 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ‘ ‘;
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - ‘0‘, ch = getchar();
if(last == ‘-‘) ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar(‘-‘);
if(x >= 10) write(x / 10);
putchar(x % 10 + ‘0‘);
}
int n;
struct Vec
{
db x, y;
db operator * (const Vec& oth)const
{
return x * oth.y - oth.x * y;
}
}a[maxn];
db area()
{
a[n + 1] = a[1];
db ans = 0;
for(int i = 1; i <= n; ++i) ans += a[i] * a[i + 1];
ans = ans < -eps ? -ans : ans;
return ans / 2.00;
}
int main()
{
while(scanf("%d", &n) && n)
{
for(int i = 1; i <= n; ++i) scanf("%lf%lf", &a[i].x, &a[i].y);
printf("%.0f\n", area());
}
return 0;
}
标签:int cst lin operator eps write mes code href
原文地址:https://www.cnblogs.com/mrclr/p/9973831.html