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; }
相关文章推荐
- 安装tomcat的native-1.2.7遇到openssl版本提示问题
- POJ 2151 Check the difficulty of problems
- 监听-我的一些理解
- 定义抽象类的注意事项
- 分布式高级数据库(怀念一下我得了及格的高级数据库)
- spring读取配置文件优化
- 安卓_Android_studio_自动导包
- reactiveCocoa实践一
- Android 获取imageview的图,在另一个imageview里显示。
- HDU 3007 【随机增量法】
- "Java消息回收机制"之源码+图文完全解析
- linux 进程单例
- java 基础安装和Tomcat8配置
- Python基础-简单输出
- Java mail乱码
- 百度2017暑期实习生编程题-页面调度算法
- matlab制作视频,还待完善
- 成语、典故的出处
- solution Of 1014. Waiting in Line (30)
- nginx: [error] invalid PID number “” in “/usr/local/var/run/nginx/nginx.pid”