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

日期类的习题

一.求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)

求1+2+3+...+n_牛客题霸_牛客网

#include <utility>
class Sum
{
public:
Sum()
{i+=t;t++;
}static int i;static int t;
};
int Sum::i=0;
int Sum::t=1;class Solution
{
public:int Sum_Solution(int n) {Sum*p=new Sum[n];delete []p ;return Sum::i;}};

二.日期差值

日期差值_牛客题霸_牛客网/activity/oj&qru=/ta/sju-kaoyan/question-ranking

#include <climits>
#include <iostream>
using namespace std;
class Date 
{
friend ostream& operator<<(ostream& out, const Date& d);
friend Date GetDate(int n);private:int _year;int _month;int _day;public:int GetMonthDay(int year,int month){static int GetMonth[13]={-1,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 GetMonth[month];}}int operator-(const Date& d) const;Date& operator++();bool operator<(const Date& d) const;bool operator==(const Date& d) const;bool operator!=(const Date& d) const;Date& operator+=(int day);
};
bool Date::operator<(const Date& d) const{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& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this;
}
Date& Date::operator++()
{*this+=1;return *this;
}
bool Date::operator==(const Date& d) const
{return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator!=(const Date& d) const
{return !(*this==d);
}int Date::operator-(const Date& d) const
{int n = 0;Date max = *this;Date min = d;if (*this < d){min = *this;max = d;}while (min != max){++min;n++;}return n;
}
Date GetDate(int n)
{Date a;a._year=n/10000;a._month=(n - a._year * 10000) / 100;a._day=n%100;return a;
}int main()
{int n1,n2;Date d1;Date d2;cin>>n1>>n2;d1=GetDate(n1);d2=GetDate(n2);cout<<d1-d2+1<<endl;
}

三.计算一年的第几天

计算日期到天数转换_牛客题霸_牛客网

#include <ctime>
#include <iostream>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:private:int _year;int _day;int _month;
};int GetMonthDay(int year, int month){static int monthDay[13] = { -1,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 monthDay[month];}}
ostream& operator<<(ostream& out, const Date& d)
{int n=0;for(int i= d._month;i>1;i--){n+=GetMonthDay(d._year,i);}n+=d._day;out<< n <<endl;return out;
}
istream& operator>>(istream& in, Date& d)
{	in >>  d._year >>  d._month >> d._day;return in;
}
int main()
{Date a;cin>>a;cout<<a;
}

四.累加天数

日期累加_牛客题霸_牛客网

#include <climits>
#include <iostream>
using namespace std;
class Date
{friend ostream& operator<<(ostream& out, const Date& d);friend Date Getmonth(int year,int month,int day);
private:int _year;int _month;int _day;
public:int GetMonthDay(int year, int month){static int GetMonth[13] = { -1,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 GetMonth[month];}}Date& operator+=(int day);Date operator+(int day);};Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this;
}Date Date::operator+(int day)
{Date tmp=*this;tmp+=day;return tmp;}
ostream& operator<<(ostream& out,  const Date& d)
{if(d._month<10 && d._day<10){out<<d._year<<"-"<<0<<d._month<<"-"<<0<<d._day;}else if (d._month<10 && d._day>10){out<<d._year<<"-"<<0<<d._month<<"-"<<d._day;}else if(d._month>=10 && d._day<10){out<<d._year<<"-"<<d._month<<"-"<<0<<d._day;}elseout<<d._year<<"-"<<d._month<<"-"<<d._day;return out;
}Date Getmonth(int year,int month,int day)
{Date b;b._year=year;b._month=month;b._day=day;return b;
}int main()
{int m,year,day,month,t;cin>>m;while(m--){cin>>year>>month>>day>>t;Date a;a=Getmonth(year,month,day);cout<<a+t<<endl;}}

五.打印日期

打印日期_牛客题霸_牛客网

#include <iostream>
using namespace std;
class Date 
{   friend ostream& operator<<(ostream& out, const Date& d);friend  Date Getday(int year,int num);private:int _year;int _month;int _day;public:  
};int GetMonthDay(int year, int month){static int GetMonth[13] = { -1,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 GetMonth[month];}} ostream& operator<<(ostream& out, const Date& d){if(d._month<10 && d._day<10)out<<d._year<<"-"<<0<<d._month<<"-"<<0<<d._day<<endl;else if(d._month<10 && d._day>=10){out<<d._year<<"-"<<0<<d._month<<"-"<<d._day<<endl;}else if(d._month>10 && d._day<10){out<<d._year<<"-"<<d._month<<"-"<<0<<d._day<<endl;}else{out<<d._year<<"-"<<d._month<<"-"<<d._day<<endl;}return out;}Date Getday(int year,int num){int month=1;for(int i=1;i<=12;i++){if(num<=GetMonthDay(year,i)){break;}else {num-=GetMonthDay(year,i);month++;if(month==13){year++;month=1;}}}Date a;a._year=year;a._month=month;a._day=num;return a;}
int main()
{int year,num;while((cin>>year>>num)){Date d;d=Getday(year,num);cout<<d;}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 2024华数杯C题解题思路、参考论文已出(无偿分享)~
  • [QT开发_音乐播放器项目笔记01]
  • PyFilesystem2 - Python 操作文件系统
  • Django中的模型小总结:
  • 双指针算法
  • 力扣-200.岛屿数量
  • 广州城市信息模型(CIM)白皮书学习
  • 【iOS】暑假第二周——网易云APP 仿写
  • 【TwinCAT3教程】IEC61131-3编程基础
  • Yolov8添加ConvNetV1和V2模块
  • PostgreSQL数据库内核(二):通过initdb传递guc参数
  • [安洵杯 2019]easy_web1
  • 微信小程序接口实现语音转文字
  • 牛客周赛 Round 54 (c++题解)
  • 使用Echarts来实现数据可视化
  • 网络传输文件的问题
  • 【前端学习】-粗谈选择器
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • Android系统模拟器绘制实现概述
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • happypack两次报错的问题
  • IDEA常用插件整理
  • java中的hashCode
  • JDK 6和JDK 7中的substring()方法
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 开源SQL-on-Hadoop系统一览
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 悄悄地说一个bug
  • 为视图添加丝滑的水波纹
  • 用Python写一份独特的元宵节祝福
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • #Ubuntu(修改root信息)
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (06)金属布线——为半导体注入生命的连接
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (附源码)ssm户外用品商城 毕业设计 112346
  • (亲测有效)解决windows11无法使用1500000波特率的问题
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (四)linux文件内容查看
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world
  • (一)十分简易快速 自己训练样本 opencv级联haar分类器 车牌识别
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .NET Core 2.1路线图
  • .net core 管理用户机密
  • .Net mvc总结
  • .net 提取注释生成API文档 帮助文档
  • .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)
  • @RequestBody与@RequestParam:Spring MVC中的参数接收差异解析
  • [AAuto]给百宝箱增加娱乐功能
  • [BT]BUUCTF刷题第4天(3.22)
  • [C/C++]_[初级]_[关于编译时出现有符号-无符号不匹配的警告-sizeof使用注意事项]
  • [C++] 从零实现一个ping服务
  • [ChromeApp]指南!让你的谷歌浏览器好用十倍!
  • [Datawhale AI夏令营 2024 第四期] 从零入门大模型微调之旅的总结