백준(BOJ) 5635 생일

margarin·2021년 2월 8일
0

알고리즘

목록 보기
1/13

문제

👉 5635 생일

풀이 방법

  1. 각 사람들의 생일을 문자열로 변경하여 저장한다.
    🧐 month, day의 경우 길이가 1이면 '0'을 앞에 붙여야 한다.
  2. 정렬하여 최소, 최대 생일을 출력한다.

코드

#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;
}
profile
화이팅 🥹

0개의 댓글