C++:OJ练习(每日练习!)
编程题:
题一:计算日期到天数的转换
计算日期到天数转换_牛客题霸_牛客网 (nowcoder.com)

示例1
输入:
2012 12 31输出:
366
思路一:
第一步:创建年,月,日的变量,并按要求输入;
第二步:创建一个数组记录平年每个月的天数,以及记录总天数的sum;
第三步:将除当前月的以外的天数记录在sum中,再去判断是不是闰年,是就+1;
第四步:打印总天数。
#include <iostream>
using namespace std;int main()
{int _year,_month,_day;cin>>_year>>_month>>_day;int sum = _day;int arr[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};int n = _month;while(--n){sum += arr[n];}if(_month > 2 && ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)))sum += 1;cout<<sum<<endl;return 0;
}
思路二:
第一步:创建一个日期类:私有成员变量有:年,月,日;
第二步:创建一个构造函数,给自定义类型的对象完成初始化;创建一个赋值运算符重载" >> "保证自定义类型的输入;以及赋值运算符重载" << "自定义保证能够输出自定义类型的内容;需要注意的是" << " " >> "需要声明为友元函数,且在类外定义;最后再创建一个函数得到当前年这个月的天数;
第三步:根据题意将输入的年,月,日转换成是这一年的第几天;
#include <iostream>
using namespace std;class Date
{public://构造函数Date(int year = 1,int month = 1,int day = 1){_year = year;_month = month;_day = day;}//计算当前年月所对应的天数int GetMonth(int& year,int& month){int arr[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;}return arr[month];}//友元函数声明friend ostream& operator<<(ostream& out,Date& d);friend istream& operator>>(istream& out,Date& d);private:int _year;int _month;int _day;
};//赋值运算符重载
ostream& operator<<(ostream& out,Date& d)
{int sum = d._day;--d._month;while(d._month >0){sum += d.GetMonth(d._year, d._month);--d._month;}out<<sum<<endl; return out;
}
istream& operator>>(istream& in,Date& d)
{in>>d._year>>d._month>>d._day;return in;
}int main()
{Date d1;cin>>d1;cout<<d1<<endl;return 0;
}
题二:日期差值
日期差值_牛客题霸_牛客网 (nowcoder.com)

示例1
输入:
20110412 20110422输出:
11
思路一:
#include <iostream>
using namespace std;
#include <stdbool.h>class Date
{
public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}int GetMonth(int& year, int& month){int arr[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;}return arr[month];}bool operator!=(Date& d){return !(_year == d._year && _month == d._month && _day == d._day);}bool operator<(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 && _day < d._day){return true;}return false;}Date& operator++(){++_day;if (_day > GetMonth(_year, _month)){_day = _day - GetMonth(_year, _month);++_month;if (_month == 13){++_year;_month = 1;}}return *this;}int operator-(Date& d){//int flag = 1;Date max = *this;Date min = d;if (*this < d){max = d;min = *this;//flag = -1;}int n = 0;while (min != max){++min;++n;}return n + 1;}friend ostream& operator<<(ostream& out, Date& d);friend istream& operator>>(istream& out, Date& d);private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out, Date& d)
{out << d._year << d._month << d._day << endl;return out;
}istream& operator>>(istream& in, Date& d)
{scanf("%4d%2d%2d", &d._year, &d._month, &d._day);return in;
}int main()
{Date d1;Date d2;cin >> d1;cin >> d2;cout << d1 - d2;return 0;
}
// 64 位输出请用 printf("%lld")
题三:打印日期
打印日期_牛客题霸_牛客网 (nowcoder.com)

示例1
输入:
2000 3 2000 31 2000 40 2000 60 2000 61 2001 60输出:
2000-01-03 2000-01-31 2000-02-09 2000-02-29 2000-03-01 2001-03-01
思路一:
#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}int GetMonth(int& year, int& month){int arr[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;}return arr[month];}void Calendar(){while (_day > GetMonth(_year, _month)){_day = _day - GetMonth(_year, _month);++_month;if (_month == 13){++_year;_month = 1;}}}friend ostream& operator<<(ostream& out, Date& d);friend istream& operator>>(istream& out, Date& d);private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out, Date& d)
{printf("%04d-%02d-%02d", d._year, d._month, d._day);return out;
}
istream& operator>>(istream& in, Date& d)
{scanf("%4d%d", &d._year, &d._day);return in;
}int main()
{Date d1;cin >> d1;d1.Calendar();cout << d1;return 0;
}
// 64 位输出请用 printf("%lld")
题四:日期累加
日期累加_牛客题霸_牛客网 (nowcoder.com)

示例1
输入:
1 2008 2 3 100输出:
2008-05-13
思路一:
#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1,int sky = 0){_year = year;_month = month;_day = day;_sky = sky;}int GetMonth(int& year, int& month){int arr[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;}return arr[month];}void Calendar(){_day = _day + _sky;while (_day > GetMonth(_year, _month)){_day = _day - GetMonth(_year, _month);++_month;if (_month == 13){++_year;_month = 1;}}}friend ostream& operator<<(ostream& out, Date& d);friend istream& operator>>(istream& out, Date& d);private:int _year;int _month;int _day;int _sky;
};ostream& operator<<(ostream& out, Date& d)
{printf("%04d-%02d-%02d", d._year, d._month, d._day);return out;
}
istream& operator>>(istream& in, Date& d)
{in>>d._year>>d._month>>d._day>>d._sky;return in;
}int main()
{int n = 0;cin>>n;while(n--){Date d1;cin>>d1;d1.Calendar();cout<<d1<<endl;;}return 0;
}
// 64 位输出请用 printf("%lld")
本人实力有限可能对一些地方解释和理解的不够清晰,可以自己尝试读代码,或者评论区指出错误,望海涵!
感谢大佬们的一键三连! 感谢大佬们的一键三连! 感谢大佬们的一键三连!

相关文章:
C++:OJ练习(每日练习!)
编程题: 题一:计算日期到天数的转换 计算日期到天数转换_牛客题霸_牛客网 (nowcoder.com) 示例1 输入: 2012 12 31 输出: 366 思路一: 第一步:创建年,月,日的变量,并按…...
GPTs Hunter 是什么?
原文: https://openaigptguide.com/openai-gpts-hunter/ GPTs Hunter 是一个功能强大的免费导航网站,支持多语言,提供用户友好的界面。 GPTs Hunter:功能强大的免费导航网站 GPTs Hunter是一个功能强大的免费导航网站ÿ…...
【移远QuecPython】EC800M物联网开发板的硬件TIM定时器精准延时
【移远QuecPython】EC800M物联网开发板的硬件TIM定时器精准延时 文章目录 导入库定时器初始化延时函数定时中断回调调用函数打包附录:列表的赋值类型和py打包列表赋值BUG复现代码改进优化总结 py打包 首先 这个定时器是硬件底层级别的 优先级最高 如果调用 会导致GN…...
HDU 1027:Ignatius and the Princess II ← next_permutation()
【题目来源】http://acm.hdu.edu.cn/showproblem.php?pid1027【题目描述】 Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5…...
主题讲座:全球增材制造现状与未来(暨香港科技大学广州|智能制造学域2024博士学位全额奖学金项目)
时间:2023 年11月16日(星期四)14:30 地点:合肥工业大学 学术会议中心三楼报告厅 主讲嘉宾:陈模军 助理教授 https://facultyprofiles.hkust-gz.edu.cn/faculty-personal-page/CHEN-Mojun/mjchen 报名表直达࿱…...
hugeGraph修改PropertyKey属性类型
修改PropertyKey字段属性的类型,发现没办法保留数据的前提下修改,智能是先删除数据,然后再修改,或者备份后修改再恢复。 方法一、 修改groovy脚本中的Text为Int后重新建元数据 schema.propertyKey(“youkey”).asText().valueSing…...
vscode 访问本地或者远程docker环境
1、vscode 访问docker本地环境 直接点击左下角连接图标,弹出选项可以选择容器,只要容器在本地运行者,选择attach可以看到运行中的容器可以选择,选择其中需要选择的就行 ## 运行容器,可以-d后台运行都可以 docker run…...
人工智能与充电技术:携手共创智能充电新时代
人工智能与充电技术:携手共创智能充电新时代 摘要:本文探讨了人工智能与充电技术的结合及其在未来充电设施领域的应用。通过分析智能充电系统的技术原理、优势以及挑战,本文展望了由人工智能驱动的充电技术为未来电动交通带来的巨大变革与机…...
『自定义B站视频播放速度』
哔哩哔哩 的最高播放速度是 2.0, 但对于我们这种程序员来说,2.0 速度观看学习视频还是稍微慢了点, 🫵🏻3.0 以上才是王道🫵🏻, 下面就是具体的操作方法: ① 在浏览器…...
Java入门篇 之 继承
本篇碎碎念:最近的课程遇到瓶颈了,看的时候感觉自己会了,但是结束仔细一回顾还是一知半解,一点一点来吧,基础必须要打好(自己给自己好的心里暗示,结局一定是好的) 今日份励志文案:慢慢改变,慢慢…...
如何计算掩膜图中多个封闭图形的面积
import cv2def calMaskArea(image,idx):mask cv2.inRange(image, idx, idx)contours, hierarchy cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)for contour in contours:area cv2.contourArea(contour)print("图形的面积为", area) image是…...
【Nginx】CentOS 安装Nignx
CentOS上安装Nginx: 1. 打开终端:使用SSH或者直接在服务器上打开终端。 2. 更新系统:运行以下命令以确保您的系统软件包列表是最新的: sudo yum update3. 安装Nginx:运行以下命令以安装Nginx: sudo yum…...
idea 代码快捷键Mac版
1、查询任何东西 双击 Shift2、文件内查找 Command F 3、文件内替换 Command R4、全局查找(根据路径) Command Shift F5、在当前文件跳转到某一行的指定处 Command L6、退回 / 前进到上一个操作的地方 Command Option 方向键左Command Opt…...
【NI-DAQmx入门】多通道数据采集
1.通道扩展解释 通道扩展是扩展数据采集设备的通道以包含另一个设备的通道的过程,从而有效地创建具有更多通道的任务。当使用通道扩展时,DAQmx 自动在 DAQmx 驱动程序级别路由触发器和时钟,以便多个设备同步。为了使设备作为一个整体运行&…...
回顾 — SFA:简化快速 AlexNet(模糊分类)
模糊图像的样本 一、说明 在本文回顾了基于深度学习的模糊图像分类(SFA)。在本文中:Simplified-Fast-AlexNet (SFA)旨在对图像是否因散焦模糊、高斯模糊、雾霾模糊或运动模糊而模糊进行分类。 二、大纲 图像模糊建模简要概述简化快速 AlexNet…...
基于51单片机PCF8591数字电压表数码管显示设计( proteus仿真+程序+设计报告+讲解视频)
PCF8591数字电压表数码管显示 1.主要功能:讲解视频:2.仿真3. 程序代码4. 设计报告5. 设计资料内容清单&&下载链接资料下载链接(可点击): 基于51单片机PCF8591数字电压表数码管设计( proteus仿真程序设计报告讲…...
分发饼干(贪心算法+图解)
455. 分发饼干 - 力扣(LeetCode) 题目描述 假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。 对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最…...
vue项目路由使用history模式,nginx配置,刷新页面显示404
需要在配置项中添加 try_files $uri $uri/ /index.html;...
redis的redis.service配置
在CentOS中,可以使用以下步骤配置redis.service: 创建redis用户和组 在终端中执行以下命令: 复制插入 sudo useradd -r -s /bin/false redis复制插入 这将创建一个名为redis的系统用户,并禁止该用户登录系统。 安装Redis 在…...
高频SQL50题(基础版)-3
文章目录 主要内容一.SQL练习题1.1174-即时食物配送代码如下(示例): 2.550-游戏玩法分析代码如下(示例): 3.2356-每位教师所教授的科目种类的数量代码如下(示例): 4.1141-查询近30天活跃用户数代码如下&…...
装饰模式(Decorator Pattern)重构java邮件发奖系统实战
前言 现在我们有个如下的需求,设计一个邮件发奖的小系统, 需求 1.数据验证 → 2. 敏感信息加密 → 3. 日志记录 → 4. 实际发送邮件 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其…...
iPhone密码忘记了办?iPhoneUnlocker,iPhone解锁工具Aiseesoft iPhone Unlocker 高级注册版分享
平时用 iPhone 的时候,难免会碰到解锁的麻烦事。比如密码忘了、人脸识别 / 指纹识别突然不灵,或者买了二手 iPhone 却被原来的 iCloud 账号锁住,这时候就需要靠谱的解锁工具来帮忙了。Aiseesoft iPhone Unlocker 就是专门解决这些问题的软件&…...
vue3 字体颜色设置的多种方式
在Vue 3中设置字体颜色可以通过多种方式实现,这取决于你是想在组件内部直接设置,还是在CSS/SCSS/LESS等样式文件中定义。以下是几种常见的方法: 1. 内联样式 你可以直接在模板中使用style绑定来设置字体颜色。 <template><div :s…...
04-初识css
一、css样式引入 1.1.内部样式 <div style"width: 100px;"></div>1.2.外部样式 1.2.1.外部样式1 <style>.aa {width: 100px;} </style> <div class"aa"></div>1.2.2.外部样式2 <!-- rel内表面引入的是style样…...
uniapp微信小程序视频实时流+pc端预览方案
方案类型技术实现是否免费优点缺点适用场景延迟范围开发复杂度WebSocket图片帧定时拍照Base64传输✅ 完全免费无需服务器 纯前端实现高延迟高流量 帧率极低个人demo测试 超低频监控500ms-2s⭐⭐RTMP推流TRTC/即构SDK推流❌ 付费方案 (部分有免费额度&#x…...
大学生职业发展与就业创业指导教学评价
这里是引用 作为软工2203/2204班的学生,我们非常感谢您在《大学生职业发展与就业创业指导》课程中的悉心教导。这门课程对我们即将面临实习和就业的工科学生来说至关重要,而您认真负责的教学态度,让课程的每一部分都充满了实用价值。 尤其让我…...
关键领域软件测试的突围之路:如何破解安全与效率的平衡难题
在数字化浪潮席卷全球的今天,软件系统已成为国家关键领域的核心战斗力。不同于普通商业软件,这些承载着国家安全使命的软件系统面临着前所未有的质量挑战——如何在确保绝对安全的前提下,实现高效测试与快速迭代?这一命题正考验着…...
听写流程自动化实践,轻量级教育辅助
随着智能教育工具的发展,越来越多的传统学习方式正在被数字化、自动化所优化。听写作为语文、英语等学科中重要的基础训练形式,也迎来了更高效的解决方案。 这是一款轻量但功能强大的听写辅助工具。它是基于本地词库与可选在线语音引擎构建,…...
Mysql8 忘记密码重置,以及问题解决
1.使用免密登录 找到配置MySQL文件,我的文件路径是/etc/mysql/my.cnf,有的人的是/etc/mysql/mysql.cnf 在里最后加入 skip-grant-tables重启MySQL服务 service mysql restartShutting down MySQL… SUCCESS! Starting MySQL… SUCCESS! 重启成功 2.登…...
DingDing机器人群消息推送
文章目录 1 新建机器人2 API文档说明3 代码编写 1 新建机器人 点击群设置 下滑到群管理的机器人,点击进入 添加机器人 选择自定义Webhook服务 点击添加 设置安全设置,详见说明文档 成功后,记录Webhook 2 API文档说明 点击设置说明 查看自…...

