题目不难,以妾身的看法,首先计算各个检测点与人的距离,再排序。前面三个自然就是距离最近的检测点咯!下面来看妾身的代码。
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct node
{
int des;//距离
int num;//序号
}n[201];
//以距离从小到大排序
bool cmp(node n1, node n2)
{
if (n1.des != n2.des)
{
return n1.des < n2.des;
}
else if (n1.num != n2.num)
{
return n1.num < n2.num;
}
}
int main()
{
int m, x, y;
cin >> m >> x >> y;
int i;
for (i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
n[i].des = pow((x - a), 2) + pow((y - b), 2);//既然是比较距离,那么不开方也没关系
n[i].num = i + 1;
}
sort(n, n + m, cmp);
for (i = 0; i < 3; i++)
{
cout << n[i].num << endl;
}
system("pause");
return 0;
}
这里使用了sort()函数来排序,如果不知道怎么使用的话,可以看看小女子写的这一篇关于sort()介绍的博客。
妾身会继续加油的哟!