728x90
10814번: 나이순 정렬
온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을
www.acmicpc.net
1) 주석 없는 VERSION
#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool com(pair<int,string> a,pair<int,string> b) {
return a.first < b.first;
}
int main() {
int n;
cin >> n;
pair<int, string>p;
vector<pair<int, string>>v;
for (int i = 0;i < n;i++) {
cin >> p.first >> p.second;
v.push_back(p);
}
stable_sort(v.begin(), v.end(), com);
for (int i = 0;i < n;i++) {
cout << v[i].first << " " << v[i].second << "\n";
}
}
2) 주석 있는 VERSION
#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// 나이가 적은 수부터 차례로 정렬하는 함수
bool com(pair<int,string> a,pair<int,string> b) {
return a.first < b.first;
}
int main() {
int n;
cin >> n;
pair<int, string>p;
vector<pair<int, string>>v;
// 나이, 이름 순으로 저장
for (int i = 0;i < n;i++) {
cin >> p.first >> p.second;
v.push_back(p);
}
// 안정정렬
stable_sort(v.begin(), v.end(), com);
//출력
for (int i = 0;i < n;i++) {
cout << v[i].first << " " << v[i].second << "\n";
}
}
'BEAKJOON' 카테고리의 다른 글
백준(15650번 N과 M (2))풀이 C++ (0) | 2021.01.18 |
---|---|
백준(15649번 N과 M (1))풀이 C++ (0) | 2021.01.18 |
백준(1181번 단어 정렬)풀이 C++ (0) | 2021.01.18 |
백준(11651번 좌표 정렬하기 2)풀이 C++ (0) | 2021.01.18 |
백준(11650번 좌표 정렬하기)풀이 C++ (0) | 2021.01.18 |