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")

 

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

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

                                              

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

如何正确使用 JavaScript 中的 slice() 方法

在 JavaScript 中&#xff0c;slice() 是一个常用的数组方法&#xff0c;用于从现有数组中提取一部分元素&#xff0c;然后返回一个新的数组。它是一个非常有用的工具&#xff0c;可以帮助你在不改变原始数组的情况下操作数组的子集。本文将介绍 slice() 的基本概念、使用方法、…

阿里云+宝塔部署项目(Java+React)

阿里云服务器宝塔面板部署项目&#xff08;SpringBoot React&#xff09; 1. 上传所需的文件到服务器 比如jdk包和java项目的jar&#xff1a;这里以上传jar 为例&#xff0c;创建文件夹&#xff0c;上传文件&#xff1b; 在创建的文件夹下上传jar包 上传jdk 2. 配置jdk环境 3.…

No201.精选前端面试题,享受每天的挑战和学习

🤍 前端开发工程师(主业)、技术博主(副业)、已过CET6 🍨 阿珊和她的猫_CSDN个人主页 🕠 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 🍚 蓝桥云课签约作者、已在蓝桥云课上架的前后端实战课程《Vue.js 和 Egg.js 开发企业级健康管理项目》、《带你从入…

【游戏开发算法每日一记】使用随机prime算法生成错综复杂效果的迷宫(C#,C++和Unity版)

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 秩沅 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a;Uni…

java实现选择排序

算法步骤 首先在未排序序列中找到最小&#xff08;大&#xff09;元素&#xff0c;存放到排序序列的起始位置再从剩余未排序元素中继续寻找最小&#xff08;大&#xff09;元素&#xff0c;然后放到已排序序列的末尾。重复第二步&#xff0c;直到所有元素均排序完毕。 动图演…

【Linux网络】ssh服务与配置,实现安全的密钥对免密登录

目录 一、SSH基础 1、什么是ssh服务器 2、对比一下ssh协议与telnet协议 3、常见的底层为ssh协议的软件&#xff1a; 4、拓展 二、SSH软件学习 1、ssh服务软件学习 2、sshd公钥传输的原理&#xff1a; 3、ssh命令学习&#xff1a; 4、学习解读sshd服务配置文件&#x…

自定义Matplotlib中的颜色映射(cmap)

要自定义Matplotlib中的颜色映射&#xff08;cmap&#xff09;&#xff0c;您可以按照以下步骤进行操作&#xff1a; 导入所需的库&#xff1a; import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap创建自定义颜色映…

一篇博客读懂栈——Stack

目录 一、栈的概念与结构 1.1栈的概念 1.2栈的结构 二、栈的实现 2.1开始前准备stack.h 2.2栈的初始化STInit 2.3栈的销毁STDestroy 2.4栈顶插入STPush 2.5栈顶删除STPop 2.6取栈顶元素STTop 2.7检查栈是否为空STEmpty 2.8栈的元素个数STSize 一、栈的概念与结…

房产中介租房小程序系统开发搭建:详细指南教你如何构建

随着微信小程序的日益普及&#xff0c;越来越多的企业和个人开始尝试开发自己的小程序。以下是制作一个房地产微信小程序的详细教程&#xff0c;希望对大家有所帮助。 一、注册登录乔拓云平台&#xff0c;进入后台 首先&#xff0c;需要注册并登录乔拓云平台&#xff0c;该平台…

JavaEE初阶(18)(JVM简介:发展史,运行流程、类加载:类加载的基本流程,双亲委派模型、垃圾回收相关:死亡对象的判断算法,垃圾回收算法,垃圾收集器)

接上次博客&#xff1a;初阶JavaEE&#xff08;17&#xff09;Linux 基本使用和 web 程序部署-CSDN博客 目录 JVM 简介 JVM 发展史 JVM 运行流程 JVM的内存区域划分 JVM 执行流程 堆 堆的作用 JVM参数设置 堆的组成 垃圾回收 堆内存管理 类加载 类加载的基本流…

Vue事件绑定

目录 前言 Vue事件处理 指令的语法格式 事件绑定指令—v-on 回调函数 前言 我们知道如何在原生js中实现事件绑定&#xff0c;那在vue中如何实现呢&#xff1f; Vue事件处理 指令的语法格式 <标签 v-指令名&#xff1a;参数名"表达式">{{插值语法}}</…

MQ四大消费问题一锅端:消息不丢失 + 消息积压 + 重复消费 + 消费顺序性

RabbitMQ-如何保证消息不丢失 生产者把消息发送到 RabbitMQ Server 的过程中丢失 从生产者发送消息的角度来说&#xff0c;RabbitMQ 提供了一个 Confirm&#xff08;消息确认&#xff09;机制&#xff0c;生产者发送消息到 Server 端以后&#xff0c;如果消息处理成功&#xff…

WebGl-Blender:建模 / 想象成形 / 初识 Blender

一、理解Blender 欢迎来到Blender&#xff01;Blender是一款免费开源的3D创作套件。 使用Blender&#xff0c;您可以创建3D可视化效果&#xff0c;例如建模、静态图像&#xff0c;3D动画&#xff0c;VFX&#xff08;视觉特效&#xff09;快照和视频编辑。它非常适合那些受益于…

解密网络世界的秘密——Wireshark Mac/Win中文版网络抓包工具

在当今数字化时代&#xff0c;网络已经成为了人们生活和工作中不可或缺的一部分。然而&#xff0c;对于网络安全和性能的监控和分析却是一项重要而又复杂的任务。为了帮助用户更好地理解和解决网络中的问题&#xff0c;Wireshark作为一款强大的网络抓包工具&#xff0c;应运而生…

Python基础入门----如何使用 Pipenv 在项目目录中创建虚拟环境

文章目录 引言Pipenv 简介安装 Pipenv在项目目录中创建虚拟环境1. 进入你的项目目录2. 设置环境变量3. 创建虚拟环境4. 激活虚拟环境结论引言 在Python开发中,使用虚拟环境是一种良好的实践,它可以帮助开发者管理项目的依赖,并避免不同项目间的依赖冲突。Pipenv 是一个流行…

nginx安装搭建

下载 免费开源版的官方网站&#xff1a;nginx news Nginx 有 Windows 版本和 Linux 版本&#xff0c;但更推荐在 Linux 下使用 Nginx&#xff1b; 下载nginx-1.14.2.tar.gz的源代码文件&#xff1a;wget http://nginx.org/download/nginx-1.14.2.tar.gz 我的习惯&#xff0…

【excel技巧】如何取消excel隐藏?

Excel工作表中的行列隐藏了数据&#xff0c;如何取消隐藏行列呢&#xff1f;今天分享几个方法给大家 方法一&#xff1a; 选中隐藏的区域&#xff0c;点击右键&#xff0c;选择【取消隐藏】就可以了 方法二&#xff1a; 如果工作表中有多个地方有隐藏的话&#xff0c;还是建…

专业调色软件 3D LUT Creator Pro 激活中文 for mac

3D LUT Creator与彩 色 图 像一起工作的简单性和清晰度不会让任何人无动于衷。此外&#xff0c;扩展名为.3dl的文件可以导入到Adobe Photoshop&#xff0c;因此您可以将这些设置作为调整图层应用&#xff0c;不仅可以将它们应用于位图图像&#xff0c;还可以将其应用于矢量图形…

Pytorch教程(代码逐行解释)

0、配准环境教程 1、开始导入相应的包 import torch from torch import nn from torch.utils.data import DataLoader from torchvision import datasets from torchvision.transforms import ToTensortorch是pytorch的简写 torch.utils.data import DataLoader 是用于读取数…

抖斗音_快块手直播间获客助手+采集脚本+引流软件功能介绍

软件功能&#xff1a; 支持同时采集多个直播间&#xff0c;弹幕&#xff0c;关*注&#xff0c;礼*物&#xff0c;进直播间&#xff0c;部分用户手*号,粉*丝团采集 不支持采集匿*名直播间 设备需求&#xff1a; 电脑&#xff08;win10系统&#xff09; 文章分享者&#xff1…