# 생각
stable_sort를 이용하여 나이만 비교할 수 있다
람다를 이용하여 간결하게 작성할 수 있고, 람다를 사용하지 않는다면
compare함수를 만들어 stable_sort에 쓰면 된다
# 전체 코드
#include <bits/stdc++.h>
using namespace std;
// 람다식을 이용한 풀이
void func1()
{
int n;
cin >> n;
vector<pair<int, string>> v(n);
for (auto& i : v)
{
cin >> i.first >> i.second;
}
stable_sort(v.begin(), v.end(),
[&](pair<int, string> a, pair<int, string> b) {return a.first < b.first; });
for (auto& i : v)
{
cout << i.first << ' ' << i.second << '\n';
}
}
// compare 메서드를 이용한 풀이
bool compare(const pair<int, string>& lhs, const pair<int, string>& rhs)
{
return lhs.first < rhs.first;
}
void func2()
{
int n;
cin >> n;
vector<pair<int, string>> v(n);
for (auto& i : v)
{
cin >> i.first >> i.second;
}
stable_sort(v.begin(), v.end(), compare);
for (auto i : v)
{
cout << i.first << ' ' << i.second << '\n';
}
}
// tuple을 사용한 풀이
void func3()
{
int n;
cin >> n;
vector<tuple<int, int, string>> v;
for (int i = 0; i < n; i++)
{
int age;
string name;
cin >> age >> name;
v.push_back({ age,i,name });
}
sort(v.begin(), v.end());
for (auto [age, b, name] : v) // c++ 17이상 지원
{
cout << age << " " << name << '\n';
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
//func1();
//func2();
func3();
}
https://www.acmicpc.net/problem/10814
10814번: 나이순 정렬
온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을
www.acmicpc.net
'Algorithm > BOJ' 카테고리의 다른 글
[BOJ] 11651_좌표 정렬하기 2 with cpp (0) | 2022.02.20 |
---|---|
[BOJ] 11650_좌표 정렬하기 with cpp (0) | 2022.02.19 |
[BOJ] 15683_감시 with cpp (0) | 2022.02.17 |
[BOJ] 11328_Strfry with cpp (0) | 2022.02.17 |
[BOJ] 3273_두 수의 합 with cpp (0) | 2022.02.17 |