백준 코딩
백준 11650:좌표 정렬하기
까르르꿍꿍
2021. 9. 25. 19:13
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
입력
첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.
출력
첫째 줄부터 N개의 줄에 점을 정렬한 결과를 출력한다.
이번 문제는 pair함수를 처음으로 사용해봤다. pair은 두가지 자료형을 한번에 다룰수 있기에 좌표에 유용하게 사용이 가능하다.
make_pair(first,second)로 pair값 지정 가능하다
#include<iostream>
#include<vector>
#include<cmath>
#include<string>
#include<algorithm>
#include<set>
#define PI 3.1415926535
using namespace std;
bool compare(pair<int,int> left, pair<int,int> right) //좌표값 비교
{
if (left.first == right.first) { //x값이 같으면
return left.second<right.second; //y값 비교
}
return left.first<right.first; //x값 비교
};
int main()
{
int t;
vector<pair<int,int>> temp;
cin >> t;
while (t--) {
int x, y;
cin >> x>> y;
temp.push_back(make_pair(x, y));
}
sort(temp.begin(), temp.end(), compare); //정렬
int len = temp.size();
for (int i = 0; i < len; i++) {
cout << temp[i].first << ' ' << temp[i].second << '\n';
}
return 0;
}