日期类相关练习题

简介: 日期类相关练习题

前言

本文记录一些有关日期类的oj题题解,实现过日期类小项目的可以练一下手,本文不做过多讲解.

一、求1+2+3+…+n

题目来源于:牛客

题目链接:传送门

题目介绍:

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


示例1:

输入:5
返回值:15

示例2:

输入:1
返回值:1

解题思路:

利用每次创建对象会自动调用构造函数,创建两个静态成员变量.

变量1:count每次调用构造函数后+1.

变量2:sum记录每次cout相加的结果.

创建一个大小为n的对象数组,这样就会创建n个对象,调用n次构造函数.

代码实现:

写法1:(友元类)

class small_Solution{
public:
    friend class Solution;
    small_Solution()
    {
        count++;
        sum+=count;
    }
private:
    static int count;
    static int sum;
};
class Solution {
public:
    int Sum_Solution(int n) {
        small_Solution a[n];
        return small_Solution::sum;
    }
};
int small_Solution::count=0;
int small_Solution::sum=0;

使用内部类:

class Solution {
public:
    int Sum_Solution(int n) {
        small_Solution a[n];
        return Solution::sum;
    }
    class small_Solution{//内部类
    public:
        small_Solution()
        {
        //内部类是外部类的天生友元,可直接使用外部类的静态成员变量
            count++;
            sum+=count;
        }
};
private:
    static int count;
     static int sum;
};
int Solution::count=0;
int Solution::sum=0;

二、计算日期到天数转换

题目来源于:牛客网

题目链接:传送门

题目介绍:

根据输入的日期,计算是这一年的第几天。

保证年份为4位数且日期合法。


输入描述:


输入一行,每行空格分割,分别是年,月,日


输出描述:


输出是这一年的第几天


示例1

输入:2012 12 31
输出:366

示例2

输入:1982 3 4
输出:63

解题思路:

1.先记录给出的day.

2.根据给出的year和month计算天数

3.将前面每个月的天数累加+day.

代码实现:

#include <iostream>
using namespace std;
int is_leap_year(int year) {
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        return 1;
    } else return 0;
}
int main() {
    int year = 0, moon = 0, day = 0;
    cin>>year>>moon>>day;
    int sum = day;
    int leap = is_leap_year(year);//判断是否是闰年
    int arr[13] = { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    for (int i = 1; i < moon; i++) {
        sum += arr[i];
        if (i == 3 && leap == 1) {
            sum += 1;
        }
    }
   cout<<sum;
   return 0;
}

三、日期累加

题目来源于:牛客

题目链接:传送门


题目介绍:

设计一个程序能计算一个日期加上若干天后是什么日期。


输入描述:


输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。


输出描述:


输出m行,每行按yyyy-mm-dd的个数输出。

解题思路:

这只是日期类中的一个接口而已,实现过日期类可以直接复用.

代码实现

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
class Date {
public:
    // 全缺省的构造函数
    Date(int year=2023, int month=1, int day=1) {
        _year = year;
        _month = month;
        _day = day;
    }
    // 获取某年某月的天数
    int GetMonthDay(int year, int month) {
        int day[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)) { //如果是闰年,且是2月
            day[2] = 29;
        }
        return day[month];
    }
    //打印日期类函数
    void Print() {
        printf("%d-%02d-%02d\n", _year, _month, _day);
    }
    // 日期+=天数
    Date& operator+=(int day) {
        if (day < 0) {
            return *this -= -day;//调用"-="的运算符重载
        }
        _day += day;
        while (_day > GetMonthDay(_year, _month)) { //如果超过当月天数
            _day -= GetMonthDay(_year, _month);
            _month++;
            if (_month > 12) {
                _month = 1;
                _year++;
            }
        }
        return *this;
    }
    Date& operator-=(int day) {
        if (day < 0) {
            return *this += -day;//调用"+="的运算符重载
        }
        _day -= day;
        while (_day <= 0) { //如果是负数
            _month--;
            if (_month <= 0) {
                _month = 12;
                _year--;
            }
            _day += GetMonthDay(_year, _month);
        }
        return *this;
    }
private:
    int _year;
    int _month;
    int _day;
};
int main()
{
    int num=0;
    cin>>num;
    while(num--)
    {
        int year=0,month=0,day=0,n=0;
        cin>>year>>month>>day>>n;
        Date d1(year,month,day);
        d1+=n;
        d1.Print();
    }
    return 0;
}

四、日期差值

题目来源于:牛客

题目链接:传送门

题目介绍

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天


输入描述:


有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD


输出描述:


每组数据输出一行,即日期差值


示例:

输入:

20110412
20110422

输出:

11

代码实现:

#include <iostream>
using namespace std;
class Date {
  public:
    //Date.cpp
    Date(int year, int month, int day) {
        _year = year;
        _month = month;
        _day = day;
    }
    int GetMonthDay(int year, int month) {
        int day[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)) { //如果是闰年,且是2月
            day[2] = 29;
        }
        return day[month];
    }
    bool operator>(const Date& d) {
        if (_year > d._year) { //如果年大
            return true;
        }
        {
            if (_year == d._year) {
                if (_month > d._month) { //年相同,月大
                    return true;
                } else {
                    if (_day > d._day) { //入如果年月相同,日大
                        return true;
                    }
                    return false;//月小,或者日小和相等
                }
            }
            return false;//如果年小
        }
    }
    // 日期-日期 返回天数
    int operator-(const Date& d) {
        //小的日期一直++,加到和大的日期一样时,加了多少次就差多少天
        Date max = *this;
        Date min = d;
        int flag = 1;
        int n = 0;
        while (min != max) { //用n统计相差多少天
            ++min;
            ++n;
        }
        return n * flag;
    }
    // 前置++
    Date& operator++() {
        _day += 1;
        while (_day > GetMonthDay(_year, _month)) { //如果超过当月天数
            _day -= GetMonthDay(_year, _month);//则减去当月的天数
            //月份向后推一个月
            _month++;
            if (_month > 12) {
                _month = 1;
                _year++;
            }
        }
        return *this;
    }
    bool operator==(const Date& d) {
        if (_year == d._year &&
                _month == d._month &&
                _day == d._day
           ) {
            return true;
        }
        return false;
    }
    // !=运算符重载
    bool operator != (const Date& d) {
        return !(*this == d);
    }
  private:
    int _year;
    int _month;
    int _day;
};
int main() {
    int year1 = 0, month1 = 0, day1 = 0;
    int year2 = 0, month2 = 0, day2 = 0;
    scanf("%4d%2d%2d", &year1, &month1, &day1);
    scanf("%4d%2d%2d", &year2, &month2, &day2);
    Date d1(year1, month1, day1);
    Date d2(year2, month2, day2);
    int ret = 0;
    if (d1 > d2) {
        ret = d1 - d2;
    } else {
        ret = d2 - d1;
    }
    cout<<ret+1;
    return 0;
}

五、打印日期

题目来源于:力扣

题目链接:传送门

题目介绍

给出年分m和一年中的第n天,算出第n天是几月几号。


输入描述:


输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。


输出描述:


可能有多组测试数据,对于每组数据, 按 yyyy-mm-dd的格式将输入中对应的日期打印出来。


示例:

输入:

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.cpp
Date(int year=2023 , int month=1, int day=0)
{
  _year = year;
  _month = month;
  _day = day;
  //这里也就体现出了,成员变量前面'_'的好处,方便与参数区分
}
    int GetMonthDay(int year, int month)
{
  int day[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))//如果是闰年,且是2月
  {
    day[2] = 29;
  }
  return day[month];
}
    // 日期+=天数
Date&  operator+=(int day)
{
  _day += day;
  while (_day > GetMonthDay(_year, _month))//如果超过当月天数
  {
    _day -= GetMonthDay(_year, _month);//通过调用GetMonthDay函数获取当月天数
    _month++;
    if (_month > 12)//月数超过12,则开始下一年
    {
      _month = 1;
      _year++;
    }
  }
  return *this;
}
    void print()
    {
        printf("%04d-%02d-%02d",_year,_month,_day);
    }
private:
  int _year;
  int _month;
  int _day;
};
int main() {
    int year=0,month=0,day=0;
    while(scanf("%d%d",&year,&day)==2)
    {
        Date d1(year);
        d1+=day;
        d1.print();
        cout<<endl;
    }
    return 0;
}

本篇文章只是记录刷题,并没有过多讲解,建议实现一下日期类.

目录
相关文章
|
18天前
|
人工智能 数据可视化 API
看完《疯狂动物城》心痒痒?试试ComfyUI,让朱迪和尼克走进你的画布
看完《疯狂动物城》意犹未尽?用ComfyUI+Flux文生图模型,让朱迪和尼克跃然纸上!通过节点式工作流精准控制生成细节,还原动画级质感。毛发、表情、服饰皆栩栩如生,支持风格定制与角色一致性强的图像创作。无需高配硬件,Lab4AI平台一键部署,轻松实现你的创意构想。Anyone can create anything!
316 1
看完《疯狂动物城》心痒痒?试试ComfyUI,让朱迪和尼克走进你的画布
|
7月前
|
存储 5G 测试技术
时钟同步测试校验仪的应用介绍
时间同步测试仪是一种高精度、高可靠性的设备,用于测量和评估时间同步系统的性能。它广泛应用于电力系统(如电网调度、继电保护)、通信网络(如5G基站、光传输网络)、铁路交通(如列车运行控制、信号系统)、工业自动化(如生产线、控制系统)以及科学研究(如天文观测、粒子物理实验)等领域。其功能包括高精度时间测量、多信号接口支持、自动测量与分析、数据存储导出及性能评估输出,确保各领域设备间的时间同步精度与稳定性,保障系统高效运行。
|
监控 应用服务中间件 定位技术
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
1069 3
|
SQL 存储 Java
完整java开发中JDBC连接数据库代码和步骤
该博客文章详细介绍了使用JDBC连接数据库的完整步骤,包括加载JDBC驱动、提供连接URL、创建数据库连接、执行SQL语句、处理结果以及关闭JDBC对象的过程,并提供了相应的示例代码。
|
机器学习/深度学习 人工智能 算法
基于Python深度学习的【垃圾识别系统】实现~TensorFlow+人工智能+算法网络
垃圾识别分类系统。本系统采用Python作为主要编程语言,通过收集了5种常见的垃圾数据集('塑料', '玻璃', '纸张', '纸板', '金属'),然后基于TensorFlow搭建卷积神经网络算法模型,通过对图像数据集进行多轮迭代训练,最后得到一个识别精度较高的模型文件。然后使用Django搭建Web网页端可视化操作界面,实现用户在网页端上传一张垃圾图片识别其名称。
520 0
基于Python深度学习的【垃圾识别系统】实现~TensorFlow+人工智能+算法网络
|
存储 数据库 Python
Loguru:Python中强大的日志库
Loguru:Python中强大的日志库
969 2
|
搜索推荐 数据挖掘 大数据
数据具有无限性、易复制性、非均质性、易腐性和原始性五大特征
数据具有无限性、易复制性、非均质性、易腐性和原始性五大特征
992 1
|
存储 弹性计算 安全
阿里云服务器怎么样?性能测评和使用教程
阿里云服务器安全可靠、弹性可伸缩,CPU可选256核、内存选到3072GB,云服务器ECS规格通用型、计算型、内存型、通用算力型、裸金属、GPU、大数据等ECS实例规格,公网带宽可选到200M,绑定弹性公网EIP带宽可达1000M,共享带宽可达20000M,阿里云百科来详细说下阿里云服务器怎么样?云服务器性能评测以及使用教程:
590 0
阿里云服务器怎么样?性能测评和使用教程
|
安全 关系型数据库 MySQL
MySQL安全与权限管理:保障数据安全与访问控制
本文深入探讨了MySQL数据库的安全与权限管理,通过详细的代码示例,介绍了用户与权限的概念,权限管理与访问控制的方法,以及数据库安全性策略的制定与实施。MySQL提供了强大的安全性功能,能够帮助管理员保护数据库的数据安全和限制用户的访问权限。了解如何创建用户、授予权限,以及如何制定数据库安全性策略,将使管理员能够有效地管理和保护数据库,降低潜在的安全风险。
2770 0