Study hard
(c++)백준 10814번: 나이순 정렬 본문
https://www.acmicpc.net/problem/10814
10814번: 나이순 정렬
온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을 �
www.acmicpc.net
[풀이]
회원의 가입순서, 나이, 이름으로 구성된 구조체를 vector의 원소로 하여 문제의 조건에 맞는 사용자함수를 만들어 sort의 세 번째 인수로 넣어서 해결할 수 있다.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>//sort
using namespace std;
struct Info {
int signUp, age;//가입순서, 나이
string name;//이름
};
int N;
vector<Info>v;//회원 정보 저장
bool sortPeople(Info &A, Info &B) {
if (A.age == B.age)
return A.signUp < B.signUp;
return A.age < B.age;
}
void input() {
int age;
string name;
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> age >> name;
v.push_back({ i,age,name });
}
}
void solution() {
input();
sort(v.begin(), v.end(), sortPeople);
for (int i = 0; i < N; i++) {
cout << v[i].age << ' ' << v[i].name << '\n';
}
}
int main() {
solution();
return 0;
}
'백준 > 여러가지 문제들' 카테고리의 다른 글
(c++)백준 10989번: 수 정렬하기 3 (0) | 2020.06.11 |
---|---|
(c++)백준 10825번: 국영수 (0) | 2020.06.11 |
(c++)백준 11651번: 좌표 정렬하기 2 (0) | 2020.06.11 |
(c++)백준 11650번: 좌표 정렬하기 (0) | 2020.06.11 |
(c++)백준 2751번: 수 정렬하기 2 (0) | 2020.06.11 |