문제링크 : https://www.acmicpc.net/problem/5635
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAX = INT_MAX;
int N;
struct info
{
string name;
int day, month, year;
};
bool cmp(struct info a, struct info b)
{
if (a.year < b.year) return 1; //생일이 빠른 순
else if (a.year == b.year)
{
if (a.month < b.month) return 1; //생일이 같으면 달이 빠른 순
else if (a.month == b.month)
{
if (a.day < b.day) return 1; //생일과 달이 같으면 날짜가 빠른 순
else return 0;
}
else return 0;
}
else return 0;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N;
vector<info>v;
for(int i=0; i<N; i++)
{
string a;
int b, c, d;
cin >> a >> b >> c >> d;
v.push_back({a, b, c, d});
}
sort(v.begin(), v.end(), cmp);
cout << v[N-1].name << "\n" << v[0].name;
return 0;
}
한번에 입력받는 수가 많아서 구조체로 받도록 하였다.
그리고 문제에 주어진 정렬 조건을 맞추기 위해 따로 정렬용 함수를 만들어서 sort에 넣어준다.
생일, 달, 날짜가 빠른 순으로 정렬했기에 나이가 많은 순으로 정렬된다.
따라서 맨 끝 값과 맨 앞값을 차례로 출력해준다.
'백준 > 실버' 카테고리의 다른 글
[백준 1822번] 차집합 (C++) (0) | 2024.04.04 |
---|---|
[백준 15688번] 수 정렬하기 5 (C++) (0) | 2024.04.03 |
[백준 19621번] 회의실 배정 2 (C++) (0) | 2024.04.01 |
[백준 1059번] 좋은 구간 (C++) (0) | 2024.03.31 |
[백준 2012번] 등수 매기기 (C++) (0) | 2024.03.30 |