본문 바로가기

알고리즘 문제

[C/C++ 백준 11651번] 좌표 정렬하기 2 (Silver 5)

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);
}