Offer 驾到,掘友接招!我正在参与2022春招打卡活动,点击查看活动详情。
题目描述
定义一个结构体,包含年月日,表示一个学生的出生日期。然后在一群学生的出生日期中找出谁的出生日期排行第二
要求:出生日期的存储必须使用结构体,不能使用其他类型的数据结构。
要求程序全过程对出生日期的输入、访问、输出都必须使用结构。
输入
第一行输入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
思路分析
定义一个结构体数组,然后排序就完了,排序要求这么多用sort比较好排。
AC代码
#include<iostream>
#include<algorithm>
using namespace std;
struct student
{
int year,month,day;
};
bool leibniz(student a,student 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 t,i;
cin>>t;
student a[t];
for(i=0;i<t;i++)
cin>>a[i].year>>a[i].month>>a[i].day;
sort(a,a+t,leibniz);
cout<<a[1].year<<'-'<<a[1].month<<'-'<<a[1].day;
}