poj1654-area 计算多边形的面积
来源:互联网 发布:阿里云域名证书 编辑:程序博客网 时间:2024/05/16 14:19
Area
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 20190 Accepted: 5501
Description
You are going to compute the area of a special kind of polygon. One vertex of the polygon is the origin of the orthogonal coordinate system. From this vertex, you may go step by step to the following vertexes of the polygon until back to the initial vertex. For each step you may go North, West, South or East with step length of 1 unit, or go Northwest, Northeast, Southwest or Southeast with step length of square root of 2.
For example, this is a legal polygon to be computed and its area is 2.5:
For example, this is a legal polygon to be computed and its area is 2.5:
Input
The first line of input is an integer t (1 <= t <= 20), the number of the test polygons. Each of the following lines contains a string composed of digits 1-9 describing how the polygon is formed by walking from the origin. Here 8, 2, 6 and 4 represent North, South, East and West, while 9, 7, 3 and 1 denote Northeast, Northwest, Southeast and Southwest respectively. Number 5 only appears at the end of the sequence indicating the stop of walking. You may assume that the input polygon is valid which means that the endpoint is always the start point and the sides of the polygon are not cross to each other.Each line may contain up to 1000000 digits.
Output
For each polygon, print its area on a single line.
Sample Input
4582567256244865
Sample Output
000.52
注:题目里的长度有1000000个,所以这里我们用字符串进行处理,而且考虑到时间复杂度和空间复杂度我们这里使用叉积的方法来求得面积。
注意答案要使用long long 来存储。
ac代码如下:
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>using namespace std;char s[1000005];int tx[10]={0,-1,0,1,-1,0,1,-1,0,1};int ty[10]={0,-1,-1,-1,0,0,0,1,1,1};long long sum,x,y,px,py;int main(){int t,i,l;scanf("%d",&t);getchar();while(t--) {gets(s);l=strlen(s);if(l<3||s[0]=='5') sum=0;else{ sum = 0;x = y = 0;for(i=0; i<l; i++){if(s[i]=='5') break;px = x+tx[s[i]-'0'];py = y+ty[s[i]-'0'];sum += (px*y-py*x);x = px;y = py;}if(sum<0) sum = -sum;} if(sum%2==0) printf("%lld\n",sum/2);elseprintf("%lld.5\n",sum/2);}return 0;}题目链接:点击打开链接http://poj.org/problem?id=1654
阅读全文
0 0
- poj1654-area 计算多边形的面积
- Area(poj1654多边形面积)
- POJ1654-Area(多边形面积)
- poj1654—Area(计算几何求多边形面积)
- POJ1654 Area(多边形面积,叉积求三角形面积)
- POJ1654叉积求多边形面积
- poj1654叉积求多边形面积
- poj1654叉积求多边形面积
- poj1654 Area 计算几何
- poj1654--Area(几何求面积)
- poj1654(求多边形面积)
- POJ 1654 Area (计算多边形面积)
- POJ1645-Area(简单多边形面积计算)
- poj1654 Area (计算几何)
- [POJ1654]Area(计算几何)
- 计算多边形的面积
- 计算多边形的面积
- 计算多边形的面积
- xcode5 arm64
- (译)Objective-C的动态特性
- 移动学习 AndroidStudio内存优化分析—hprof文件分析
- Objective C运行时(runtime)技术的几个要点总结
- Associated Objects
- poj1654-area 计算多边形的面积
- centos7安装docker
- javascript模拟实现多次使用window.onload事件处理函数
- Method Swizzling
- MSVC/MinGW中导入glog静态库的正确方式
- ios7 JavaScriptCore.framework
- Linux中的socket通信——TCP
- OC 调用javaScript建议详情页面
- Objective-C 的“多继承”