请问这个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