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

计算日期到天数转换

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

#include <iostream>
using namespace std;
class Date {
  public:
    friend istream& operator>>(istream& in, Date& d);
    Date(int year = 1, int month = 1, int day = 1) {
        _year = year;
        _month = month;
        _day = day;
    }
    bool isyear(int year);
    static int GetMonthDay(int year, int month);
    static int DateCount(Date d);
  private:
    int _year;
    int _month;
    int _day;
};
int Date::GetMonthDay(int year, int month) {
    static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int day = days[month];
    if (month == 2 && ((year % 4 == 0 && year % 100 != 0)
                       || (year % 400 == 0))) {
        day += 1;
    }
    return day;
}
bool Date::isyear(int year) {
    if ((year % 4 == 0 && year % 100 != 0)
            || (year % 400 == 0)) {
        return true;
    }
    return false;
}
int Date::DateCount(Date d) {
    int n = 0;
    for (int i = 1; i < d._month; i++) {
        n += GetMonthDay(d._year, i);
    }
    n += d._day;
    return n;
}

inline istream& operator>>(istream& in, Date& d) {
    in >> d._year >> d._month >> d._day;
    return in;
}

int main() {
    Date d1;
    cin >> d1;
    int n = Date::DateCount(d1);
    cout << n << endl;
    return 0;

}

全部评论

相关推荐

找个工作&nbsp;学历是要卡的&nbsp;要求是高的&nbsp;技能不足是真的&nbsp;实习经验是0的&nbsp;简历无处可写是事实的&nbsp;钱不好赚是真的&nbsp;想躺平又不敢躺&nbsp;也不甘心躺&nbsp;怕自己的灵感和才华被掩埋甚至从未被自己发现&nbsp;又质疑自己是否真正有才华
码农索隆:你现在啊,你心里都明白咋回事,但是你没办法改变现状,一想到未来,你又没有信心狠下心来在当下努力。 得走出这种状态,不能一直困在那里面,哪不行就去提升哪,你一动不动那指定改变不了未来,动起来,积少成多才能越来越好
点赞 评论 收藏
分享
05-26 09:07
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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