top1编程
← 返回题目
题解

【入门】坐标排序

1 条题解

  • 0
    @ 2026-7-28 23:24:24
    #include <bits/stdc++.h>
    using namespace std;
    struct zb{
    	int x;
    	int y;
    }a[10010];
    bool cmp(zb a,zb b){
        //x先从小到大排序,x一样y从小到大排序
        if(a.x<b.x||a.x==b.x&&a.y<b.y){
            return true;
        }else{
            return false;
        }
    }
    int main(){
    	int n;
    	cin>>n;
    	for(int i=1;i<=n;i++){
    		cin>>a[i].x>>a[i].y;
    	}
    	sort(a+1,a+n+1,cmp);//辅助排序函数
    	for(int i=1;i<=n;i++){
    		cout<<a[i].x<<" "<<a[i].y<<endl;
    	}
    	return 0;
    }
    
    • 1