[백준] 11651 좌표 정렬하기 2

0

백준

목록 보기
149/271
post-thumbnail
post-custom-banner

[백준] 11651 좌표 정렬하기 2

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

bool cmp(pair<int, int> a, pair<int, int> b) {
	if (a.second < b.second) return true;
	else if (a.second == b.second) return a.first < b.first;
	return false;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	
	int n;
	cin >> n;

	vector<pair<int, int>> vec;
	for (int i = 0; i < n; ++i) {
		int x, y;
		cin >> x >> y;
		vec.push_back({ x,y });
	}

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

	for (int i = 0; i < n; ++i) {
		cout << vec[i].first << " " << vec[i].second << "\n";
	}
	
	return 0;
}

1년 전 풀이

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<pair<int, int>> vec;

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

	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		int x, y;
		cin >> x >> y;
		vec.push_back(make_pair(y, x));
	}

	sort(vec.begin(), vec.end());

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

	return 0;
}

profile
Be able to be vulnerable, in search of truth
post-custom-banner

0개의 댓글