计算距离检测地点最近的点,距离相同的点序号小的最近
代码如下:
#include <stdio.h>
#include <math.h>
//距离计算
double D(double a,double b,double m,double n)
{
double D;
double Dx2 = pow((a-m),2.0);
double Dy2 = pow((b-n),2.0);
D = sqrt (Dx2 + Dy2);
return (D);
}
int main()
{
int n,x,y;
//a[i][0]为X,a[i][1]为Y,a[i][2]为该检测点与市民的距离,a[i][3]为检测点的编号
double a[201][4];
double t;
int i,j;
scanf("%d %d %d",&n,&x,&y);
for(i=0;i<n;i++)
{
scanf("%lf %lf",&a[i][0],&a[i][1]);
a[i][2] = D(a[i][0], a[i][1], x, y);//两点之间的距离
a[i][3] = i+1;//监测点的编号
}
//将距离和相应的检测点编号进行冒泡排列
for(i = 0; i < n; i++)
{
for (j = 0; j < n - i -1; j++)
{
if (a[j][2] > a[j+1][2])
{
t = a[j][2];
a[j][2] = a[j+1][2];
a[j+1][2] = t;
t = a[j][3];
a[j][3] = a[j+1][3];
a[j+1][3] = t;
}
}
}
//输出前三的序号
for (i = 0; i < 3; i++)
{
printf("%d\n", (int)a[i][3]);
}
return 0;
}