poj 1654 Area

题意:总坐标源点出发,给定8个方向走出一个多边形,然后求出多边形的面积。


分析:把多边形分解成一个个三角形,然后求出面积,利用叉积可以快速算出面积。

这题真的好恶心,刚开始用的double保存的坐标,肯定中间精度会有误差,WA了。因为根据题目的条件只会有整点,然后又换成了int,好了坐标是没什么问题了,可是我输出结果用的double,于是用WA了几发,然后换成int WA。。。,最后用long long 终于A了,做完这题好像撞墙~,以后能用整形就一定只用整形,浮点数太可怕了。


以下附上代码:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

struct Point{
  int x,y;
  
  Point(){x = 0; y = 0;}
  Point(int x_, int y_) :
    x(x_), y(y_) {}
    
    
  //Point
  Point operator + (const Point &b) const{
    Point t(x+b.x,y+b.y);
    return t;
  }
  Point operator - (const Point &b) const{
    Point t(x+b.x,y+b.y);
    return t;
  }
  bool operator != (const Point &b) const{
    return x != b.x || y != b.y;
  }
  
  //Vector 
  int operator ^ (const Point &b) const{
    return x*b.y - y*b.x;
  }
};

Point d[10];
string s;

void init()
{
  d[8] = Point(0,1);
  d[2] = Point(0,-1);
  d[6] = Point(1,0);
  d[4] = Point(-1,0);
  
  d[9] = Point(1,1);
  d[7] = Point(-1,1);
  d[3] = Point(1,-1);
  d[1] = Point(-1,-1);
}


int main()
{
  int t;
  init();
  scanf("%d",&t);
  while(t--){
    cin >> s;
    ll res = 0;
    if(s.size() < 4){
      puts("0");
      continue;
    }
    
    Point O(0,0);
    Point P1 = O;
    Point P2 = P1 + d[s[0]-'0'];
    Point v1;
    Point v2;
    for(int i = 1; i < s.size()-1; i++){
      P1 = P2;
      P2 = P2 + d[s[i]-'0'];
      v1 = P1 - O;
      v2 = P2 - O;
      res += (v1^v2);
    }

    if(res < 0) res = -res;
    
    if(res % 2) printf("%lld.5\n",res/2);
    else printf("%lld\n",res/2);
    
  }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值