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

 

5635번: 생일

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

www.acmicpc.net

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

class Student
{

public:
	string name;
	int day;
	int month;
	int year;

};

bool sortfunc(Student& s1, Student& s2)
{
	if (s1.year < s2.year)
		return false;
	else if (s1.year > s2.year)
		return true;
	else
	{
		if (s1.month < s2.month)
			return false;
		else if (s1.month > s2.month)
			return true;
		else
		{
			if (s1.day < s2.day)
				return false;
			else if (s1.day > s2.day)
				return true;
		}
	}
}

int main()
{
	int n;
	cin >> n;

	vector<Student> v;
	v.resize(n);

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

	sort(v.begin(), v.end(), sortfunc);

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

	return 0;
}

학생의 정보를 담는 Student 클래스를 만들어서 벡터로 학생 정보를 일단 담는다.

그리고 STL을 이용해 sort 함수를 호출해서 나이를 오름차순으로 정렬할건데

세번째 매개변수에 정렬기준으로 사용할 함수를 사용할것이고 그 함수는 sortfunc이다.

매개변수로는 Student 변수를 받는다.

태어난 연도를 기준으로 먼저 정렬하고 연도가 같으면 월, 월이 같으면 일로 정렬하면 쉽게 된다.

'알고리즘 문제' 카테고리의 다른 글

[C++] 백준 11047번 : 동전 0  (0) 2022.07.14
[C++] 백준 11399번 : ATM  (0) 2022.07.14
[C++] 백준 14916번 : 거스름돈  (0) 2022.07.13
[C++] 백준 9625번 : BABBA  (0) 2022.07.12
[C++] 백준 9656번 : 돌 게임 2  (0) 2022.07.12

+ Recent posts