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

#문제

백준 11651번 좌표 정렬하기 2

#풀이

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

using namespace std;

vector<pair<int, int>> coordinate;

bool cmp(pair<int, int> x, pair<int, int> y)
{
	if (x.second != y.second)
	{
		return x.second < y.second;
	}
	else
	{
		return x.first < y.first;
	}
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(NULL);

	int N;
	cin >> N;

	for (int i = 0; i < N; ++i)
	{
		int x, y;
		cin >> x >> y;

		coordinate.push_back(make_pair(x, y));
	}

	sort(coordinate.begin(), coordinate.end(), cmp);

	for (int i = 0; i < N; ++i)
	{
		cout << coordinate[i].first << ' ' << coordinate[i].second << '\n';
	}

	return 0;
}

#정리

좌표를 N개 만큼 입력받는다. y의 크기를 먼저 비교하고, x의 크기를 비교하여 출력하는 프로그램을 만든다. sort의 cmp를 이용하여 해결했다.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • [백준] 1010번 다리 놓기 / C++
  • [백준] 2751번 수 정렬하기 2 / C++
  • [백준] 10825번 국영수 / C++
  • [백준] 1181번 단어 정렬 / C++
  • [백준] 11650번 좌표 정렬하기 / C++