/* Copyright by ZhongMing-Bian Jan,6,2010 */
/* 最小套圈问题双重循环算法 低效率 遍历 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_POINTS 100000 /*坐标点数的最大值 */
#define MAX_TEST 100 /*最大测试次数*/
typedef struct { /*定义坐标点*/
float x;
float y;
} Point;
float dist(Point a,Point b) { /*求两个坐标点之间的距离*/
return sqrt((float)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
float nearest(Point* points, int n) { /*求最小距离,双循环遍历*/
float near=100000,temp;
int i,j;
if(n==1) return 0;
else{
for(i=0; i<n; i++)
for(j=0; j<n; j++){
if(i==j) continue;
else {
temp=dist(points[i],points[j]);
near=(near>temp)?temp:near;
}
}
return near;
}
}
void main() {
int i,j=0,numPoints;
Point points[MAX_POINTS];
float result[MAX_TEST]; /*记录结果*/
for(i=0;i<MAX_TEST;i++) result[i]=-1;
printf("请输入数据:/n");
while(1){ /*进行多次测试*/
loop: scanf("%d", &numPoints);
if(!numPoints) break;
if(numPoints<0||numPoints>MAX_POINTS) {
printf("Error!/n"); /*容错处理*/
goto loop;
}
for(i=0; i<numPoints; i++) /*输入点的数量及点的坐标值*/
scanf("%f %f", &points[i].x, &points[i].y);
result[j]=nearest(points, numPoints)/2;
j++;
}
i=0;
printf("/n最短距离分别为:/n");
while(result[i]!=-1) /*输出测试结果*/
printf("%.2f/n",result[i++]);
}