728x90
11651 좌표 정렬하기2

#include <bits/stdc++.h>
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b)
{
if (a.second == b.second)
return a.first < b.first;
else
return a.second < b.second;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n;
vector<pair<int, int>> n_arr;
cin >> n;
for (int i = 0; i < n; ++i)
{
int x, y;
cin >> x >> y;
n_arr.push_back({x, y});
}
sort(n_arr.begin(), n_arr.end(),compare);
for (auto i : n_arr)
{
cout << i.first <<" " << i.second << '\\n';
}
return 0;
}
- 좌표 정렬하기1과 같은 문제
- y를 기준으로 정렬하는 부분이 달라졌다
- sort(a,b, compare)로 함수를 만들어줘서 해결했다.
728x90
'Study > Baekjoon' 카테고리의 다른 글
[백준/C++] 2587 대표값2 (0) | 2024.02.22 |
---|---|
[백준/C++] 10814 나이순 정렬 (0) | 2024.02.21 |
[백준/C++] 11650 좌표 정렬하기 (0) | 2024.02.21 |
[백준/C++] 1427 소트인사이드 (0) | 2024.02.21 |
[백준/C++] 2839 설탕 배달 (1) | 2024.02.21 |