文章

52

粉丝

68

获赞

22

访问

11.6k

头像
找最小数 题解:自定义比大小,利用cpp机制
P1374 北京邮电大学机试题
发布于2025年2月3日 15:23
阅读数 26

#include <bits/stdc++.h>
using namespace std;

struct newInt{
    int x,y;
    newInt():x(0),y(0){}
    newInt(int x,int y):x(x),y(y){}
    bool operator<(newInt b){
        if(x==b.x)return y<b.y;
        else return x<b.x;
    }
};

int main(){
    int n;
    while(cin>>n){
        std::vector<newInt> a(n);
        for(int i=0;i<n;i++){
            int x,y;cin>>x>>y;
            a[i]=newInt(x,y);
        }
        sort(a.begin(),a.end());
        cout<<a[0].x<<' '<<a[0].y<<endl;
    }
}

sort函数会根据小于号的规则进行比较,这样我们只需要在结构体内进行小于号的重定义即可达到和外部写compare函数的同样效果

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发