题解 | #日期累加#

日期累加

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

#include <iostream>
using namespace std;

class Date{
private:
    size_t _year;
    size_t _month;
    size_t _date;
public:
    Date(const size_t year,const size_t month,const size_t date)
    :_year(year)
    ,_month(month)
    ,_date(date)
    {}
    int Day(size_t month)//每月天数
    {
        static int arr[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))
        {
            arr[2]=29;
        }
        return arr[month];
    }
    void addDate(int i)//累加
    { 
        _date+=i;
        while(_date>Day(_month))
        {
            _date-=Day(_month);
            _month++;
            if(_month>12)
            {
                _year++;
                _month=1;
            }
        }

//输出
    printf("%04d-%02d-%02d\n",_year,_month,_date);
        
    }
};


int main()
{
    int i=0;
    cin>>i;
    while(i--)
    {
        size_t y,m,d,num;
        cin>>y>>m>>d>>num;
        Date day(y,m,d);
        day.addDate(num);
    }

    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
09-19 12:15
门头沟学院 Java
迷茫的大四🐶:这下是真的打牌了,我可以用感谢信和佬一起打牌吗
点赞 评论 收藏
分享
夏日狂想曲:连体婴是这样的,不过国内还有上四休三的公司?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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