题解 | #日期累加#

日期累加

https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d


#include <iostream>
using namespace std;

class Date
{
    friend ostream& operator<<(ostream& out, Date& d);
public:
    Date(int year = 1, int month = 1, int day = 0)
        :_year(year)
        , _month(month)
        , _day(day)
    {}

    int GetMonthDay(int year, int month)
    {
        static int MonthdayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
        if (month == 2 && ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
        {
            return 29;
        }
        else
        {
            return MonthdayArray[month];
        }
    }

    bool operator<(Date& d)
    {
        if (_year < d._year)
        {
            return true;
        }
        else if (_year == d._year)
        {
            if (_month < d._month)
            {
                return true;
            }
            else if (_month == d._month)
            {
                if (_day < d._day)
                {
                    return true;
                }
            }
        }

        return false;
    }

    bool operator==(Date& d)
    {
        return _year == d._year
            && _month == d._month
            && _day == d._day;
    }

    Date& operator++()
    {
        *this += 1;

        return *this;
    }

    Date operator+=(int day)
    {
        _day += day;
        while (_day > GetMonthDay(_year, _month))
        {
            _day -= GetMonthDay(_year, _month);
            ++_month;
            if (_month == 13)
            {
                ++_year;
                _month = 1;
            }
        }

        return *this;
    }

    Date& operator+(int day)
    {
        *this += day;

        return *this;
    }
private:
    int _year;
    int _month;
    int _day;
};

ostream& operator<<(ostream& out,  Date& d)
{
    if (d._month < 10 && d._day < 10)
    {
        out << d._year << "-0" << d._month << "-0" << d._day << endl;
    }
    else if (d._month < 10 && d._day >= 10)
    {
        out << d._year << "-0" << d._month << "-" << d._day << endl;
    }
    else if (d._month >= 10 && d._day < 10)
    {
        out << d._year << "-" << d._month << "-0" << d._day << endl;
    }
    else
    {
        out << d._year << "-" << d._month << "-" << d._day << endl;
    }

    return out;

}
int main()
{
    int n = 0;
    int year, month, day;
    int Accumulated_Days;
    cin >> n;
    while (n--)
    { // 注意 while 处理多个 case

        while (cin >> year >> month >> day >> Accumulated_Days)
        {
            Date d1(year, month, day);
            Date d2 = d1 + Accumulated_Days;
            cout << d1;
        }
        
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

2025-12-30 16:42
同济大学 C++
仁狂躁使者:哎呀,不用担心,我当时配环境配了两天,项目捋不清就问问导师能不能用ai,慢慢就清了,会好起来的
点赞 评论 收藏
分享
嵌入式的小白:面试少的,说明你的投递的岗位和简历匹配度不高,技术这个东西很杂的,你这种情况,建议 1.看看嵌入式招聘的岗位需求,会有不同大方向的,比如MCU,RTOS的,或者linux上驱动的,或者应用层的,这都是简单分类,但对技术要求差异很大的 2.结合你的经验,看能和哪类匹配上,就找对应类别的 3.简历和招聘岗位需求对着看下,看人家需要啥,你会啥,匹配度高才有会高概率有面试的
秋招的第一个offer,...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务