Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

poj 1654 Area 多边形面积

$
0
0
/*
poj 1654 Area 多边形面积

题目意思很简单,但是1000000的point开不了
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
const int N=1000000+10;
const double eps=1e-8;
struct point 
{
	double x,y;
	point(){}
	point(double a,double b):x(a),y(b){}
};
int len,index;
char t_s[N];
//基础函数/////////////////////////
double mo_distance(point p1,point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double mo_xmult(point p2,point p0,point p1)//p1在p2左返回负,在右边返回正
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

bool mo_ee(double x,double y)
{
	double ret=x-y;
	if(ret<0) ret=-ret;
	if(ret<eps) return 1;
	return 0;
}
bool mo_gg(double x,double y)  {   return x > y + eps;} // x > y   
bool mo_ll(double x,double y)  {   return x < y - eps;} // x < y   
bool mo_ge(double x,double y) {   return x > y - eps;} // x >= y   
bool mo_le(double x,double y) {   return x < y + eps;}     // x <= y   



//求多边形面积
double area_polygon(point shang,point zhe)
{
	point yuan;
	yuan.x=yuan.y=0;
	return mo_xmult(zhe,yuan,shang)/2;
}



///////////////////
int getpoint(point shang,point &zhe)
{
	if(t_s[index]=='5')
		return 0;;
	char doo=t_s[index];
	int n=1;
	while(t_s[index+1]==t_s[index])
	{
		++n;
		++index;
	}
	++index;
	if(doo=='1')
	{
		zhe.x=shang.x-n;
		zhe.y=shang.y-n;
		
	}else if(doo=='2')
	{
		zhe.x=shang.x;
		zhe.y=shang.y-n;
		
	}else if(doo=='3')
	{
		zhe.x=shang.x+n;
		zhe.y=shang.y-n;
		
	}else if(doo=='4')
	{
		zhe.x=shang.x-n;
		zhe.y=shang.y;
		
	}else if(doo=='9')
	{
		zhe.x=shang.x+n;
		zhe.y=shang.y+n;
		
	}else if(doo=='6')
	{
		zhe.x=shang.x+n;
		zhe.y=shang.y;
		
	}else if(doo=='7')
	{
		zhe.x=shang.x-n;
		zhe.y=shang.y+n;
		
	}else if(doo=='8')
	{
		zhe.x=shang.x;
		zhe.y=shang.y+n;
		
	}
	return 1;
}
int main()
{
	int t;
	scanf("%d",&t);
	getchar();
	while(t--)
	{
		gets(t_s);
		len=strlen(t_s);
		index=0;
		point shang(0,0),zhe;
		double area=0;
		while(getpoint(shang,zhe))
		{
			area+=area_polygon(shang,zhe);
			shang=zhe;
		}
		if(area<0)
			area=-area;
		__int64 ret=(__int64)area,i;
		for(i=ret;i<=ret+2;++i)
		{
			if(mo_ee((double)i,area))
			{
				printf("%I64d\n",i);
				break;
			}else if(mo_ee(0.5+i,area))
			{
				printf("%I64d.5\n",i);
			}
		}
	}
	return 0;
}

作者:qq172108805 发表于2013-8-2 17:27:01 原文链接
阅读:0 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>