ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

称检测点查询 ccf

2020-12-13 09:31:04  阅读:451  来源: 互联网

标签:检测点 int x1 查询 ++ second y1 include ccf


题目:

在这里插入图片描述

3 2 2
2 2
2 3
2 4

样例输出

1
2
3

此题利用vector,或者结构体来排序比较好,主要考察的地方就是排序,1、根据距离排序,2、距离相同根据编号排序

代码如下

//结构体数组版本
#include <iostream>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;

struct Point
{
	int id;
	int distance;
};

bool cmp(Point& a, Point& b)
{
	if (a.distance == b.distance)
		return a.id < b.id;
	return a.distance < b.distance;
}
int main()
{
	int n, x, y;
	cin >> n >> x >> y;
	int x1, y1;
	Point p[205];
	for (int i = 0; i < n; i++)
	{
		cin >> x1 >> y1;
		p[i].id = i + 1;
		p[i].distance = pow(x1 - x, 2) + pow(y1 - y, 2);
	}
	sort(p, p + n, cmp);

	for (int i = 0; i < 3; i++)
	{
		cout << p[i].id << endl;
	}

	return 0;
}

另一个版本,这里可以直接传入vector数组,不经过map

当时想看看make_pair的使用方法,所以饶了一下

#include <iostream>
#include <math.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

typedef pair<int, int> PAIR;

bool cmp(const PAIR &a, const PAIR &b)
{
    //距离相同时,比较编号大小
    if (a.second == b.second)
    {
        return a.first < b.first;
    }
    //距离不同时,比较距离大小
    return a.second < b.second;
}

int main()
{
    int n, x, y;
    cin >> n >> x >> y;

    int x1, y1;
    map<int, int> dis;

    for (int i = 0; i < n; i++)
    {
        cin >> x1 >> y1;
        dis.insert(make_pair(i + 1, pow((x - x1), 2) + pow((y - y1), 2)));
    }

    vector<PAIR> vec;
    for (map<int, int>::iterator iter = dis.begin(); iter != dis.end(); iter++)
    {
        vec.push_back(make_pair(iter->first, iter->second));
    }

    sort(vec.begin(), vec.end(), cmp);
    for (int i = 0; i < 3; i++)
    {
        cout << vec[i].first << endl;
    }

    return 0;
}

整理后的版本

#include <iostream>
#include<vector>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;

typedef pair<int, int> PAIR;

bool cmp(const PAIR& a, const PAIR& b)
{
    //距离相同时,比较编号大小
    if (a.second == b.second)
    {
        return a.first < b.first;
    }
    //距离不同时,比较距离大小
    return a.second < b.second;
}

int main()
{
    int n, x, y;
    cin >> n >> x >> y;
    vector<PAIR> p;

    int x1, y1;
    for (int i = 0; i < n; i++)
    {
        cin >> x1 >> y1;
        p.push_back(make_pair(i + 1, pow((x - x1), 2) + pow((y - y1), 2)));
    }

    sort(p.begin(), p.end(), cmp);
    for (int i = 0; i < 3; i++)
    {
        cout << p[i].first << endl;
    }

    return 0;
}

标签:检测点,int,x1,查询,++,second,y1,include,ccf
来源: https://blog.csdn.net/qq_41399256/article/details/111088349

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有