코딩테스트_백준풀이
좌표 정렬하기 #11650 c++ 풀이
wanna_dev
2023. 10. 13. 16:58
https://www.acmicpc.net/problem/11650
11650번: 좌표 정렬하기
첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.
www.acmicpc.net
조건이 2개이상이면, 이제는 자연스럽게 자료구조를 정의하고 comp함수를 넘겨주는 방법으로 sort를 진행하는 것 같다.
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
typedef struct idx {
int x;
int y;
};
bool comp(idx a, idx b) {
if (a.x != b.x)
return a.x < b.x;
else
return a.y < b.y;
}
vector <idx> indices;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
for (int i = 0; i < N; i++) {
idx index;
cin >> index.x >> index.y;
indices.push_back(index);
}
sort(indices.begin(), indices.end(), comp);
for (int i = 0; i < indices.size(); i++) {
cout << indices[i].x << " " << indices[i].y << '\n';
}
}