当前位置: 首页 > news >正文

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

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

  • 1. 计算日期到天数转换
    • (1). 解题思路:
    • (2). 代码实现:
  • 2. 打印日期
    • (1). 解题思路:
    • (2). 代码实现:
  • 3. 日期累加
    • (1). 解题思路:
    • (2). 代码实现:
  • 4. 日期差值
    • (1). 解题思路:
    • (2). 代码实现:

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;elsereturn 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;elsereturn 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;//加上当前月份天数_dayreturn 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;elsereturn 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;elsereturn 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;elsereturn 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;elsereturn false;;
}bool Date::operator!=(const Date& d)const {if (_year == d._year && _month == d._month && _day == d._day)return false;elsereturn 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
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Git-下载的zip包项目重新指向github项目地址
  • Request Response
  • VSCode学习笔记
  • 802.11 中 scrambler的matlab仿真
  • 云计算之大数据(下)
  • 陕西农信银行合规知识竞赛活动方案
  • STM32 HAL freertos零基础(一)-任务创建
  • 算法-双指针技巧
  • 搭建Kafka+zookeeper集群调度
  • 运营如何判断账号是否起号失败?
  • Bev pool 加速(1): torch.autograd.Function的使用
  • 从C到C++
  • 微信小程序-文件下载
  • 体系结构权衡分析方法(ATAM)
  • 基于阿里云函数计算(FC)x 云原生 API 网关构建生产级别 LLM Chat 应用方案最佳实践
  • Google 是如何开发 Web 框架的
  • ➹使用webpack配置多页面应用(MPA)
  • CentOS 7 防火墙操作
  • emacs初体验
  • HTTP--网络协议分层,http历史(二)
  • Laravel Telescope:优雅的应用调试工具
  • MyEclipse 8.0 GA 搭建 Struts2 + Spring2 + Hibernate3 (测试)
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • Promise面试题,控制异步流程
  • Spark学习笔记之相关记录
  • SQLServer之创建显式事务
  • Vue2 SSR 的优化之旅
  • webgl (原生)基础入门指南【一】
  • 从tcpdump抓包看TCP/IP协议
  • 翻译:Hystrix - How To Use
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 十年未变!安全,谁之责?(下)
  • 消息队列系列二(IOT中消息队列的应用)
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • 大数据全解:定义、价值及挑战
  • ​​快速排序(四)——挖坑法,前后指针法与非递归
  • ​html.parser --- 简单的 HTML 和 XHTML 解析器​
  • ​Java基础复习笔记 第16章:网络编程
  • # linux 中使用 visudo 命令,怎么保存退出?
  • # Panda3d 碰撞检测系统介绍
  • # 利刃出鞘_Tomcat 核心原理解析(八)-- Tomcat 集群
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • $.ajax,axios,fetch三种ajax请求的区别
  • (02)Unity使用在线AI大模型(调用Python)
  • (2)MFC+openGL单文档框架glFrame
  • (5)STL算法之复制
  • (八)Spring源码解析:Spring MVC
  • (区间dp) (经典例题) 石子合并
  • (学习总结16)C++模版2
  • ******IT公司面试题汇总+优秀技术博客汇总
  • **PHP分步表单提交思路(分页表单提交)
  • .mysql secret在哪_MYSQL基本操作(上)
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .NET delegate 委托 、 Event 事件,接口回调
  • .net程序集学习心得