1.题目详情
本题难度不大,主要是在计算距离和判断距离远近的时候有些小细节需要注意,在计算距离时最好用平方直接进行大小比较,避免开根号产生浮点数影响后续结果,然后本题我是定义了一个结构体(distance和id),最后用sort函数进行大小比较,所以这里需要自己定义排列顺序,即cmp,最后输出前三个就行。
3.代码实现#include #include using namespace std; struct node{ int distance; //距离 int id; //检测点编号 }; bool cmp(node n1,node n2) //自定义sort顺序 { if(n1.distance==n2.distance) //距离一样编号小的排在前面 return n1.id < n2.id; return n1.distance < n2.distance; } int main() { int n,x,y; cin>>n>>x>>y; node point[n]; for(int i=0;i<n;i++) { int point_x,point_y; cin>>point_x>>point_y; point[i].distance = (point_x-x)*(point_x-x) + (point_y-y)*(point_y-y); point[i].id = i+1; } sort(point,point+n,cmp); for(int i=0;i<3;i++) cout<<point[i].id<<endl; return 0; }
更多CCFCSP认证真题详解,请点击>>CCFCSP历年认证考试真题解答汇总