求完整代码,下面是部分算法
int ClosePoints(int x[], int y[], int n)
{
int index1, index2;
int d, minDist = 10000;
for (int i = 0; i<n - 1; i++)
for (int j = i + 1; j <= n - 1; j++)
{
d = (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]);
if (d <= minDist)
{
minDist = d;
index1 = i;
index2 = j;
}
}
cout << "最近的点对是:" << index1 << "和" << index2<< endl;
return minDist;
}
相关分类