[ 문제 ]
[ 접근방법 ]
구조체를 만들어 입력을 받고 비교함수를 정의하여 정렬순서를 정한다.
아래처럼 cmp라는 bool함수를 만들고 이 함수를 sort 함수 안에 매개변수로 활용하면 나만의 정렬이 가능하다.
[ 소스코드 ]
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Student
{
string name;
int day, month, year;
};
bool cmp(Student x, Student y)
{
if (x.year != y.year)
return x.year > y.year;
else if (x.month != y.month)
return x.month > y.month;
return x.day > y.day;
}
int n;
vector<Student> vec;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
{
Student stu;
cin >> stu.name >> stu.day >> stu.month >> stu.year;
vec.push_back(stu);
}
sort(vec.begin(), vec.end(), cmp);
cout << vec[0].name << "\n"
<< vec[vec.size() - 1].name;
return 0;
}
'PS' 카테고리의 다른 글
[ 백준 / C++ ] 2294 : 동전 2 (0) | 2024.07.08 |
---|---|
[ 백준 / C++ ] 13459 : 구슬 탈출 (0) | 2024.07.05 |
[ 백준 / C++ ] 2343 : 기타 레슨 (0) | 2024.06.27 |
[ 백준 / C++ ] 1926 : 그림 (0) | 2024.06.26 |
[ 백준 / C++ ] 12100 : 2048 (Easy) (0) | 2024.06.24 |