| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1930 人关注过本帖
标题:请问这个sort函数出了什么问题
取消只看楼主 加入收藏
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
结帖率:96.88%
收藏
已结贴  问题点数:20 回复次数:1 
请问这个sort函数出了什么问题
(Top-k points) We have a list of points on the plane. Please find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance. For point [x,y], the distance is Sqrt(x^2+y^2).
(若存在多个点与原点距离相等,则按照第1象限Quadrant,第2象限,第3象限,第4象限来取。)
( 同一象限不存在距离相等的点)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
【输入】第一行K值,K为正整数,K小于1000;
第二行,n个点的坐标值(x,y)。
【输出】K个离原点最近的坐标点信息。(若存在多个点与原点距离相等,则按照第1象限Quadrant,第2象限,第3象限,第4象限来取。)
例如:
【输入】
2
3 3 5 -1 -2 4//坐标点(3,3),(5,-1),(-2,4),
【输出】
3 3 -2 4//坐标点(3,3),(-2,4)按照由近及远来排序

#include<string.h>
#include <algorithm>
#include<cmath>
using namespace std;
struct zuobiao{
int heng;
int shu;};
int cmp( zuobiao a, zuobiao b )
{return (a.heng*a.heng+a.shu*a.shu)<(b.heng*b.heng+b.shu*b.shu);
}
int main()
{
    struct zuobiao a[1000];
    int n;
    cin>>n;
    int x,y,i=0;
    while(cin>>x>>y)
    {
        a[i].heng=x;
        a[i++].shu=y;
    }
    sort(a,a+i-1,cmp);
    for(int j=0;j<n;j++)
        cout<<a[j].heng<<' '<<a[j].shu<<' ';
}
//我还没想到怎么按一二三四区间排序,就先写了一个计算长度的cmp,但是永远得不出结果QAQ
搜索更多相关主题的帖子: the Sqrt sort 坐标 int 
2020-05-13 17:08
komorebi0110
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:145
专家分:17
注 册:2019-11-23
收藏
得分:0 
非常感谢!我连sort函数怎么用都不知道就开始瞎写

[此贴子已经被作者于2020-5-13 19:43编辑过]


我想要两颗西柚。
2020-05-13 19:36
快速回复:请问这个sort函数出了什么问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012199 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved