서두르지 말고 쉬지 말자

백준 10814번 나이순 정렬 (C++) 본문

코딩 공부/Baekjoon Problem Solving

백준 10814번 나이순 정렬 (C++)

philos 2025. 8. 11. 13:29

solved.ac 티어 : 실버 5

문제 유형 : 정렬, 집합과 맵

문제 해결 소감 : 라이브러리가 잘 되어 있어서 상대적으로  적은 노력으로 만들었는데 C로 짠다면 이라는 아찔한 생각이 든다. C로도 짤수 있을 정도로 공부해야 겠다.

문제 해결 전략 : std::pair과 std::stable_sort를 사용해서 만들었다.

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
int n;
int age;
std::string name;
std::vector<std::pair<int, std::string>> pv;
bool comparePeople(const std::pair<int, std::string>& a,
                   const std::pair<int, std::string>& b);
int main(void) {
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> age >> name;
    pv.push_back({age, name});
  }
  std::stable_sort(pv.begin(), pv.end(), comparePeople);
  for (const std::pair<int, std::string>& i : pv) {
    std::cout << i.first << " " << i.second << '\n';
  }
}

bool comparePeople(const std::pair<int, std::string>& a,
                   const std::pair<int, std::string>& b) {
  return a.first < b.first;
}

 

반응형

'코딩 공부 > Baekjoon Problem Solving' 카테고리의 다른 글

백준 1764 듣보잡 (C++)  (0) 2025.08.04
백준 7568번 덩치 (C++)  (2) 2025.08.04
백준 14626번 ISBN (C++)  (3) 2025.07.24
백준 2164번 카드 2(C++)  (0) 2025.07.16
백준 11650번 좌표 정렬하기 (C++)  (2) 2025.07.14