題目描述
設p1=(x1, y1), p2=(x2, y2), …, pn=(xn, yn)是平面上n個點構成的集合S,設計算法找出集合S中距離最近的點對。
輸入
多組測試數據,第一行爲測試數據組數n(0<n≤100),每組測試數據由兩個部分構成,第一部分爲一個點的個數m(0<m≤1000),緊接着是m行,每行爲一個點的座標x和y,用空格隔開,(0<x,y≤100000)
輸出
每組測試數據輸出一行,爲該組數據最近點的距離,保留4爲小數。
樣例輸入
2
2
0 0
0 1
3
0 0
1 1
1 0
樣例輸出
1.0000
1.0000
代碼
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
typedef struct{
int x,y;
}Coor;
Coor h[10000];
int main(){
int n;
cin>>n;
while(n--){
double ans=99999.9;
int m;
cin>>m;
for(int j = 0; j < m ; j++){
cin>>h[j].x >>h[j].y;
}
for(int i =0 ; i< m-1; i++){
for( int j = 1+i; j< m; j++){
ans= min(ans,sqrt(pow(h[i].x-h[j].x ,2)+pow(h[i].y - h[j].y ,2 )));
}
}
printf("%.4lf\n",ans);
}
return 0;
}
思路
略。