문제
👉 5635 생일

풀이 방법
- 각 사람들의 생일을 문자열로 변경하여 저장한다.
🧐 month, day의 경우 길이가 1이면 '0'을 앞에 붙여야 한다.
- 정렬하여 최소, 최대 생일을 출력한다.
코드
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Person {
public:
string name;
string birthday;
};
bool compareAge(Person first, Person second) {
return first.birthday > second.birthday;
}
string getBirthDay(int year, int month, int day) {
string syear = to_string(year);
string smonth = to_string(month);
string sday = to_string(day);
if (month < 10) {
smonth = '0' + smonth;
} else if (day < 10) {
sday = '0' + sday;
}
return syear + smonth + sday;
}
int main() {
int n, year, month, day;
string name;
vector<Person> people;
cin >> n;
for (int i=0; i<n; i++) {
cin >> name >> day >> month >> year;
people.push_back({name, getBirthDay(year, month, day)});
}
sort(people.begin(), people.end(), compareAge);
cout << people[0].name << "\n" << people[people.size()-1].name << endl;
}