Algorithm/C++
[백준 11650] 좌표 정렬하기
imagineer_jinny
2022. 9. 23. 21:49
11650번: 좌표 정렬하기
첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.
www.acmicpc.net
내 풀이
using namespace std;
#include <iostream>
#include <vector>
#include <algorithm>
bool cmp(pair<int,int>p1, pair<int,int>p2) { // compare 함수
if(p1.first==p2.first)return p1.second<p2.second;
return p1.first<p2.first;
}
int main(void) {
int n;
cin>>n;
vector<pair<int,int>> v;
while(n--)
{
int t1,t2;
cin>>t1>>t2;
v.push_back(make_pair(t1,t2));
}
sort(v.begin(),v.end(),cmp);
for(auto i:v)
{
cout<<i.first<<" "<<i.second<<"\n";
}
}
배운것
오름차순 a<b
내림차순 a>b
정답 코드
// Authored by : std-freejia
// Co-authored by : BaaaaaaaaaaarkingDog
// http://boj.kr/5a5a95bcc0644b138ad6850e5db9e87a
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
int n, a, b;
pair<int, int> p[100004];
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++) {
cin >> p[i].X >> p[i].Y;
}
sort(p, p + n);
for(int i = 0; i < n; i++) cout << p[i].X << ' ' << p[i].Y << '\n';
}
참고