提交时间:2024-03-02 21:14:05
运行 ID: 134828
#include <iostream> #include <vector> #include <algorithm> // 定义一个结构体表示一对整数 struct Pair { int first; int second; }; // 自定义比较函数,按照题目要求进行排序 bool comparePairs(const Pair& p1, const Pair& p2) { if (p1.first == p2.first) { return p1.second < p2.second; } return p1.first < p2.first; } int main() { int n; std::cin >> n; std::vector<Pair> pairs(n); for (int i = 0; i < n; i++) { std::cin >> pairs[i].first >> pairs[i].second; } // 使用自定义的比较函数进行排序 std::sort(pairs.begin(), pairs.end(), comparePairs); // 输出排序后的结果 for (const Pair& p : pairs) { std::cout << p.first << " " << p.second << std::endl; } return 0; }