Koala - 14기/코딩테스트 준비 스터디

[백준/C++] 5635번: 생일

소코기 2024. 3. 28. 13:21

https://www.acmicpc.net/problem/5635

 

5635번: 생일

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

www.acmicpc.net

  • 코드
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdio.h>
#include <queue>
#include <vector>
#include <unordered_map>
#include <set>
#include <map>
#include<cmath>
#include<stack>
#include<deque>
#define LL long long
using namespace std;

struct Birth {
	string name;
	int month;
	int day;
	int year;
};

Birth arr[100];

bool compare(const Birth& a, const Birth& b) {

	if (a.year != b.year)
		return a.year > b.year;
	if (a.month != b.month)
		return a.month > b.month;
	return a.day > b.day;
}


int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> arr[i].name >> arr[i].day >> arr[i].month>> arr[i].year;
	}
	
	sort(arr, arr + n, compare);

	cout << arr[0].name << endl;
	cout << arr[n - 1].name << endl;

	return 0;
}
  • 해설

cmp와 구조체를 이용하여 해결했다. 년도, 달, 일 순으로 내림차순을 하고 두 숫자가 같지 않다면 정렬을 해주도록 했다.