C. 谁是老二(结构体)
题目描述
定义一个结构体,包含年月日,表示一个学生的出生日期。然后在一群学生的出生日期中找出谁的出生日期排行第二
要求:出生日期的存储必须使用结构体,不能使用其他类型的数据结构。
要求程序全过程对出生日期的输入、访问、输出都必须使用结构。
输入
第一行输入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
输入样例2
5
1980 5 6
1981 8 3
1980 5 3
1983 9 12
1981 11 23
输出样例2
1980-5-6
输入样例3
4
2000 11 28
2021 10 30
2022 8 29
2022 5 1
输出样例3
2021-10-30
该题比较简单,主要考察对结构数组的使用,不过注意为了方便比较,我们将每个人的生日转换成数字
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
using namespace std;
struct student
{
int year;
int month;
int day;
int num;
};
int main()
{
int n;
cin >> n;
student* per = new student[n];
for (int i = 0; i < n; i++)
{
cin >> per[i].year >> per[i].month >> per[i].day;
per[i].num = per[i].year * 10000 + per[i].month * 100 + per[i].day;
}
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
if (per[j].num > per[j + 1].num)
{
swap(per[j], per[j + 1]);
}
}
}
cout << per[1].year << '-' << per[1].month << '-' << per[1].day << endl;
delete per;
return 0;
}