【刷题日志】牛客 HJ73 计算日期到天数转换

计算日期到天数转换

  • 阅读题目
  • 解题方案 及 解题思路
  • 方法一 . 手撕日期类
  • 方法二 . 分别直接算出平年和闰年每个月的时间 并对应下标存入数组中,判断该年份为平年还是闰年,再 for循环依次求和
      • 优化:也不用分别算出平年和闰年的每个月的时间,闰年就比平年多一天,所以直接算出平年各月的天数,判断是否为闰年,是就在原基础上天数再 +1 就行。
  • 方法三 . 直接按平年,算好每个月份已累积到的天数(累积到的都是前面的月份完整天数的总和),再加上该月已过的天数,就是这一年中的第几天了



阅读题目

【刷题日志】牛客 HJ73 计算日期到天数转换_第1张图片



解题方案 及 解题思路

方法一 . 手撕日期类

我只把日期类中真正派上用场的成员函数写出来

思路从 int TheDayOfYear() 开始看起,顺着 //注释 思路看

//方案一 手撕一个日期类出来
#include 
using namespace std;
class Date {
  public:
    Date(int year = 0, int month = 0, int day = 0) {
        _year = year;
        _month = month;
        _day = day;
    }

    bool operator>(const Date& d) const {
        if (this->_year > d._year)
            return true;
        else if (this->_year == d._year && this->_month > d._month)
            return true;
        else if (this->_year == d._year && this->_month == d._month &&
                 this->_day > d._day)
            return true;
        else
            return false;
    }

    bool operator==(const Date& d) const {
        if (this->_year == d._year
                && this->_month == d._month
                && this->_day == d._day)
            return true;
        else
            return false;
    }

    bool operator!=(const Date& d) const {
        return !(*this == d);
    }

    bool operator<(const Date& d) const {
        return !(*this > d || *this == d);
    }

    Date& operator-=(int day) {
        if (day < 0) {
            *this += -day;
            return *this;
        }
        _day -= day;
        while (_day <= 0) {
            _month--;
            if (_month == 0) {
                _month = 12;
                _year--;
            }
            _day += GetMonthDays(_year, _month);
        }
        return *this;
    }

    Date& operator+=(int day) {
        if (day < 0) {
            *this -= -day;
            return *this;
        }
        // 对日期进行+=,直到++min到与max时间相同时,则结束
        _day += day;                   
        while (_day > GetMonthDays(_year, _month)) {    
            _day -= GetMonthDays(_year, _month);
            _month++;
            if (_month == 13) {
                _month = 1;
                _year++;
            }
        }
        return *this;
    }

    int GetMonthDays(int year, int month) {
        static int monthDays[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 monthDays[month];
    }

    Date& operator++() {
        *this += 1;     //再调用 `+=`自定义类型 重载运算符
        return *this;
    }

    int operator-(const Date& d) const {
        Date max = *this;
        Date min = d;
        int flag = 1;
        int cnt = 0;
        if (*this < d) {   //调用 bool`<` 自定义类型 重载判断运算符
            max = d;
            min = *this;
            flag = -1;
        }
        
        // 直到加到日期重合为止
        while (min != max) {   //调用 bool`!=`自定义类型 重载判断运算符
            ++cnt;     //内置类型的 ++
            ++min;     //调用 `++`自定义类型 重载运算符
        }
        return cnt * flag;
    }

    int TheDayOfYear() {
        return (*this - Date(_year, 1, 1)) + 1;  //调用 `-`重载运算符
    }
  private:
    int _year;
    int _month;
    int _day;
};



int main() {
    int year,month,day;
    while(cin>>year>>month>>day)
    {
        Date d(year,month,day);
        cout<<d.TheDayOfYear()<<endl;
    }
}


方法二 . 分别直接算出平年和闰年每个月的时间 并对应下标存入数组中,判断该年份为平年还是闰年,再 for循环依次求和

优化:也不用分别算出平年和闰年的每个月的时间,闰年就比平年多一天,所以直接算出平年各月的天数,判断是否为闰年,是就在原基础上天数再 +1 就行。

#include 
using namespace std;


int main() {
    //平年
    int ar[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int year, month, day;
    int cnt = 0;
    while (cin >> year >> month >> day) {
        for (int i = 1; i < month; i++) {
            cnt += ar[i];
        }

    }
    
    //并不是所有二月都要+1的,要判断是否经过 二月的最后一天
    if (month > 2 && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))) {
        cnt++;
    }
    cout << cnt + day << endl;
}


方法三 . 直接按平年,算好每个月份已累积到的天数(累积到的都是前面的月份完整天数的总和),再加上该月已过的天数,就是这一年中的第几天了

#include 
using namespace std;


int main() {
    //平年每月
    //int ar[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    //平年每个月的累积
    int ar[13] = {0,31,59,90,120,151,181,212,243,273,304,334};

    int year, month, day;
    cin>>year>>month>>day;

    //并不是所有二月都要+1的,要判断是否经过 二月的最后一天
    if (month > 2 && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))) {
        cout << ar[month-1]+day+1 << endl;
    }
    else {
        cout << ar[month-1]+day << endl;
    }    
}

你可能感兴趣的:(牛客刷题日志,算法,C++)