poj1265

本文介绍了一个基于Pick公式的计算几何应用实例,通过计算格子点构成的简单多边形的面积,展示了如何利用Pick公式进行计算,并提供了一段C++代码实现,该程序能够读取输入并计算出多边形的边上的点数和内部的点数,最终给出多边形的面积。

Pick公式:平面上以格子点为顶点的简单多边形,如果边上的点数为on,内部的点数为in,则它的面积为area=on/2+in-1

ContractedBlock.gifExpandedBlockStart.gifView Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;

struct Point
{
int x, y;
} s, e;

int t, n;

int gcd(int x, int y)
{
if (!x || !y)
return x > y ? x : y;
for (int t; t = x % y; x = y, y = t)
;
return y;
}

void work()
{
int x, y;
scanf("%d", &n);
s.x = 0;
s.y = 0;
int ans = 0;
int cnt = 0;
for (int i = 0; i < n; i++)
{
scanf("%d%d", &x, &y);
e.x = s.x + x;
e.y = s.y + y;
ans += e.y * s.x - e.x * s.y;
cnt += gcd(abs(x), abs(y));
s = e;
}
printf("%d %d %.1f\n", (ans - cnt) / 2 + 1, cnt, ans / 2.0);
}

int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++)
{
printf("Scenario #%d:\n", i + 1);
work();
putchar('\n');
}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值