题目描述
定义一个结构体,包含年月日,表示一个学生的出生日期。然后在一群学生的出生日期中找出谁的出生日期排行第二
要求:出生日期的存储必须使用结构体,不能使用其他类型的数据结构。
要求程序全过程对出生日期的输入、访问、输出都必须使用结构。
输入
第一行输入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
答案
#include<iostream>
using namespace std;
struct birth{
int year;
int month;
int day;
};
int main()
{
int t;
cin >> t;
birth *stu = new birth[t];
for(int i = 0; i < t; i++){
cin >> stu[i].year;
cin >> stu[i].month;
cin >> stu[i].day;
}
int n = 2;
birth* arr1 = new birth[t];
birth * arr2 = new birth[t];
birth c;
while(n--){
int minYear =2023,s1=0,s2=0;
for (int i = 0; i < t; i++) {
if (stu[i].year < minYear) {
minYear = stu[i].year;
}
}
for (int i = 0; i < t; i++) {
if (stu[i].year == minYear) {
arr1[s1] = stu[i];
s1++;
}
}
int minMon=12;
for (int i = 0; i < s1; i++) {
if (arr1[i].month < minMon) {
minMon = stu[i].month;
}
}
for (int i = 0; i < s1; i++) {
if (arr1[i].month == minMon) {
arr2[s2] = arr1[i];
s2++;
}
}
int minDay=31;
for(int i=0;i<s2;i++)
if(arr2[i].day< minDay)
{
minDay = arr2[i].day;
c = arr2[i];
}
int a=0;
if(t!=0){
for (int i = 0; i < t; i++) {
if (stu[i].day == c.day && stu[i].month == c.month && stu[i].year == c.year) {
a = i;
}
}
for (int i = a; i < t; i++) {
stu[a] = stu[a + 1];
}
}
}
cout << c.year << "-" << c.month << "-" << c.day << endl;
return 0;
}