即扔圈套物,求所有点中距离最近的两点,这两点距离为环的直径,一般用二分法。下面是比较神奇的方法,不是二分
与二分相同的是都要按x和y排序解法二与二分思路:http://blog.csdn.net/hellobabygogo3/article/details/8042650
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
double x,y;
}a[100005];
int cmpxy( const void *a , const void *b )
{
struct node *c = (struct node *)a;
struct node *d = (struct node *)b;
if( fabs(c->x-d->x)>1e-8 ) return c->x > d->x ? 1 : -1;//比较x相同用fabs()
else return c->y > d->y ? 1 : -1;
}
double min2(double x,double y)
{
return x>y?y:x;
}
double dis(struct node a,struct node b) //参数类型坑死人,struct不能省
{
return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
}
double f( struct node a[],int n)
{
int i;
double d = 1e+10,td;
if(n == 2) return dis(a[0],a[1]);
for(i=0;i<n-2;i++)
{
td = min2( dis(a[i],a[i+1]) , dis(a[i],a[i+2]) );
td = min2( td , dis(a[i+1],a[i+2]) );
d = min2( td,d);
}
return d;
}
int main()
{
int i,n;
// freopen("a.txt","r",stdin);
while( scanf("%d",&n)!=EOF && n )
{
for(i=0;i<n;i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
qsort(a,n,sizeof(a[0]),cmpxy);
printf("%.2lf\n",f(a,n)/2);
}
return 0;
}