최대 1 분 소요

Question

11651Q

11650 과 비슷한 문제이고 정렬기준이 x좌표에서 y좌표로 바뀌었을 뿐이다.


Solution

  • pair를 사용하여 x,y 좌표를 한번에 담아 vector에 저장한다
  • 비교 함수 compare를 만들어 y죄표가 같으면 x좌표를 비교한다
  • vector를 정렬 후 출력

Cord

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<pair<int, int>> vCoord;

bool compare(pair<int, int> _a, pair<int, int> _b)
{
	// y좌표가 같으면 x좌표를 비교
	if (_a.second == _b.second)
	{
		return _a.first < _b.first;
	}
	else
	{
		return _a.second < _b.second;
	}
}

int main()
{
	// 점의 수 입력
	int nums;
	cin >> nums;

	for (int idx = 0; idx < nums; idx++)
	{
		pair<int, int> coord;
		cin >> coord.first >> coord.second;
		vCoord.push_back(coord);
	}

	// 내림차순 정렬
	sort(vCoord.begin(), vCoord.end(), compare);

	// 출력
	for (int idx = 0; idx < vCoord.size(); idx++)
	{
		cout << vCoord[idx].first << ' ' << vCoord[idx].second << '\n';
	}

	return 0;
}

Result

11651

태그: ,

카테고리:

업데이트: