谁是老二(结构体)

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

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

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

输入
第一行输入t表示有t个出生日期

每行输入三个整数,分别表示年、月、日

依次输入t个实例

输出
输出排行第二老的出生日期,按照年-月-日的格式输出

样例输入
6
1980 5 6
1981 8 3
1980 3 19
1980 5 3
1983 9 12
1981 11 23
样例输出
1980-5-3

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

typedef struct student
{
	int year;
	int month;
	int day;
}student;

bool compare(const student& a, const student& b) {
	int birth1 = a.year * 10000 + a.month * 100 + a.day;
	int birth2 = b.year * 10000 + b.month * 100 + b.day;
	return birth1 < birth2;
}


int main()
{
	int num;
	cin >> num;
	student* stu = new student[num];
	for (int i = 0; i < num; i++) {
		cin >> stu[i].year >> stu[i].month >> stu[i].day;
	}
	sort(stu, stu + num, compare);
	cout << stu[1].year << "-" << stu[1].month << "-" << stu[1].day << endl;
	delete[] stu;
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值