728x90
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio> // c 문법 헤더파일
#include<vector> // 동적배열 라이브러리
#include<algorithm>
#include <string.h>
#include<string>
using namespace std; // cin,cout 편하게 사용 라이브러리
// [백준 10814 c++ V] 나이순 정렬
// 문제: 나이,이름을 입력받고 1. 나이순, 2.등록순으로 정렬
// 접근: pair로 정보 입력받고 벡터에 저장 후 정의함수로 정렬
// 풀이:
// 정복 젝터에 pair로 저장
// stable 정렬 // 나이가 같은 경우는
// 출력
// 개념:
// : stable_sort() : 입력된 순서를 유지하면서 정렬하는것
int n;
vector<pair<int, string>> v;
bool mysort(pair<int, string> a, pair<int, string> b) {
// stable 정렬이기 때문에 나이만 비교한면 됨
return a.first < b.first; // 나이 비교
}
int main()
{
ios::sync_with_stdio(false); // 계산시간 단축 // 문제마다 오류 유무 다름
cin.tie(NULL); cout.tie(NULL);// 입출력 시간 단축
// 나이,이름 입력, 벡터저장
cin >> n;
for (int i = 0; i < n; i++) {
int age; string name;
cin >> age >> name;
v.push_back({ age,name }); // 벡터에 페어로 저장
}
// 정렬 후 출력
stable_sort(v.begin(), v.end(),mysort); // stable sort
for (int i = 0; i < v.size(); i++) {
cout << v[i].first << " " << v[i].second << '\n';
}
return 0;
}
|
cs |
반응형
'Algorithm_BOJ(백준) > 정렬' 카테고리의 다른 글
[백준 10814 c++ VV] 나이순 정렬 (0) | 2021.08.06 |
---|---|
[백준 11650 c++ OO] 좌표 정렬하기 (0) | 2021.08.06 |
[백준 1181 c++ O] 단어 정렬 (0) | 2021.07.15 |
[백준 11651 c++ O] 좌표 정렬하기 2 (0) | 2021.07.15 |
[백준 11650 c++ O] 좌표 정렬하기 (0) | 2021.07.15 |