본문 바로가기
C C++/C C++ 백준

C언어 백준 11651번 좌표 정렬하기 2

by Go! Jake 2022. 6. 26.

문제 풀이

11650번 문제와 다르지 않다. 구조체를 통해 x, y 좌표를 입력 받고 qsort를 통해 퀵정렬한다.

소스코드

#include <stdio.h>

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

int compare(const void* a, const void* b)
{
	coord A = *(coord*)a;
	coord B = *(coord*)b;
	
	if (A.y<B.y)
	{
		return -1;
	}
	else if (A.y==B.y)
	{
		if (A.x<B.x)
		{
			return -1;
		}
		else
		{
			return 1;
		}
	}
	else
	{
		return 1;
	}
	
	return 0;
}

int main()
{
	int n;
	scanf("%d", &n);
	coord p[n];
	
	int i=0;
	
	while (i<n)
	{
		scanf("%d %d", &p[i].x, &p[i].y);
		i++;
	}
	
	qsort(p,n,sizeof(coord),compare);
	i=0;
		
	while (i<n)
	{
		printf("%d %d\n", p[i].x, p[i].y);
		i++;
	}

    return 0;
}

댓글