题解 | #日期差值#

日期差值

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

题目

描述

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天

输入描述: 有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD

输出描述: 每组数据输出一行,即日期差值

主要思路

通过重载运算符-来实现日期差,d1-d2

实际实现方法1:

大的日期为max,小的日期为min

min+=1,直到min==max,计算中间+=多少次

实际实现方法2:

将每个日期与当年1月1日作差,得相差天数

再将两个日期年份做差,判断中间有几个闰年后

再将结果进行运算

重点知识点

  1. C++运算符重载
  • +=和+的区别

+=引用调用运算符的对象本身

Date& operator+=(int day)
    {
        _day+=day;
        while(_day>GetMonthDay(_year,_month))
        {
            _day-=GetMonthDay(_year,_month);
            _month++;
            if(_month>12)
            {
                _month=1;
                _year++;
            }
        }
        return *this;
    }

+则返回一个新的Date对象

Date operator+(int day) const
{
    Date temp = *this;
    temp += day;
    return temp;
}
  1. 从字符串中提取多组输入

通过 substr 方法从字符串中提取年、月、日部分,然后将它们转换成整数类型并存储在 Date 结构体中,可以有效地处理多组日期输入。

Date(string d)
    {
        _year=stoi(d.substr(0,4));
        _month=stoi(d.substr(4,2));
        _day=stoi(d.substr(6,2));
    }

int main() {
    string str1,str2;
    cin >>str1>>str2;
    Date d1(str1);  //用字符串中提取的输入实例化对象
    Date d2(str2);
}

解题代码

使用了第一种实现方法

#include <iostream>
#include <string>
using namespace std;

class Date
{
public:
    Date();
    Date(int year,int month,int day)
    :_year(year),_month(month),_day(day){}
    Date(string d)
    {
        _year=stoi(d.substr(0,4));
        _month=stoi(d.substr(4,2));
        _day=stoi(d.substr(6,2));
    }
    Date(const Date& d)
    {
        _year=d._year;
        _month=d._month;
        _day=d._day;
    }

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

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

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

    Date& operator+=(int day)
    {
        _day+=day;
        while(_day>GetMonthDay(_year,_month))
        {
            _day-=GetMonthDay(_year,_month);
            _month++;
            if(_month>12)
            {
                _month=1;
                _year++;
            }
        }
        return *this;
    }

    int operator-(const Date& d2)
    {
        Date max=*this;
        Date min=d2;
        int n=0;
        while(min!=max)
        {
            min+=1;
            n++;
        }
        return ++n;
    }

private:
    int _year;
    int _month;
    int _day;
};

int main() {
    string str1,str2;
    cin >>str1>>str2;
    Date d1(str1);
    Date d2(str2);
    if(d1>d2)
    {
        cout <<d1-d2<<endl;
    }
    else 
    {
        cout<<d2-d1<<endl;
    }
}
#c++##解题#
刷题 - 解题 文章被收录于专栏

记录遇到的题目,解题思路和相关知识点

全部评论

相关推荐

黑皮白袜臭脚体育生:简历条例统一按使用了什么技术实现了什么功能解决了问题或提升了什么性能指标来写会好些,如使用布隆过滤器实现了判断短链接是否存在,大大提升了查询速度
点赞 评论 收藏
分享
牛客鼠:校友你这简历基本无敌了,春招刷刷题去冲大厂
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务