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

巩固类和对象的知识点——牛客5道题目

文章目录

  • JZ64求1+2+3+...+n
    • 思路
    • 代码
  • HJ73计算日期到天数转换
    • 思路
    • 代码
  • KY111日期差值
    • 思路
  • 代码
  • KY222打印日期
    • 思路
    • 代码
  • KY258日期累加
    • 思路
    • 代码


JZ64求1+2+3+…+n

思路

在这里插入图片描述

从题目的要求中可以看出,不能使用循环,分支语句,也不能使用位运算符,不能使用递归。
学习了c++之后,我们知道每次进行对象实例化的时候,都会调用构造函数,我们利用这个特性。在实现一个类,类的成员为静态的(因为要求和,要保留上一次的构造)。静态的成员不能在构造函数中定义。只能在全局定义。其次我们会访问该类的私有成员,所以需要把class Solution这个类设置为友元类。

代码

class A
{
    friend class Solution;
public:
    A()
    {
        _sum+=_i;
        _i++;
    }
private:
    static int _sum;
    static int _i;
};
int A::_sum=0;
int A::_i=1;
class Solution {
    
public:
    int Sum_Solution(int n) {
        A sum[n];//这里运用了柔性数组
        return A::_sum;
    }
};

HJ73计算日期到天数转换

思路

年的总天数都是相同的(闰年多一天)。我们可以用一个数组记录从一月到每个月的累计天数。根据前一个月的累计天数加上该月的天数即为这一年的第几天。(注意闰年的二月)

代码

#include <iostream>
using namespace std;

int main() {
    int year, month, day;
    cin >> year >> month >> day;
    int y_day[13] = {0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};

    if (month > 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
        cout << y_day[month-1] + day + 1;
    else
        cout << y_day[month-1] + day;
}

KY111日期差值

思路

这个的日常差值,我用的是每天每天进行累计的。直到加到和另一个日期相等。重载了+,-运算符。

代码

#include <iostream>
using namespace std;
class Date
{
public:
    Date(int year, int month, int day)
        :_year(year)
        , _month(month)
        , _day(day)
    {}
    Date& operator+(int m)
    {
        _day += m;
        while (_day > judge(_year, _month))
        {
            _day -= judge(_year, _month);
            _month++;
            if (_month == 13)
            {
                _year++;
                _month = 1;
            }
        }
        return *this;

    }
    int judge(int year, int month)
    {
        static int M[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 M[month];
    }
    bool operator==(Date& A)
    {
        if (_year != A._year)
            return false;
        else if (_year == A._year && _month != A._month)
            return false;
        else if (_year == A._year && _month == A._month && _day != A._day)
            return false;
        else
            return true;
    }
    int operator-(Date A)
    {
        int count = 1;
        while (!(A == (*this)))
        {
            A + 1;
            count++;
        }
        return count;
    }

private:
    int _year;
    int _month;
    int _day;
};
int main() {
    int a, b;
    while (cin >> a >> b) {
        Date x(a / 10000, a % 10000 / 100, a % 100);
        Date y(b / 10000, b % 10000 / 100, b % 100);
        cout << y - x;

    }
}

KY222打印日期

思路

思路非常简单,就是天数从第一个月开始减,直到天数小于该月的时候截至。主要要注意减2月份的时候——闰年29。

代码

没有用类和对象的知识。

#include <iostream>
using namespace std;
int judge(int year)
{
    if((year%4==0&&year%100!=0)||year%400==0)
    return 1;
    return 0;
}
int main() {
    int y, n;
    while (cin >> y >> n) {
        static int y_day[13] = {0, 31,28,31,30,31,30,31,31,30,31,30,31};
        int i ;
        for (i = 1; i <=12; i++) {
            if (n > y_day[i]) {
                n -= y_day[i];
                if(i==2&&judge(y))
                {
                    n--;
                    if(n==0)
                    {
                        i=2;
                        n=29;
                        printf("%d-%02d-%02d\n", y,i,n);
                        break;
                    }
                }     
            }
            else
            {
                printf("%d-%02d-%02d\n", y,i,n);
                break;
            }
        }
    }
}

类和对象的写法

#include <iostream>
using namespace std;
class Date
{
public:
    Date(int year)
    :_year(year)
    ,_month(1)
    ,_day(0)
    {}
    Date& operator+(int day)
    {
        _day+=day;
        while(_day>judge(_year,_month))
        {
            _day-=judge(_year,_month);
            _month++;
        }
        return *this;
    }
    int judge(int year,int month)
    {
        static int y_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))
        {
            return 29;
        }
        return y_day[month];
    }
    void Print()
    {
        printf("%d-%02d-%02d\n", _year,_month,_day);
    }
private:
    int _year;
    int _month;
    int _day;
};
int main() {
    int a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        Date A(a);
        (A+b).Print();
    }
}

KY258日期累加

思路

首先我们创建一个日期类。该类包含构造函数(为了初始化),运算符重载(加日期),打印(访问私有成员)。
对于加的日期我们需要进行调整,使它变成正常的日期。
当该日期大于该年月的日期,就需要减掉该年月的日常,同时月数加1,如果月数等于13,则修改成1,且年数加1。直到最后符合正常的日期为止。

代码

#include <iostream>
using namespace std;
class Date
{
public:
    Date(int year,int month,int day)
    {
        _year=year;
        _month=month;
        _day=day;
    }
    Date& operator+(int m)
    {
        _day+=m;
        while(_day>judge(_year, _month))
        {
            _day-=judge(_year,_month);
            _month++;
            if(_month==13)
            {
                _year++;
                _month=1;
            }
        }
        return *this;

    }
    int judge(int year,int month)
    {
        static int M[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 M[month];
    }
    void Print()
    {
        printf("%d-%02d-%02d\n",_year,_month,_day);
    }
private:
    int _year;
    int _month;
    int _day;

};
int main()
{
    int m;
    cin>>m;
    while(m--)
    {
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        Date A(a,b,c);
        (A+d).Print();        
    }
    return 0;
}

相关文章:

  • 黄老板,给我来个亲笔签名,抽显卡~
  • 儿童头部保护玩具CPC认证亚马逊美国站CPC认证
  • CentOS Docker 安装 常用命令
  • C语言中宏定义的盲区有哪些?
  • springboot之redis缓存探索
  • 高并发系统架构设计之微服务篇: 秒杀系统下的服务拆分
  • jieba
  • 学术英语写作(更新中)
  • 关于穿越机FPV视频果冻效应的讨论
  • 顺序表(c++类模板实现)
  • Leetcode 698. 划分为k个相等的子集
  • 开发工具安装
  • 图解字符串匹配算法:从Brute-Force到KMP,一下子就整明白了
  • Python语言:散修笔记
  • 为什么要学习Linux内核,如何学习?
  • __proto__ 和 prototype的关系
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • C++类中的特殊成员函数
  • java第三方包学习之lombok
  • Nodejs和JavaWeb协助开发
  • python3 使用 asyncio 代替线程
  • spring学习第二天
  • 笨办法学C 练习34:动态数组
  • 微信开放平台全网发布【失败】的几点排查方法
  • 微信小程序:实现悬浮返回和分享按钮
  • 我的zsh配置, 2019最新方案
  • 一个项目push到多个远程Git仓库
  • 由插件封装引出的一丢丢思考
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • ​渐进式Web应用PWA的未来
  • ​用户画像从0到100的构建思路
  • (06)金属布线——为半导体注入生命的连接
  • (13):Silverlight 2 数据与通信之WebRequest
  • (ZT)出版业改革:该死的死,该生的生
  • (附源码)ssm高校运动会管理系统 毕业设计 020419
  • (七)Java对象在Hibernate持久化层的状态
  • (新)网络工程师考点串讲与真题详解
  • (译)2019年前端性能优化清单 — 下篇
  • .NET delegate 委托 、 Event 事件,接口回调
  • .net refrector
  • .vue文件怎么使用_我在项目中是这样配置Vue的
  • :中兴通讯为何成功
  • [2016.7 test.5] T1
  • [Angular 基础] - 自定义指令,深入学习 directive
  • [BetterExplained]书写是为了更好的思考(转载)
  • [BIZ] - 1.金融交易系统特点
  • [BJDCTF2020]The mystery of ip
  • [BSGS算法]纯水斐波那契数列
  • [C#小技巧]如何捕捉上升沿和下降沿
  • [CDOJ 1343] 卿学姐失恋了
  • [docker] Docker容器服务更新与发现之consul
  • [git] windows系统安装git教程和配置
  • [javascript]Tab menu实现