11650번: 좌표 정렬하기
사용 언어: C++
풀이
1) struct Point안에서 연산자 오버로딩을 통해 비교 방법을 만들거나,
2) bool Compare 함수를 만들어서 포인트들을 비교할 수 있는 방법을 만든다.
아래는 오버로딩을 할때 쓸 코드
bool operator < (const Point &p) const {
if(x != p.x) return x < p.x;
else return y < p.y;
}
아래는 2)로 푼 코드
#include <bits/stdc++.h>
using namespace std;
struct Point{
int x, y;
};
bool Compare(const Point &a, const Point &b)
{
if(a.x != b.x)
{
return a.x < b.x;
}
else
{
return a.y < b.y;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector<Point> V(N);
for(int i=0; i<N; i++)
{
cin >> V[i].x;
cin >> V[i].y;
}
sort(V.begin(), V.end(), Compare);
for(const Point& i : V)
{
cout << i.x << " " << i.y << "\n";
}
}
'BOJ' 카테고리의 다른 글
1654번: 랜선 자르기 (0) | 2024.09.27 |
---|---|
2110번: 공유기 설치 (0) | 2024.09.27 |
18870번: 좌표 압축 (0) | 2024.09.26 |
10816번: 숫자 카드 2 (0) | 2022.02.04 |
1929번: 소수 구하기 (0) | 2022.01.27 |