题目描述
定义一个结构体,包含年月日,表示一个学生的出生日期。然后在一群学生的出生日期中找出谁的出生日期排行第二
要求:出生日期的存储必须使用结构体,不能使用其他类型的数据结构。
要求程序全过程对出生日期的输入、访问、输出都必须使用结构。
输入
第一行输入t表示有t个出生日期
每行输入三个整数,分别表示年、月、日
依次输入t个实例
输出
输出排行第二老的出生日期,按照年-月-日的格式输出
输入样例1
6
1980 5 6
1981 8 3
1980 3 19
1980 5 3
1983 9 12
1981 11 23
输出样例1
1980-5-3
#include <iostream>
#include <algorithm>
using namespace std;
struct birthday {
int year;
int month;
int day;
};
bool compare(birthday a, birthday 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() {
int n, i;
cin >> n;
birthday *stu = new birthday[n];
for (i = 0; i < n; i++) {
cin >> stu[i].year >> stu[i].month >> stu[i].day;
}
sort(stu, stu + n, compare);
cout << stu[1].year << "-" << stu[1].month << "-" << stu[1].day;
}