포스트

백준 11651번

백준 단계별로 풀어보기

백준 11651번

백준 2750번 : 좌표 정렬하기 2

https://www.acmicpc.net/problem/11651

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
#include<stdlib.h>

typedef struct {
	int x;
	int y;
} P_t;

int cmp(const void* a, const void* b) {
	int ax = ((P_t*)a)->x;
	int ay = ((P_t*)a)->y;
	int bx = ((P_t*)b)->x;
	int by = ((P_t*)b)->y;

	if (ay == by) return ax - bx;
	else return ay - by;
}
int main() {
	
	int N;
	scanf("%d", &N);

	P_t* p = (P_t*)malloc(sizeof(P_t) * N);
	for (int i = 0; i < N; i++) {
		scanf("%d %d", &p[i].x, &p[i].y);
	}

	qsort(p, N, sizeof(P_t), cmp);

	for (int i = 0; i < N; i++) {
		printf("%d %d\n", p[i].x, p[i].y);
	}
	return 0;
}