0
点赞
收藏
分享

微信扫一扫

【15分】E. 谁是老二(结构体)

云岭逸人 2022-03-26 阅读 57
算法c++

【15分】E. 谁是老二(结构体)

题目描述
定义一个结构体,包含年月日,表示一个学生的出生日期。然后在一群学生的出生日期中找出谁的出生日期排行第二

要求:出生日期的存储必须使用结构体,不能使用其他类型的数据结构。

要求程序全过程对出生日期的输入、访问、输出都必须使用结构。

输入
第一行输入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 SDate
{
    int year,month,day;
};

bool cmp(SDate &a, SDate &b)
{
    if(a.year != b.year) return a.year < b.year;
    if(a.month != b.month) return a.month < b.month;
    if(a.day != b.day) return a.day < b.day;
}

int main( )
{ 
    int t;
    cin >> t;
  	SDate date[t];
    for(int i = 0;i < t;i ++)
    {
        cin >> date[i].year >> date[i].month >> date[i].day;
    }  
    sort(date,date + t,cmp);
    cout << date[1].year << "-" << date[1].month << "-" << date[1].day << endl;
    return 0;
}
举报

相关推荐

【15分】D. 线段相交----结构体

E. Best Pair

E. Mishap in Club

E. Breaking the Wall

E. Replace the Numbers

CF E. Best Pair

E. Equal Tree Sums

0 条评论