티스토리 뷰

728x90
반응형

BOJ 11650번 좌표 정렬하기와 동일한 문제였습니다. 단지, 좌표를 정렬할 때 먼저 y좌표를 기준으로 오름차순으로 정렬한 후 y좌표가 동일할 때는 x좌표를 기준으로 오름차순 정렬하는 것으로 변경되었습니다.

BOJ 11650번에 대해 작성한 글의 링크입니다.

https://ark-hive.tistory.com/9

 

BOJ 11650번 좌표 정렬하기

구조체를 활용하여 좌표를 저장하였습니다. 저장된 좌표를 c언어의 라이브러리에 내장된 퀵 정렬을 사용하여 좌표를 정렬하였습니다. 이때 문제에서 요구하는 순서로 좌표를 출력하기 위해 comp

ark-hive.tistory.com

#include<stdio.h>
#include<stdlib.h>

int N;

typedef struct point {
	int x;
	int y;
} POINT;

POINT points[100020];

int compare(const void* first, const void* second) {
	if (((POINT*)first)->y < ((POINT*)second)->y) {
		return -1;
	}
	else if (((POINT*)first)->y == ((POINT*)second)->y) {
		if (((POINT*)first)->x < ((POINT*)second)->x) return -1;
		if (((POINT*)first)->x == ((POINT*)second)->x) return 0;
		if (((POINT*)first)->x > ((POINT*)second)->x) return 1;
	}
	else
		return 1;
}

int main(void) {

	scanf_s("%d", &N);

	for (int i = 0; i < N; i++) {
		scanf_s("%d %d", &points[i].x, &points[i].y);
	}

	qsort(points, N, sizeof(POINT), compare);

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

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

728x90
반응형
반응형
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함