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 n>>x>>y;
node point[n];
for(int i=0;i>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
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?