谁是老二(结构体)

题目描述

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

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

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

输入

第一行输入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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值