AOJ (C++ Programming II)—5-A-sorting pairs
程序员文章站
2022-04-14 11:19:43
5-A-sorting pairs题目Sorting Pairs Write a program which print coordinates (xi,yi) of given n points on the plane by the following criteria. first by x-coordinate in case of a tie, by y-coordinate输入A sequence is given in the following format.nx0 y0x1...
5-A-sorting pairs
题目
Sorting Pairs Write a program which print coordinates (xi,yi) of given n points on the plane by the following criteria. first by x-coordinate in case of a tie, by y-coordinate
输入
A sequence is given in the following format.
n
x0 y0
x1 y1
:
xn-1 yn-1
In the first line, the number of points n is given. In the following n lines, coordinates of each point are given.
输出
Print coordinate of given points in order.
样例输入
5
4 7
5 5
2 3
6 8
2 1
样例输出
2 1
2 3
4 7
5 5
6 8
题解(代码流程)
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
vector<pair<int, int>> a(n);//pair数组
for (int i = 0; i < n; i++)cin >> a[i].first >> a[i].second;
sort(a.begin(), a.end());//sort排序
for (auto e:a) {
cout << e.first << " " << e.second << endl;//分别输出两个参数
}
return 0;
}
小结
理解pair排序,正确使用sort函数进行排序,及pair内两个参数
sort()
本文地址:https://blog.csdn.net/m0_45695811/article/details/107962315