链接:http://poj.org/problem?id=1654
Area
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14952 | Accepted: 4189 |
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
4 5 825 6725 6244865
Sample Output
0 0 0.5 2
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
又看了题解才A掉,记得自己曾对别人说WA不要马上看题解,一道题做两三天很正常,自己却MLE马上看题解
自己定的规则自己都不遵守

1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6 #include <math.h> 7 8 #define MAXX 1000002 9 #define eps 1e-6 10 typedef struct 11 { 12 double x; 13 double y; 14 } point; 15 16 double crossProduct(point a,point b,point c) 17 { 18 return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x); 19 } 20 21 bool dy(double x,double y) 22 { 23 return x>y+eps; 24 } 25 bool xy(double x,double y) 26 { 27 return x<y-eps; 28 } 29 bool dd(double x,double y) 30 { 31 return fabs(x-y)<eps; 32 } 33 34 int main() 35 { 36 int n,m,i,j,x,y; 37 scanf("%d",&n); 38 char str[MAXX]; 39 int move[10][2]= {{0,0},{-1,-1},{0,-1},{1,-1},{-1,0},{0,0},{1,0},{-1,1},{0,1},{1,1}}; 40 for(i=0; i<n; i++) 41 { 42 scanf("%s",str); 43 int len=strlen(str); 44 int x1=0,y1=0,x2,y2; 45 long long ans=0; 46 for(j=0; j<len-1; j++) 47 { 48 x2=x1+move[str[j]-'0'][0]; 49 y2=y1+move[str[j]-'0'][1]; 50 ans+=((x1*y2)-(x2*y1)); 51 x1=x2; 52 y1=y2; 53 } 54 ans = ans > 0 ? ans : (-1)*ans; 55 if(ans == 0) 56 printf("0\n"); 57 else if(ans % 2 == 0) 58 printf("%lld\n",ans/2); 59 else if(ans % 2 != 0) 60 printf("%lld.5\n",ans/2); 61 } 62 return 0; 63 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单