C++:OJ练习(每日练习!)

news2024/10/5 11:26:01

编程题:

题一:计算日期到天数的转换

计算日期到天数转换_牛客题霸_牛客网 (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")

 

 本人实力有限可能对一些地方解释和理解的不够清晰,可以自己尝试读代码,或者评论区指出错误,望海涵!

感谢大佬们的一键三连! 感谢大佬们的一键三连! 感谢大佬们的一键三连!

                                              

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1209055.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

十年软件测试老程序告诉你性能测试的左移右移到底能干嘛

常规的性能测试一般都是在测试阶段集成测试时候才开始介入&#xff0c;很容易测试时间不够&#xff0c;可不可以借鉴测试左移右移的思路&#xff0c;更早的介入和发现性能风险&#xff0c;然后在测试阶段更专注于分析优化&#xff1f; 借着这个问题&#xff0c;结合自己的实践…

数据结构:反射

基本概念 反射中的四个类 Class类 Java文件在被编译之后&#xff0c;生成了.class文件&#xff0c;JVM此时解读.class文件&#xff0c;将其解析为java.lang.Class 对象&#xff0c;在程序运行时每个java文件就最终变成了Class类对象的一个实例。通过反射机制应用这个 实例就…

黑马点评回顾 redis实现共享session

文章目录 传统session缺点整体访问流程代码实现生成验证码登录 问题具体思路 传统session缺点 传统单体项目一般是把session存入tomcat&#xff0c;但是每个tomcat中都有一份属于自己的session,假设用户第一次访问第一台tomcat&#xff0c;并且把自己的信息存放到第一台服务器…

基于Vue+SpringBoot的天然气工程运维系统 开源项目

目录 一、摘要1.1 项目介绍1.2 项目详细录屏 二、功能模块2.1 系统角色分类2.2 核心功能2.2.1 流程 12.2.2 流程 22.3 各角色功能2.3.1 系统管理员功能2.3.2 用户服务部功能2.3.3 分公司&#xff08;施工单位&#xff09;功能2.3.3.1 技术员角色功能2.3.3.2 材料员角色功能 2.3…

计算机网络原理 谢希仁(第8版)第四章习题答案

4-01 网络层向上提供的服务有哪两种&#xff1f;试比较其优缺点。 面向连接的和无连接。 面向连接优点&#xff1a; 通过虚电路发送分组&#xff0c;分组只用填写虚电路编号&#xff0c;分组开销较小&#xff1b;分组按序达到终点。 面向连接缺点&#xff1a; 一个节点出故障&a…

【电源专题】低功耗设备如何解决POE协议要求的PD最小功耗?

要让PD正常工作起来除了需要与PSE握手协商外,还要求PD有一个最小功耗输出。 其原因是如果PD没有在一定时间内给出一个最小功耗,那么PSE将会认为PD设备断开而自动关闭,将功率分配给其他网口。对于不同的类别PD,其要求也不一样。如下所示为Type 1/2/2/4最小电流的要求:如类…

队列与二值信号量

一、队列简介&#xff1a;队列也称为消息队列&#xff0c;是一种用于消息间进行通信的数据结构&#xff0c;队列可以用于任务与任务之间、中断与任务之间传递消息&#xff0c;队列通常采用先进先出&#xff08;FIFO&#xff09;的数据缓冲机制。 二、队列常见的API函数 1.创建…

MySQL主主复制

主1 192.168.66.15 主2 192.168.66.16 主1&#xff1a; roottest2 ~]# hostname master1 [roottest2 ~]# bash [rootmaster1 ~]# vim /etc/my.cnf server-id11 log-binmysql-bin auto_increment_increment2 auto_increment_offset1 replicate-do-dbdemo_db …

STM32笔记—USART

课外知识插入&#xff1a;STM32单片机extern全局变量_stm32全局变量-CSDN博客 如果你把temple定义在A中&#xff0c;然后让A.h和B.h包含在includes.h中&#xff0c;然后把includes.h放在A.c和B.c中单个编译是没有问题的&#xff0c;但是链接的时候会出现问题&#xff0c; “S…

振南技术干货集:深入浅出的Bootloader(4)

注解目录 1、烧录方式的更新迭代 1.1 古老的烧录方式 (怀旧一下&#xff0c;单片机高压烧录器。) 1.2 ISP 与ICP 烧录方式 (还记得当年我们玩过的 AT89S51?) 1.3 更方便的 ISP 烧录方式 1.3.1串口 ISP &#xff08;是 STC 单片机成就了我们&#xff0c;还是我们成就了…

【开源】基于Vue.js的快递管理系统的设计和实现

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、研究内容2.1 数据中心模块2.2 快递类型模块2.3 快递区域模块2.4 快递货架模块2.5 快递档案模块 三、界面展示3.1 登录注册3.2 快递类型3.3 快递区域3.4 快递货架3.5 快递档案3.6 系统基础模块 四、免责说明 一、摘要 1.1 项目介绍 …

ARMday06(总线、串口、RCC章节分析)

总线 总线是完成各个部件之间传输的一种媒介 串行/并行总线 串行总线&#xff0c; 在同一时刻&#xff0c;根据时钟线的变化&#xff0c;只可以收发一位数据 优点&#xff1a;占用引脚资源少 缺点&#xff1a;传输速度比较慢 并行总线&#xff0c; 在同一时刻&#xff…

软件工程分析报告05体系结构说明书——基于Paddle的肝脏CT影像分割

基于Paddle的肝脏CT影像分割系统的体系结构说明书 目录 HIPO图 H图 Ipo图 软件结构图 面向数据流的体系结构设计图 程序流程图 S图 用PDL语言描述的伪代码 HIPO图 H图 Ipo图 软件结构图 面向数据流的体系结构设计图 程序流程图 S图 PAD图 用PDL语言描述的伪代码 (1)…

ubuntu20.04.6安装Intel AX211网卡驱动

前言 环境&#xff1a; ThinkBook16 2023 款网卡Intel AX211 Wi-Fi6ubuntu版本20.04.6&#xff08;最后一位小数很重要&#xff09;系统内核 Linux wzy 5.15.0-67-generic #74~20.04.1-Ubuntu SMP Wed Feb 22 14:52:34 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux 方法&#x…

【数据结构】树与二叉树(十五):二叉树的基础操作:查找结点(算法Find)

文章目录 5.2.1 二叉树二叉树性质引理5.1&#xff1a;二叉树中层数为i的结点至多有 2 i 2^i 2i个&#xff0c;其中 i ≥ 0 i \geq 0 i≥0。引理5.2&#xff1a;高度为k的二叉树中至多有 2 k 1 − 1 2^{k1}-1 2k1−1个结点&#xff0c;其中 k ≥ 0 k \geq 0 k≥0。引理5.3&…

AVL树的插入和删除

一.AVL树的四种旋转方式 以上是AVL树插入和删除时需要用到的四种旋转方式。为什么要旋转&#xff1f;因为树不平衡了,通过旋转使其再次平衡。 但是上面的四副图在旋转前就是平衡的&#xff0c;所以这样的旋转是没有意义的&#xff0c;重点在于理解旋转的方法。下面的插入和删除…

修改Openwrt软路由的web端口

如何修改openwrt路由器的web访问端口号&#xff1f; 在OpenWrt路由器上&#xff0c;如何修改Web访问端口号&#xff0c;通常涉及到修改HTTP服务器的配置文件。默认情况下&#xff0c;OpenWrt使用的HTTP服务器是uHTTPd。 以下是修改Web访问端口号的步骤&#xff1a; 一、通过…

【LeetCode刷题-双指针】--80.删除有序数组中的重复项II

80.删除有序数组中的重复项II 方法&#xff1a;双指针 因为给定数组是有序的&#xff0c;所以相同元素必然连续&#xff0c;使用双指针解决&#xff0c;遍历数组检查每一个元素是否应该被保留&#xff0c;如果应该保留&#xff0c;就将其移动到指定位置。我们定义两个指针slow…

【LeetCode刷题-滑动窗口】-- 643.子数组最大平均数I

643.子数组最大平均数I 方法&#xff1a;滑动窗口 class Solution {public double findMaxAverage(int[] nums, int k) {int n nums.length;int winSum 0;//先求出第一个窗口的和for(int i 0;i<k;i){winSum nums[i];}//通过遍历求出除了第一窗口的和int res winSum;fo…