문제
풀이
좌표 (x, y)를 pair<int, int> 형으로 받아 x좌표가 같으면 y좌표를 비교하고 아니라면 x좌표를 비교하는 비교자를 만들어 정렬하였다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
int main() {
int n;
vector<pair<int, int>> v;
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
v.push_back(make_pair(x, y));
}
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); i++)
cout << v[i].first << " " << v[i].second << "\n";
return 0;
}
'백준 > 정렬' 카테고리의 다른 글
10814번: 나이순 정렬 [C++] (0) | 2023.03.27 |
---|---|
11651번 좌표 정렬하기 2 [C++] (0) | 2023.03.27 |
1427번: 소트인사이드 [C++] (0) | 2023.03.27 |
2108번: 통계학 [C++] (0) | 2023.03.27 |
10989번: 수 정렬하기 3 [C++] (0) | 2023.03.27 |