最近点问题(一)

来源:互联网 发布:三菱编程软件的安装 编辑:程序博客网 时间:2024/05/18 01:39

   最近点问题:就是在一个点集合中找到距离最近的两个点,本文章求的是最近的两个点距离。

   解决最近点问题有两种主要的方法,蛮力法与分治法,这篇文章主要涉及蛮力法。

#include<iostream>using namespace std;#include<string>#include<math.h>#include<iomanip>struct point{int x;int y;};//求两点之间的距离double Dis(point a, point b){return sqrt((a.x - b.x)*(a.x - b.x)*1.0 + (a.y - b.y)*(a.y - b.y)*1.0);}//求两个最近点的距离,效率O(n*n)double ClosePooints(point pt[], int num){double d, max = 10000000;int i, j;for (i = 0; i < num - 1; i++){for (j = i + 1; j < num; j++){d = Dis(pt[i], pt[j]);if (max>d)max = d;}}return max;}int main(){point pt[1000];int num;cin >> num;int i;for (i = 0; i < num; i++){cin >> pt[i].x;cin >> pt[i].y;}double t = ClosePooints(pt, num);cout << setiosflags(ios::fixed) << setprecision(4) << t << endl;return 0;}


0 0
原创粉丝点击