본문 바로가기

백준/실버

[백준 5635번] 생일 (C++)

문제링크 : https://www.acmicpc.net/problem/5635

 

5635번: 생일

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

www.acmicpc.net

#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에 넣어준다.

 

생일, 달, 날짜가 빠른 순으로 정렬했기에 나이가 많은 순으로 정렬된다.

따라서 맨 끝 값과 맨 앞값을 차례로 출력해준다.