二維座標排序

Posted on Thu, Nov 18, 2021 題解 進階程設課程
#include <bits/stdc++.h>

using namespace std;

struct point{
    int x;
    int y;
};

bool cmp(point a,point b){
    if(a.x==b.x){
        return a.y<b.y;
    }else
    {
        return a.x<b.x;
    }
}

int main()
{

    int N;
    cin>>N;
    point pos[N];
    for(int i=0;i<N;i++){
        cin>>pos[i].x>>pos[i].y;
    }
    sort(pos,pos+N,cmp);
    for(int i=0;i<N;i++){
        cout<<pos[i].x<<" "<<pos[i].y<<endl;
    }

    return 0;
}