최대 1 분 소요

Question

10814Q


Solution

  • 2중 pair로 입력된 순서와 나이, 이름을 가진 변수를 vector에 저장한다
  • 나이순으로 정렬하되 같으면 입력된 순서로 정렬하는 비교함수 compare를 재정의
  • vector의 요소들을 출력

Cord

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

using namespace std;

vector<pair<int, pair<int, string>>> vPeople;

bool compare(pair<int, pair<int, string>> _a, pair<int, pair<int, string>> _b)
{
	// 나이 순으로 정렬 하되 같으면 입력된 순서로 한다.
	if (_a.second.first == _b.second.first)
	{
		return _a.first < _b.first;
	}
	else
	{
		return _a.second.first < _b.second.first;
	}
}

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

	for (int idx = 0; idx < people; idx++)
	{
		pair<int, pair<int, string>> data;
		cin >> data.second.first >> data.second.second;
		data.first = idx;	// 입력 순서 추가
		vPeople.push_back(data);
	}

	// 나이 오름차순 정렬
	sort(vPeople.begin(), vPeople.end(), compare);

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

	return 0;
}

Result

10814

태그: ,

카테고리:

업데이트: