POJ 1654 Area

本文提供了一种解决POJ1654 Area问题的有效算法实现,使用C++语言并通过定义point和segment类来处理几何计算。特别强调了在计算过程中使用long long类型变量的重要性,以避免整数溢出导致错误的答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POJ 1654 Area

很水的题。。

然后注意用上longlong,否则可能答案会不对。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;
#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define DOR(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)

#define oo 1e6
#define eps 1e-8
#define nMax 1000010
#define pb push_back

#define F first
#define S second

#define bug puts("OOOOh.....");
#define zero(x) (((x)>0?(x):-(x))<eps)

#define LL long long
#define DB double 


int dcmp(double x){
    if(fabs(x)<eps) return 0;
    return x>0?1:-1;
}
class point {
public:
    double x,y;
    point (double x=0,double y=0):x(x),y(y) {}
    void make(double _x,double _y) {x=_x;y=_y;}
    void read() { scanf("%lf%lf",&x,&y); }
    void out() { printf("%.2lf %.2lf\n",x,y);}
    double len() { return sqrt(x*x+y*y); }
    point friend operator - (point const& u,point const& v) {
        return point(u.x-v.x,u.y-v.y);
    }
    point friend operator + (point const& u,point const& v) {
        return point(u.x+v.x,u.y+v.y);
    }
    double friend operator * (point const& u,point const& v) {
        return u.x*v.y-u.y*v.x;
    }
    double friend operator ^ (point const& u,point const& v) {
        return u.x*v.x+u.y*v.y;
    }
    point friend operator * (point const& u,double const& k) {
        return point(u.x*k,u.y*k);
    }
	friend bool operator < (point const& u,point const& v){
		if(dcmp(v.x-u.x)==0) return dcmp(u.y-v.y)<0;
		return dcmp(u.x-v.x)<0;
	}
	friend bool operator != (point const& u,point const& v){
		return dcmp(u.x-v.x) || dcmp(u.y-v.y);
	}
};
typedef class line{
public:
    point a,b;
    line() {}
    line (point a,point b):a(a),b(b){}
    void make(point u,point v) {a=u;b=v;}
    void read() { a.read(),b.read(); }
}segment;


int dx[]={0,-1,-1,-1,0,0,0,1,1,1};
int dy[]={0,-1,0,1,-1,0,1,-1,0,1};

char s[nMax];
int main(){
	//freopen("input.txt","r",stdin);
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%s",s);
		int l=strlen(s);
		for(int i=0;i<l;i++) s[i]-='0';
		LL ans=0;
		LL x,y;
		if(l>2){
			l--;
			x=dx[s[0]],y=dy[s[0]];
			for(int i=1;i<l;i++){
				ans += x*dy[s[i]]-y*dx[s[i]];
				x+=dx[s[i]];
				y+=dy[s[i]];
			}
		}
		if(ans<0) ans=-ans;
		if(ans&1)printf("%I64d.5\n",ans/2);
		else printf("%I64d\n",ans/2);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值