当前位置: 首页 > 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天活跃用户数代码如下&…...

Qwen3-Coder-30B-A3B-Instruct-FP8:终极代码模型对比分析指南

Qwen3-Coder-30B-A3B-Instruct-FP8&#xff1a;终极代码模型对比分析指南 【免费下载链接】Qwen3-Coder-30B-A3B-Instruct-FP8 项目地址: https://ai.gitcode.com/hf_mirrors/Qwen/Qwen3-Coder-30B-A3B-Instruct-FP8 在当今AI代码生成领域&#xff0c;Qwen3-Coder-30B-…...

除了ulimit -c unlimited:深入理解Linux core dump机制与高级配置指南

深入Linux核心转储&#xff1a;从基础配置到生产环境实战指南当服务器上的关键应用突然崩溃时&#xff0c;系统管理员最需要的就是一份完整的"事故现场记录"。Linux的core dump机制正是为此而生&#xff0c;它能保存程序崩溃时的内存状态、寄存器值和调用堆栈&#x…...

为什么92%的DeepSeek二次开发团队在6个月内遭遇交付延迟?——基于17个真实项目的技术债务归因分析

更多请点击&#xff1a; https://intelliparadigm.com 第一章&#xff1a;为什么92%的DeepSeek二次开发团队在6个月内遭遇交付延迟&#xff1f;——基于17个真实项目的技术债务归因分析 在对17个采用DeepSeek-R1/VL模型开展定制化开发的工业级项目进行回溯审计后&#xff0c;我…...

双系统Ubuntu磁盘告急?别重装!用GParted无损扩容保姆级教程(附U盘启动盘制作)

双系统Ubuntu磁盘告急&#xff1f;别重装&#xff01;用GParted无损扩容保姆级教程&#xff08;附U盘启动盘制作&#xff09;当你在Windows和Ubuntu双系统环境下工作时&#xff0c;是否遇到过这样的窘境&#xff1a;当初安装时给Ubuntu分配的空间捉襟见肘&#xff0c;而Windows…...

Qri高级功能:如何使用JSON Schema验证和描述数据集结构

Qri高级功能&#xff1a;如何使用JSON Schema验证和描述数据集结构 【免费下载链接】qri youre invited to a data party! 项目地址: https://gitcode.com/gh_mirrors/qr/qri Qri是一个强大的开源数据协作工具&#xff0c;它提供了丰富的功能来帮助用户管理、共享和验证…...

ZTE光猫工厂模式解锁:5分钟开启隐藏功能的终极指南

ZTE光猫工厂模式解锁&#xff1a;5分钟开启隐藏功能的终极指南 【免费下载链接】zteOnu A tool that can open ZTE onu device factory mode 项目地址: https://gitcode.com/gh_mirrors/zt/zteOnu 核心关键词&#xff1a;ZTE光猫工厂模式解锁 长尾关键词&#xff1a; ZT…...

什么情况下会核销贷款

贷款核销的核心前提是&#xff1a;贷款被认定为 “损失类” 且经 “穷尽追偿” 仍无法收回&#xff0c;银行按监管与会计规则从账面冲销&#xff0c;但债权不消灭、仍可追偿。一、核心认定条件&#xff08;满足其一即可&#xff09;破产 / 注销 / 吊销&#xff1a;借款人和担保…...

氘可来昔替尼常见副作用为鼻咽炎头痛及腹泻,如何应对

任何口服药物的临床价值&#xff0c;都必须在疗效与安全性的天平上找到精准的平衡点。氘可来昔替尼以PASI 75应答率的全面胜出证明了自己在银屑病治疗中的卓越地位&#xff0c;而其不良反应谱同样经过了严苛的临床验证。鼻咽炎、头痛和腹泻构成了这款药物最需关注的三大安全信号…...

保姆级教程:在Ubuntu上配置Frida环境,搞定Android App的IO重定向与签名绕过

在Ubuntu上构建Android逆向工程环境&#xff1a;Frida实战与IO重定向技术解析 对于习惯Linux环境的安全研究人员而言&#xff0c;Windows-centric的逆向工具链往往带来诸多不便。本文将系统性地介绍如何在Ubuntu上搭建完整的Android逆向环境&#xff0c;并深入探讨如何利用Frid…...

【大模型聚合平台深度评测:阿里云百炼 vs 腾讯云 ADP,企业如何选型?】

大模型聚合平台深度评测&#xff1a;阿里云百炼 vs 腾讯云 ADP&#xff0c;企业如何选型&#xff1f; 随着大模型技术的快速发展&#xff0c;越来越多的企业开始将 AI 能力融入到业务流程中。然而&#xff0c;面对市场上众多的大模型产品&#xff0c;企业往往面临着 “选择困难…...