结构体排序法

找最小数

https://www.nowcoder.com/practice/ba91786c4759403992896d859e87a6cd

#include <iostream>
#include <algorithm>
using namespace std;
struct ab {
    int x;
    int y;
};
bool compear(ab a, ab b) {
    if (a.x == b.x) {
        return a.y < b.y;
    } else {
        return a.x < b.x;
    }
}
int main() {
    int n;
    cin>>n;
    ab* xy = (ab*)malloc(sizeof(ab) * n);
    for (int i = 0; i < n; ++i) {
        cin >> xy[i].x >> xy[i].y;
    }
    sort(xy, xy + n, compear);
    std::cout << xy[0].x << " " << xy[0].y << std::endl;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务