POJ 1654
来源:互联网 发布:java 指定jdk版本运行 编辑:程序博客网 时间:2024/06/05 15:53
题目:
求n个点的多边形的有向面积,算法见书P258
#include <iostream>#include<cstdio>#include<cstring>#define M 1000000using namespace std;char s[M+5];int stepx[10]={ 0,-1,0,1,-1,0,1,-1,0,1};int stepy[10]={ 0,-1,-1,-1,0,0,0,1,1,1};struct Point{ double x,y; Point(double x=0,double y=0):x(x),y(y) {}}p[3];typedef Point Vector;Vector operator - (Point A,Point B) {return Vector(A.x-B.x,A.y-B.y);}double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}double Area(Point *p,int n){ double area=0; for(int i=1;i<=n-1;i++) { area+=Cross(p[i]-p[0],p[i+1]-p[0]); // printf("area=%.2f\n",area); } if(area<0) area=-area; return area/2;}int main(){ int ncase; int len,n; double area; scanf("%d",&ncase); while(ncase--) { scanf("%s",s); len=strlen(s); n=len-2; if(n<=1) puts("0"); else{ area=0; p[1].x=0+stepx[s[0]-'0'], p[1].y=0+stepy[s[0]-'0']; for(int i=2;i<=n;i++) { p[2].x=p[1].x+stepx[s[i-1]-'0'], p[2].y=p[1].y+stepy[s[i-1]-'0']; area+=Cross(p[1]-p[0],p[2]-p[0]); // printf("area=%.2f\n",area); p[1]=Point(p[2].x,p[2].y); } if(area<0) area=-area; long long a=(long long)(area); // 要用longlong或者 __int64 // printf("a=%d\n",a); if( a%2) printf("%lld.5\n",a/2); else printf("%lld\n",a/2); } } return 0;}
0 0
- poj 1654
- POJ 1654
- poj 1654
- poj 1654
- POJ 1654
- poj 1654
- poj 1654
- poj 1654 Area
- POJ 1654 Area
- POJ 1654 Area
- poj 1654 Area
- POJ 1654 Area
- poj 1654Area
- poj 1654 Area
- POJ 1654 多边形面积
- POJ 1654 Area
- POJ 1654 Area
- poj 1654 Area
- uva -213Message Decoding
- UVA10112 Myacm Triangles
- 案例
- 德州头晕失眠怎么办
- 暗影寶庫相傳是幾千年前,魔族定居暗影山脈的第一代統領,以無上
- POJ 1654
- iOS7下隐藏status bar的详细研究
- uva 1494 Qin Shi Huang's National Road System
- google map 开发心得1
- MFC热键设置的方法
- vc汇编(三)80X86寄存器详解
- C中const关键字
- GTK+图形化应用程序开发学习笔记(四)—容器、构件
- 理解Certificate、App Id、Identifiers 和 Provisioning Profile