【C/C++】“秒懂”学C/C++不可错过的“经典编程题” — 日期类的经典运用 (含题链接)

1. 计算日期到天数转换

点这里:本题牛客网链接
在这里插入图片描述
我们先来看看这段关键代码:

该段代码巧用数组下标得到某年某月的天数,下面所有题都会运用此段代码

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

(1). 解题思路:

举列子:
在这里插入图片描述
通过函数GetMonthDay(int year, int month)可以轻松得到某年某月天数(0月是0天),看图片右边,我们先将3月天数15单独放一边,让3月先减1得到2,然后通过函数得到2月天数,将其保存再sum中,然后月份再减1,得到1月天数,累加到sum(初始化为0)中,直到月份为0;这里我们就得到了1月加2月的天数,最后输出的时候我们把单独放在一边的3月的天数加上。
其他日期道理相同,因此得到代码:

(2). 代码实现:

#include <iostream>
using namespace std;
int GetMonthDay(int year, int month)
{
    static int arrDays[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 arrDays[month];
}
int main() {
    int _year, _month, _day;
    cin >> _year >> _month >> _day;
    int sum = 0;
    while (_month > 0)
    {
        _month--;
        sum += GetMonthDay(_year, _month);//除去当前月份的天数之和
    }
    cout << sum + _day << endl;//加上当前月份天数_day
    return 0;
}

2. 打印日期

点这里:本题牛客网链接
在这里插入图片描述

(1). 解题思路:

举列子:
在这里插入图片描述
先定义并初始化month为1,循环结束条件为当前天数小于对应月份总天数,
进入循环先让天数减去1月总天数,月份加1为2,减去2月总天数,剩余天数小于3月份天数时结束循环,如果月份加到13,让年加1并且月份重新赋值为1。

其他日期道理相同,因此得到代码:

(2). 代码实现:

#include <iostream>
using namespace std;
int GetMonthDay(int year, int month)
{
    static int arrDay[] = {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 arrDay[month];
}
int main() {
    int year, n;
    scanf("%4d%d",&year, &n);
    int month = 1;
    while(n > GetMonthDay(year, month))
    {
        n -= GetMonthDay(year, month);
        month++;
        if(month == 13)
        {
            year++;
            month = 1;
        }
    } 
    printf("%04d-%02d-%02d\n",year,month,n);//n为余下的天数
}

3. 日期累加

点这里:本题牛客网链接
在这里插入图片描述

(1). 解题思路:

举例子:
最开始输入1个日期:
在这里插入图片描述

由于最开始可输入多个日期,所以定义count为输入日期个数,用while循环来达到目的。
所以得到代码:

(2). 代码实现:

#include <iostream>
using namespace std;
int GetMonthDay(int year, int month)
{
    static int arrDay[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 arrDay[month];
}
int main() {
    int count;
    scanf("%d\n",&count);
    while(count--)
    {
    int year, month, day, n;
   scanf("%4d%2d%2d%d",&year,&month, &day, &n);

   day += n;
   while(day > GetMonthDay(year, month))
   {
        day -= GetMonthDay(year, month);
        month++;
        if(month == 13)
        {
            year++;
            month = 1;
        }
    }
    printf("%04d-%02d-%02d\n",year, month, day);
  
    }
}

4. 日期差值

点这里:本题牛客网链接
在这里插入图片描述

(1). 解题思路:

计算两日期的间隔,这里的方法是:

  1. 先判断这两个日期的大小,然后让小的日期一天一天加到大的日期,在这期间通过定义并且初始化为0的变量n来计数,n即为这两日期相差天数
  2. 本道题采用将日期自定义为一个类型来实现,巩固并深入理解上篇博客
  3. 根据思路1我们可以写出代码:
int Date::GapDays(Date& d)
{
    Date max = *this;
    Date min = d;
    if (*this < d)//小于运算符重载
    {
        min = *this;
        max = d;
    }
    int n = 0;
    while (min != max)//不等于运算符重载
    {
        n++;
        ++min;//前置++运算符重载
    }
    return n;
}

由于日期已经被我们自定义为一个类型,所以根据上面代码我们知道要依次运用运算符重载写函数:

1.小于运算符重载函数bool operator<(const Date& d)const;
2.不等于运算符重载函数bool operator!=(const Date& d)const;
3.前置++运算符重载函数Date operator++();
4.而前置++运算符重载函数里面又要写函数Date& operator+=(const int& d);

最终得到如下代码

(2). 代码实现:

#include <climits>
#include <iostream>
using namespace std;
class Date {
public:
    Date(int year, int month, int day) :_year(year), _month(month), _day(day) {}

    int GetMonthDay(int _year, int _month);

    int GapDays(Date& d);

    bool operator<(const Date& d)const;
    bool operator!=(const Date& d)const;
    Date operator++();
    Date& operator+=(const int& d);

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

int Date::GetMonthDay(int year, int month)
{
    static int arrDays[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 arrDays[month];
}

int Date::GapDays(Date& d)
{
    Date max = *this;
    Date min = d;
    if (*this < d)//小于运算符重载
    {
        min = *this;
        max = d;
    }
    int n = 0;
    while (min != max)//不等于运算符重载
    {
        n++;
        ++min;//前置++运算符重载
    }
    return n;
}

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

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

Date Date::operator++()
{
    *this += 1;//+=运算符重载
    return *this;
}

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

int main() {
    int year1, month1, day1;
    scanf("%4d%2d%2d", &year1, &month1, &day1);
    Date d1(year1, month1, day1);

    int year2, month2, day2;
    scanf("%4d%2d%2d", &year2, &month2, &day2);
    Date d2(year2, month2, day2);

    cout << d1.GapDays(d2) + 1;//加1符合题意,即连续两天间隔天数为2
}
  • 184
    点赞
  • 131
    收藏
    觉得还不错? 一键收藏
  • 128
    评论
评论 128
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值