博客原文地址:http://blog.youkuaiyun.com/xuechelingxiao/article/details/40618519
题目大意:
给你一个数字组成的字符串,每一位都有1-9(除了5)8个数字,表示8个方向的移动,字符串以5结尾。问最后轨迹形成的多边形的面积是多少。
解题思路:
其实这个题本身并不是很难,关键是细节的处理比较蛋疼。。。首先100w的数组起初我用double开的时候MLE
了,然后看了一下,确实没必要用double,就改成int了。然后就是输出格式,刚开始我以为是SPJ,就直接用double写的,
WA了发现竟然不是SPJ,那怎么保留。。。原来是如果有小数就输出小数,没有就不输出,加个 %2 判断就好了。还有就是多边形面积可能会超int,用long long就可以了。
其他坑点应该就没什么了-。-
其他的详见代码吧:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <algorithm>
#define LL long long
//#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define max3(a, b, c) (a>b?max(a, c):max(b, c))
#define min3(a, b, c) (a<b?min(a, c):min(b, c))
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define Read() freopen("data.in", "r", stdin);
#define Write() freopen("data.out", "w", stdout);
using namespace std;
int dx[10] = {0, -1, 0, 1, -1, 0, 1, -1, 0, 1};
int dy[10] = {0, -1, -1, -1, 0, 0, 0, 1, 1, 1};
struct Point {
int x, y;
} P[1000010];
int xmult(Point a, Point b, Point c) {
return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
char s[1000010];
int main()
{
int T;
scanf("%d", &T);
while(T--) {
scanf("%s", s);
int len = strlen(s);
P[0].x = 0;
P[0].y = 0;
for(int i = 0; i < len-1; ++i) {
P[i+1].x = P[i].x+dx[s[i]-'0'];
P[i+1].y = P[i].y+dy[s[i]-'0'];
}
LL area = 0;
Point o = {0, 0};
for(int i = 1; i < len-1; ++i) {
area += xmult(P[i], P[i+1], o);
}
area = area > 0 ? area : -area;
if(area % 2 == 0)
printf("%lld\n", area/2);
else
printf("%lld.5\n", area/2);
}
return 0;
}
/*test case*/
/*
*/