BOJ - 5635- 생일

ggh·2022년 5월 11일
0

백준

목록 보기
27/112

BOJ - 5635- 생일

문제


5635번: 생일

문제 개념


문제

어떤 반에 있는 학생들의 생일이 주어졌을 때, 가장 나이가 적은 사람과 가장 많은 사람을 구하는 프로그램을 작성하시오.

입력

첫째 줄에 반에 있는 학생의 수 n이 주어진다. (1 ≤ n ≤ 100)

다음 n개 줄에는 각 학생의 이름과 생일이 "이름 dd mm yyyy"와 같은 형식으로 주어진다. 이름은 그 학생의 이름이며, 최대 15글자로 이루어져 있다. dd mm yyyy는 생일 일, 월, 연도이다. (1990 ≤ yyyy ≤ 2010, 1 ≤ mm ≤ 12, 1 ≤ dd ≤ 31) 주어지는 생일은 올바른 날짜이며, 연, 월 일은 0으로 시작하지 않는다.

이름이 같거나, 생일이 같은 사람은 없다.

출력

첫째 줄에 가장 나이가 적은 사람의 이름, 둘째 줄에 가장 나이가 많은 사람 이름을 출력한다.

구현


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Student
{
public:
    string m_name;
    int m_years, m_month, m_days;

    Student(string name, int years, int month, int days)
    {
        this->m_name = name;
        this->m_years = years;
        this->m_month = month;
        this->m_days = days;
    }
    bool operator < (Student &student)
    {
        if(this->m_years == student.m_years)
        {
            if(this->m_month == student.m_month)
                return  this->m_days < student.m_days;
            else
                return this->m_month < student.m_month;
        }
        else
            return this->m_years < student.m_years;
    }
};

int main()
{
    int num;
    string name;
    int years, month, days; 
    
    cin >> num;
    vector<Student> v;
    for(int i=0; i < num; i++)
    {
        cin >> name >> days >> month >> years;
        Student stu(name, years, month, days);
        v.push_back(stu);
    }

    sort(v.rbegin(), v.rend());

    cout << v[0].m_name << endl << v[num-1].m_name;
    return 0;
}

SOL


비교해야 할 대상이 3개 이상이기에 pair를 사용하기 보단 조금 번거롭더라도 사용자 정의 자료형 class student와 operator의 개념을 이용하여 해결한다.

Reference & 다른 PS 모음집


https://github.com/ggh-png/PS

C++ STL sort ( )

profile
See you in a minute. : )

0개의 댓글