一个简单的用叉积求任意多边形面积的题,并不难,但我却错了很多次,double的数据应该是要转化为long long,我转成了int...这里为了节省内存尽量不开数组,直接计算,我MLE了一发...,最后看了下别人的才过,我的代码就不发了,免得误导,不得不说几何真是...
还有就是这个大神的代码,貌似G++,过不了,C++AC
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <iostream> using namespace std; int moveY[20] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; int moveX[20] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; char step[1000007]; #define EPS 1e-8 int main() { int t; scanf("%d ", &t); int x, y, px, py; double area; long pos = 0; while (t--) { x = y = px = py = 0; pos = area = 0; scanf("%s", step); while (step[pos] != '5') { x = px + moveX[step[pos]-'1']; y = py + moveY[step[pos]-'1']; area += 0.5*(px*y-py*x); px = x; py = y; pos++; } area = fabs(area); if (fabs((long long)area - area)<EPS) { printf("%.0lf ", area); } else { printf("%.1lf ", area); } } return 0; }