ZOJ 1453 Surround the Trees

求个凸包,求个周长,太弱太弱。

新手可以从这里学几个控制精度的函数。。。

inline 只不过是函数的#define罢了,不高端。

另外,做叉积的那个函数写成int型的会非常方便。

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define X 1100
#define eps 1e-10
struct point{
	double x,y;
}c[X],stk[X];
int n,top;
inline bool dy(double x,double y){return x>y+eps;}
inline bool xy(double x,double y){return x<y-eps;}
inline bool dyd(double x,double y){return x>y-eps;}
inline bool xyd(double x,double y){return x<y+eps;}
inline bool dd(double x,double y){return fabs(x-y)<eps;}
inline int cp(point a,point b,point c){
	double t=(c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
    if(t<-eps)return -1;
    if(t>eps)return 1;
    return 0;
}
inline double disp2p(point a,point b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline int cmp(point a,point b){
	int len=cp(c[0],a,b);
	if(len==0) return xy(disp2p(c[0],a),disp2p(c[0],b));
	return len<0;
}
void graham(){
	int tmp=0,i;
	for(i=1;i<n;i++)
	    if(xy(c[i].x,c[tmp].x)||dd(c[i].x,c[tmp].x)&&xy(c[i].y,c[tmp].y))
	        tmp=i;
	 swap(c[0],c[tmp]);
	 sort(c+1,c+n,cmp);
	 stk[0]=c[0];stk[1]=c[1];
	 top=1;
	 for(i=2;i<n;i++){
			while(cp(stk[top],stk[top-1],c[i])<=0&&top>=1)
			    top--;
			stk[++top]=c[i];
		}
	stk[++top]=stk[0];
	}
int main(){
	int i;
	double sum;
	while(~scanf("%d",&n)&&n){
		for(i=0;i<n;i++)
		    scanf("%lf%lf",&c[i].x,&c[i].y);
		if(n==1)
		    printf("0.00\n");
		else if(n==2)
		    printf("%.2lf\n",2*disp2p(c[0],c[1]));
		else{
		graham();
		sum=0.0;
		for(i=0;i<top;i++)
		    sum+=disp2p(stk[i],stk[i+1]);
		printf("%.2lf\n",sum);
		}
	}
	return 0;
}


 

你可能感兴趣的:(c,struct,ini)