https://www.acmicpc.net/problem/11651
11651번: 좌표 정렬하기 2
첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.
www.acmicpc.net
구조체와 STL에서 제공하는 sort 함수를 이용해서 해결하자.
#include <cstdio>
#include <algorithm>
using namespace std;
typedef class Point{
public:
int x;
int y;
}Point;
bool comp(Point a, Point b){
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
int main(void){
int N;
scanf("%d", &N);
Point point[N];
for(int i=0; i<N; i++){
scanf("%d %d",&point[i].x, &point[i].y);
}
sort(point, point + N, comp);
for(int i=0; i<N; i++)
printf("%d %d\n",point[i].x, point[i].y);
}
'알고리즘 문제' 카테고리의 다른 글
[C/C++ 백준 1915번] 가장 큰 정사각형 (Gold 5) (0) | 2020.08.22 |
---|---|
[C/C++ 백준 10816번] 숫자 카드 2 (Silver 4) (0) | 2020.08.20 |
[C/C++ 백준 15829번] Hashing (Bronze 2) (0) | 2020.08.20 |
[C/C++ 백준 10814번] 나이순 정렬 (Silver 5) (0) | 2020.08.20 |
[C/C++ 백준 18111번] 마인크래프트 (Silver 3) (0) | 2020.08.20 |