题解 | #计算日期到天数转换#

计算日期到天数转换

https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded

#include <iostream>
using namespace std;

class Date
{
public:
    void DateInit()
    {
        cin>>_year;
        cin>>_month;
        cin>>_date;
    }
    int GetMonthDay(int year,int month)
    {
        static int dateArr[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;
        }
        return dateArr[month];
    }

    int GetAllDay()
    {
        int allDay=0;
        for(int i=1;i<_month;i++)
        {
            allDay += GetMonthDay(_year,i);
        }
        allDay+=_date;
        return allDay;
    }

private:
    int _date=0;
    int _month=0;
    int _year=0;
};

int main() {
    Date day;
    day.DateInit();
    cout<<day.GetAllDay()<<endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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