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

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练习(每日练习!)

编程题&#xff1a; 题一&#xff1a;计算日期到天数的转换 计算日期到天数转换_牛客题霸_牛客网 (nowcoder.com) 示例1 输入&#xff1a; 2012 12 31 输出&#xff1a; 366 思路一&#xff1a; 第一步&#xff1a;创建年&#xff0c;月&#xff0c;日的变量&#xff0c;并按…...

GPTs Hunter 是什么?

原文&#xff1a; https://openaigptguide.com/openai-gpts-hunter/ GPTs Hunter 是一个功能强大的免费导航网站&#xff0c;支持多语言&#xff0c;提供用户友好的界面。 GPTs Hunter&#xff1a;功能强大的免费导航网站 GPTs Hunter是一个功能强大的免费导航网站&#xff…...

【移远QuecPython】EC800M物联网开发板的硬件TIM定时器精准延时

【移远QuecPython】EC800M物联网开发板的硬件TIM定时器精准延时 文章目录 导入库定时器初始化延时函数定时中断回调调用函数打包附录&#xff1a;列表的赋值类型和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博士学位全额奖学金项目)

时间&#xff1a;2023 年11月16日&#xff08;星期四&#xff09;14:30 地点&#xff1a;合肥工业大学 学术会议中心三楼报告厅 主讲嘉宾&#xff1a;陈模军 助理教授 https://facultyprofiles.hkust-gz.edu.cn/faculty-personal-page/CHEN-Mojun/mjchen 报名表直达&#xff1…...

hugeGraph修改PropertyKey属性类型

修改PropertyKey字段属性的类型&#xff0c;发现没办法保留数据的前提下修改&#xff0c;智能是先删除数据&#xff0c;然后再修改&#xff0c;或者备份后修改再恢复。 方法一、 修改groovy脚本中的Text为Int后重新建元数据 schema.propertyKey(“youkey”).asText().valueSing…...

vscode 访问本地或者远程docker环境

1、vscode 访问docker本地环境 直接点击左下角连接图标&#xff0c;弹出选项可以选择容器&#xff0c;只要容器在本地运行者&#xff0c;选择attach可以看到运行中的容器可以选择&#xff0c;选择其中需要选择的就行 ## 运行容器&#xff0c;可以-d后台运行都可以 docker run…...

人工智能与充电技术:携手共创智能充电新时代

人工智能与充电技术&#xff1a;携手共创智能充电新时代 摘要&#xff1a;本文探讨了人工智能与充电技术的结合及其在未来充电设施领域的应用。通过分析智能充电系统的技术原理、优势以及挑战&#xff0c;本文展望了由人工智能驱动的充电技术为未来电动交通带来的巨大变革与机…...

『自定义B站视频播放速度』

哔哩哔哩 的最高播放速度是 2.0&#xff0c; 但对于我们这种程序员来说&#xff0c;2.0 速度观看学习视频还是稍微慢了点&#xff0c; &#x1faf5;&#x1f3fb;3.0 以上才是王道&#x1faf5;&#x1f3fb;&#xff0c; 下面就是具体的操作方法&#xff1a; ① 在浏览器…...

Java入门篇 之 继承

本篇碎碎念&#xff1a;最近的课程遇到瓶颈了&#xff0c;看的时候感觉自己会了&#xff0c;但是结束仔细一回顾还是一知半解&#xff0c;一点一点来吧&#xff0c;基础必须要打好(自己给自己好的心里暗示&#xff0c;结局一定是好的) 今日份励志文案:慢慢改变&#xff0c;慢慢…...

如何计算掩膜图中多个封闭图形的面积

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&#xff1a; 1. 打开终端&#xff1a;使用SSH或者直接在服务器上打开终端。 2. 更新系统&#xff1a;运行以下命令以确保您的系统软件包列表是最新的&#xff1a; sudo yum update3. 安装Nginx&#xff1a;运行以下命令以安装Nginx&#xff1a; sudo yum…...

idea 代码快捷键Mac版

1、查询任何东西 双击 Shift2、文件内查找 Command F 3、文件内替换 Command R4、全局查找&#xff08;根据路径&#xff09; Command Shift F5、在当前文件跳转到某一行的指定处 Command L6、退回 / 前进到上一个操作的地方 Command Option 方向键左Command Opt…...

【NI-DAQmx入门】多通道数据采集

1.通道扩展解释 通道扩展是扩展数据采集设备的通道以包含另一个设备的通道的过程&#xff0c;从而有效地创建具有更多通道的任务。当使用通道扩展时&#xff0c;DAQmx 自动在 DAQmx 驱动程序级别路由触发器和时钟&#xff0c;以便多个设备同步。为了使设备作为一个整体运行&…...

回顾 — SFA:简化快速 AlexNet(模糊分类)

模糊图像的样本 一、说明 在本文回顾了基于深度学习的模糊图像分类&#xff08;SFA&#xff09;。在本文中&#xff1a;Simplified-Fast-AlexNet (SFA)旨在对图像是否因散焦模糊、高斯模糊、雾霾模糊或运动模糊而模糊进行分类。 二、大纲 图像模糊建模简要概述简化快速 AlexNet…...

基于51单片机PCF8591数字电压表数码管显示设计( proteus仿真+程序+设计报告+讲解视频)

PCF8591数字电压表数码管显示 1.主要功能&#xff1a;讲解视频&#xff1a;2.仿真3. 程序代码4. 设计报告5. 设计资料内容清单&&下载链接资料下载链接&#xff08;可点击&#xff09;&#xff1a; 基于51单片机PCF8591数字电压表数码管设计( proteus仿真程序设计报告讲…...

分发饼干(贪心算法+图解)

455. 分发饼干 - 力扣&#xff08;LeetCode&#xff09; 题目描述 假设你是一位很棒的家长&#xff0c;想要给你的孩子们一些小饼干。但是&#xff0c;每个孩子最多只能给一块饼干。 对每个孩子 i&#xff0c;都有一个胃口值 g[i]&#xff0c;这是能让孩子们满足胃口的饼干的最…...

vue项目路由使用history模式,nginx配置,刷新页面显示404

需要在配置项中添加 try_files $uri $uri/ /index.html;...

redis的redis.service配置

在CentOS中&#xff0c;可以使用以下步骤配置redis.service&#xff1a; 创建redis用户和组 在终端中执行以下命令&#xff1a; 复制插入 sudo useradd -r -s /bin/false redis复制插入 这将创建一个名为redis的系统用户&#xff0c;并禁止该用户登录系统。 安装Redis 在…...

高频SQL50题(基础版)-3

文章目录 主要内容一.SQL练习题1.1174-即时食物配送代码如下&#xff08;示例&#xff09;: 2.550-游戏玩法分析代码如下&#xff08;示例&#xff09;: 3.2356-每位教师所教授的科目种类的数量代码如下&#xff08;示例&#xff09;: 4.1141-查询近30天活跃用户数代码如下&…...

超轻量级OpenClaw与LaTeX结合:学术文档自动化处理

超轻量级OpenClaw与LaTeX结合&#xff1a;学术文档自动化处理 科研工作者每天需要处理大量的文献整理、公式编辑和文档排版工作&#xff0c;传统手动方式耗时且容易出错。本文将展示如何用超轻量级OpenClaw实现学术文档的自动化处理&#xff0c;让LaTeX文档编写变得轻松高效。 …...

3分钟搭建免费B站视频解析服务:零基础教程

3分钟搭建免费B站视频解析服务&#xff1a;零基础教程 【免费下载链接】bilibili-parse bilibili Video API 项目地址: https://gitcode.com/gh_mirrors/bi/bilibili-parse 你是否曾经想要保存B站的精彩视频却不知道如何操作&#xff1f;或者需要在自己的网站上嵌入B站视…...

Phi-4-mini-reasoning一文详解:专为多步推理设计的开源大模型实战

Phi-4-mini-reasoning一文详解&#xff1a;专为多步推理设计的开源大模型实战 1. 模型概述 Phi-4-mini-reasoning是一款专注于推理任务的文本生成模型&#xff0c;特别擅长处理需要多步分析的复杂问题。与通用聊天模型不同&#xff0c;它被设计用来解决数学题、逻辑题等需要逐…...

Linux系统管理必备:常用命令在Phi-3-vision模型部署与运维中的应用

Linux系统管理必备&#xff1a;常用命令在Phi-3-vision模型部署与运维中的应用 1. 前言&#xff1a;为什么需要掌握这些命令 部署和管理AI模型服务时&#xff0c;熟练使用Linux命令就像拥有了一把瑞士军刀。特别是对于Phi-3-vision这样的视觉大模型&#xff0c;从查看日志到监…...

OCRmyPDF技术解构:3大创新点与制造业/法律服务效能优化实践

OCRmyPDF技术解构&#xff1a;3大创新点与制造业/法律服务效能优化实践 【免费下载链接】OCRmyPDF OCRmyPDF adds an OCR text layer to scanned PDF files, allowing them to be searched 项目地址: https://gitcode.com/GitHub_Trending/oc/OCRmyPDF 一、技术内核&…...

Qwen3-ASR-0.6B效果展示:金融客服录音(专业术语+缩略语)识别术语表匹配

Qwen3-ASR-0.6B效果展示&#xff1a;金融客服录音&#xff08;专业术语缩略语&#xff09;识别术语表匹配 金融客服电话录音里&#xff0c;客户和坐席的对话常常像在说“天书”。一会儿是“LPR”&#xff0c;一会儿是“T0”&#xff0c;还有各种产品代码和内部术语。把这些录音…...

CHORD-X深度研究报告生成:集成MySQL进行数据存储与管理的配置指南

CHORD-X深度研究报告生成&#xff1a;集成MySQL进行数据存储与管理的配置指南 如果你正在使用CHORD-X这类强大的研究报告生成工具&#xff0c;可能会遇到一个甜蜜的烦恼&#xff1a;生成的内容越来越多&#xff0c;数据越来越杂&#xff0c;怎么才能把它们管得井井有条&#x…...

Gurobi优化求解器状态码全解析:从model.status到对偶变量获取

Gurobi优化求解器状态码深度实战指南 当你在深夜调试一个复杂的供应链优化模型时&#xff0c;控制台突然弹出"STATUS: 3"的提示——这意味着什么&#xff1f;该如何快速定位问题&#xff1f;又该如何提取关键诊断信息&#xff1f;作为数学优化领域的工业级求解器&…...

foobar2000 DUI界面深度解析:foobox-cn技术架构与实战配置完整指南

foobar2000 DUI界面深度解析&#xff1a;foobox-cn技术架构与实战配置完整指南 【免费下载链接】foobox-cn DUI 配置 for foobar2000 项目地址: https://gitcode.com/GitHub_Trending/fo/foobox-cn foobox-cn是针对foobar2000播放器开发的现代化DUI&#xff08;默认用户…...

Vue3+Vant4移动端架构设计:现代化移动应用工程实践

Vue3Vant4移动端架构设计&#xff1a;现代化移动应用工程实践 【免费下载链接】vue3-vant4-mobile &#x1f44b;&#x1f44b;&#x1f44b; 基于Vue3.2、vite3、vant4、pinia2、Typescript、windicss 等主流技术开发&#xff0c;集成 Dark Mode(暗黑)模式和系统主题色&#x…...