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



    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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    HJ73计算日期到天数转换

    思路

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

    代码

    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    KY111日期差值

    思路

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

    代码

    #include 
    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;
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    KY222打印日期

    思路

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

    代码

    没有用类和对象的知识。

    #include 
    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;
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    类和对象的写法

    #include 
    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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    KY258日期累加

    思路

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

    代码

    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
  • 相关阅读:
    论如何直接用EF Core实现创建更新时间、用户审计,自动化乐观并发、软删除和树形查询(下)
    033-使用UIManager设置组件外观界面,适应不同操作系统
    编译原理—中间代码生成、布尔表达式翻译、短路计算、控制流语句翻译、条件语句、循环语句
    DPDK vhost库
    我参加NVIDIA Sky Hackathon 训练文件的路径设置
    Eureka 基本教程
    springboot毕设项目打印助手平台21swx(java+VUE+Mybatis+Maven+Mysql)
    何为链表、链表示例以及翻转链表
    互联网摸鱼日报(2022-10-21)
    【一步到位】Jenkins的安装、部署、启动(完整教程)
  • 原文地址:https://blog.csdn.net/m0_60598323/article/details/126920057