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

【C++】日期类基础题

                                个人主页:CSDN_小八哥向前冲~

                                 所属专栏:C++入门


一些C++基础题目,帮你巩固一下!

目录

关于内存问题

栈和堆基础问题

计算日期到天数的转换

日期差值

日期累加

打印日期


关于内存问题

答案:D     B

第一题:

classA构造了五次,但是代码是delete pclassa,而不是delete [ ]pclassa,只会析构一次,但往往会引发程序崩溃!

第二题:

由于是内置类型,delete 相当于free,所有并不会造成内存泄漏,但是如果是自定义类型,就会程序崩溃!所以不建议!

栈和堆基础问题

答案:C     D

第一题:

A.栈区主要存在局部变量和函数参数,其空间的管理由编译器自动完成,无需手动控制,堆区是自己申请的空间,在不需  要时需要手动释放

B.栈区先定义的变量放到栈底,地址高,后定义的变量放到栈顶,地址低,因此是向下生长的,堆区则相反.

C.频繁的申请空间和释放空间,容易造成内存碎片,甚至内存泄漏,栈区由于是自动管理,不存在此问题.

D.32位系统下,最大的访问内存空间为4G,所以不可能把所有的内存空间当做堆内存使用,故错误.

第二题:

A.堆大小受限于操作系统,而栈空间一般有系统直接分配

B.频繁的申请空间和释放空间,容易造成内存碎片,甚至内存泄漏,栈区由于是自动管理,不存在此问题

C.堆无法静态分配,只能动态分配

D.栈可以通过函数_alloca进行动态分配,不过注意,所分配空间不能通过free或delete进行释放

计算日期到天数的转换

题目:【牛客】日期到天数转换

思路:

比如日期:2012-12-31,我们可以将2012-1-01一直累加到2012-12-31,同时用变量记录差值,就能得到这个日期是这一年的第几天。

但是又有闰年平年的问题,所有我们可以用一个数组管理每个月的天数!

代码:

#include <iostream>
using namespace std;

int main() {
	int year = 0, month = 0, day = 0,sum=0;
	cin >> year >> month >> day;
    int GetMonthday[13]={0,31,28,31,30,31,
    30,31,31,30,31,30,31};
    //判断闰年
    if(year%4==0&&year%100!=0||year%400==0)
        GetMonthday[2]=29;
    //开始累加    
    for(int i=0;i<month;i++)
        sum+=GetMonthday[i];
    sum+=day;
    cout<<sum;
    return 0;
}

日期差值

题目:【牛客】日期差值

思路:

给定了两个日期,让我们求它们之间的差值天数,我们可以将小的日期一直累加到大的日期,同时用变量记录差值,就能得到这个差值天数。

题目输入的是一串数字,我们可以用除和取模的方法得到年月日。

写一个日期类,重载减法,加加等函数。

代码:

#include <iostream>
#include <iterator>
#include <linux/limits.h>
using namespace std;

class Date
{
public:
    friend Date DateCheck(int date);

    Date(int year=0,int month=0,int day=0)
        :_year(year)
        ,_month(month)
        ,_day(day)
        {}
    int GetMonthDay()
    {
        int MonthDay[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 MonthDay[_month];
    }
    bool operator==(Date d)
    {
        return _year==d._year&&_month==d._month&&_day==d._day;
    }
    bool operator!=(Date d)
    {
        return !(*this==d);
    }
    bool operator>(Date d)
    {
        if(_year>d._year) return true;
        else if(_year==d._year)
        {
            if(_month>d._month) return true;
            else if(_month==d._month)
            {
                if(_day>d._day) return true;
            }
        }
        return false;
    }
    Date& operator++()
    {
        _day++;
        if(_day>GetMonthDay())
        {
            _month++;
            _day=1;
            if(_month==13)
            {
                _year++;
                _month=1;
            }
        }
        return *this;
    }
    int operator-(Date d)
    {
        Date max=*this,min=d;
        int sum=0,flage=1;
        if(d>*this) 
        {
            flage=-1;
            max=d;
            min=*this;
        }
        while(min!=max)
        {
            ++min;
            sum++;
        }
        return sum;
    }
private:
    int _year;
    int _month;
    int _day;
};
Date DateCheck(int date)
{
    Date d;
    d._year=date/10000;
    d._day=date%100;
    d._month=date/100%100;
    return d;
}
int main() {
    int date1,date2;
    cin>>date1>>date2;
    Date d1=DateCheck(date1);
    Date d2=DateCheck(date2);
    cout<<d2-d1+1;
    return 0;
}

日期累加

题目:【牛客】日期累加

思路:

给定一个日期,让其加一个天数,计算出新的日期,我们可以将这个日期的日加这个天数,再去将这个新日期计算成合法。

但是又有闰年平年的问题,所有我们可以用一个数组管理每个月的天数!

如果这个新的日期这个月的天数大于合法天数,那么就进行月进位,如果月进位到大于12,就进行年进位,重复操作,直到日期合法。

代码:

#include <iostream>
using namespace std;

int GetMonthDay(int year, int month) {
    int MonthDay[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 MonthDay[month];
}
int main() {
    int m, year, month, day, k;
    cin >> m;
    while (m--) {
        cin >> year >> month >> day >> k;
        day += k;
        while (day > GetMonthDay(year, month)) {
            day -= GetMonthDay(year, month);
            month++;
            if (month == 13) {
                year++;
                month = 1;
            }
        }
        if (month < 10 && day < 10)
            cout << year << "-" << 0 << month << "-" << 0 << day<<endl;
        else if (month < 10)
            cout << year << "-" << 0 << month << "-" << day<<endl;
        else if (day < 10)
            cout << year << "-" << month << "-" << 0 << day<<endl;
        else
            cout << year << "-" << month << "-" << day<<endl;
        

    }
    return 0;
}

打印日期

题目:【牛客】打印日期

思路:

给定一个年份和天数,计算这个年份的日期。

同样的道理,我们就是要将这个日期合法话而已,和上一道题异曲同工之处,不做过多介绍!

只要注意一下输出形式而已!

代码:

#include <iostream>
using namespace std;

int GetMonthDay(int year, int month) {
    int MonthDay[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 MonthDay[month];
}

int main() {
    int year,n;
    while (cin >> year >> n) {
        int month = 1, day = n;
        while (day > GetMonthDay(year, month)) {
            day -= GetMonthDay(year, month);
            month++;
            if (month == 13) {
                year++;
                month = 1;
            }
        }
        if (month < 10 && day < 10)
            cout << year << "-" << 0 << month << "-" << 0 << day << endl;
        else if (month < 10)
            cout << year << "-" << 0 << month << "-" << day << endl;
        else if (day < 10)
            cout << year << "-" << month << "-" << 0 << day << endl;
        else
            cout << year << "-" << month << "-" << day << endl;

    }
    return 0;
}

我们下期见哦!


http://www.kler.cn/news/309255.html

相关文章:

  • 笔记整理—内核!启动!—kernel部分(6)buxybox详解
  • 视觉检测中的深度学习应用
  • vue3 ref的用法及click事件的说明
  • 使用 uni-app 开发微信小程序的详细指南
  • go mod文件为啥又两个require
  • C#使用TCP-S7协议读写西门子PLC(四)
  • Qt常用控件——QDateTimeEdit
  • 【华为OD】2024D卷——生成哈夫曼树
  • CAD图纸加密软件哪个好?10款2024主流CAD图纸加密软件分享!
  • 如何利用Samba跨平台分享Ubuntu文件夹
  • 电路设计学习(一)
  • 【Day14-单例设计模式动态代理】
  • 一文吃透JVM面试八股文
  • 每日学习一个数据结构-DFA确定有限状态机
  • 【linux】VisiData:强大的命令行数据处理工具
  • 跟李沐学AI:序列到序列seq2seq
  • 本地部署大模型并使用知识库Windows下Ollama+Docker+MaxKB安装的记录
  • 影刀RPE学习——自动化
  • 地大信息-基础信息平台 GetImg 任意文件读取漏洞复现
  • http和https分别是什么?区别是什么?
  • GO GIN SSE DEMO
  • Springboot项目打war包运行及错误解决
  • SpringCloud Alibaba入门简介
  • 最优化理论与自动驾驶(一):概述
  • 你认为嵌入式软件开发的尽头是什么?
  • 了解 React 应用程序中的渲染和重新渲染:它们如何工作以及如何优化它们
  • NEXT.js 中间件 NextResponse.redirect 无效
  • 2576. 求出最多标记下标(24.9.12)
  • 【C/C++】涉及string类的经典OJ编程题
  • Mina protocol - 体验教程