求大佬看一下,我写的最近对问题,运行不了
#include<iostream>using namespace std;
struct POINT {
double x;
double y;
};
double DISTANCE(POINT a, POINT b);
double CLOSEST(POINT s[], int low, int high);
double FIND_MINIMUM(double d1, double d2, double d3);
int QUICKSORT(struct POINT p[],int begin,int end) ;
int PARTITION(POINT p, int begin, int end);
int main() {
POINT p; //设定点的集合
int n;
cout << "输入点的个数:\n"; //输入点的个数
cin >> n;
cout << "输入点集:(x,y)\n";
for (int i = 0; i < n; i++)
cin >> p.x >> p.y;
double d=CLOSEST(p, 0,n); //对输入的点先排序
cout << "最小距离为:\n" << d; //输出点对的最小问题
return 0;
}
double DISTANCE(POINT a, POINT b) {
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
double FIND_MINIMUM(double d1,double d2) {
if (d1 < d2) {
returnd1;
}
else if (d2 < d1)
{
return d2;
}
return 0;
}
double FIND_MINIMUM(double d1, double d2, double d3){
if ((d1 < d2) && (d1 < d3)) {
returnd1;
}
else if ((d2 < d1) && (d2 < d3))
{
return d2;
}
else if ((d3 < d1) && (d3 < d2)) {
return d3;
}
return 0;
}
double CLOSEST(POINT s[] ,int low , int high ) {
double d1;
double d2;
double d3;
double d;
POINT *p=new POINT();
if (high - low == 1) {
return DISTANCE(s, s);
}
if (high - low == 2) {
d1 = DISTANCE(s,s);
d2 = DISTANCE(s,s);
d3 = DISTANCE(s,s);
return FIND_MINIMUM(d1,d2,d3);
}
int middle = (low + high) / 2;
d1 = CLOSEST(s, low, middle);
d2 = CLOSEST(s, middle + 1, high);
d = FIND_MINIMUM(d1,d2);
int index = 0;
for (int i = middle; (i >= low) && (s.x >= s.x - d); i--) {
p = s;
}
for (int j = middle + 1; (j <= high) && (s).x <= s.x + d; j++) {
p = s;
}
QUICKSORT(p,0,index-1);
for (int i = 0; i < index; i++) {
for (int j = i + 1; j < index; j++) {
if (p.y - p.y >= d) {
break;
}
else {
d3 = DISTANCE(p,p);
if (d3 < d) { d = d3; }
}
}
}
return d;
}
int QUICKSORT(POINT p[], int begin, int end) {
if (begin < end) {
int sign = PARTITION(*p, begin, end);
PARTITION(*p,begin,sign-1);
PARTITION(*p,sign+1,end);
}
return 0;
}
int PARTITION(POINT p[], int begin, int end) {
double sign_element = p.y;
int i = begin - 1;
for (int j = begin; j < end; j++) {
if (p.y <= sign_element) {
i++;
exchange(p,p);
}
}
exchange(p, p);
return i;
} 你别说你是huster 你貌似一开始没有对x进行排序,后面只对y排了序。不对x排序的话分low high middle 就没有意义了(你还注释了对输入点对先排序
页:
[1]