C++类和对象-牛客OJ

 前言

本篇博客讲解一些关于C++类和对象-牛客OJ,看这篇博客之前,请先看类和对象

💓 个人主页:普通young man-CSDN博客

⏩ 文章专栏:LeetCode_普通young man的博客-CSDN博客

⏩ 本人giee:   普通小青年 (pu-tong-young-man) - Gitee.com

      若有问题 评论区见📝

🎉欢迎大家点赞👍收藏⭐文章

JZ64 求1+2+3+...+n

求1+2+3+...+n_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=N7T8https://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1?tpId=13&tqId=11200&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

#include <iostream>
using namespace std;

class Solution {
  private:
    // 静态整型变量,初始值为 1
    static int _i;
    
    // 静态整型变量,初始值为 0
    static int _ret;
    
    // 嵌套类 Sum 用于计算累加值
    class Sum {
      
      public:
        // 默认构造函数
        Sum() {
            // 将 _ret 的值增加 _i 的值
            _ret += _i;
            
            // 增加 _i 的值
            ++_i;
        }
        
        // 静态成员函数,返回 _ret 的值
        static int getret() {
            return _ret;
        }
    };

  public:
    // 成员函数 Sum_Solution,接受一个整数 n 作为参数
    int Sum_Solution(int n) {
        // 创建一个大小为 n 的 Sum 类型的数组
        Sum arr[n]; 
        
        // 返回所有 Sum 对象累加的结果
        return Sum::getret();
    }
};

// 初始化静态成员变量
int Solution::_i = 1;
int Solution::_ret = 0;

int main() {
    Solution sol;
    int n;
    cout << "Enter the number of elements to create: ";
    cin >> n;
    int result = sol.Sum_Solution(n);
    cout << "The sum is: " << result << endl;
    return 0;
}

这段代码定义了一个名为 Solution 的类,其中包含了一个嵌套类 Sum 和一个公共成员函数 Sum_Solution。下面是详细的解析和思路:

类 Solution

成员变量
  • _i: 静态整型变量,初始值为 1
  • _ret: 静态整型变量,初始值为 0
成员函数
  • Sum_Solution: 这个函数接受一个整数 n 作为参数,创建一个大小为 n 的 Sum 类型的数组,并返回所有 Sum 对象累加的结果。

嵌套类 Sum

成员函数
  • 默认构造函数 Sum: 当一个 Sum 对象被创建时,它会执行以下操作:
    • 将 _ret 的值增加 _i 的值。
    • 增加 _i 的值。
  • getret: 返回 _ret 的值。

代码工作原理

Sum_Solution 函数被调用时,会创建一个大小为 nSum 类型的数组 arr。数组中的每一个元素都会调用 Sum 的默认构造函数,这会导致 _ret 的值根据 _i 的值累加,并且 _i 每次都会增加。

例如,如果 n5,那么会发生以下过程:

  1. 创建第一个 Sum 对象时,_ret = 1(因为 _i 的初始值为 1),然后 _i 变为 2
  2. 创建第二个 Sum 对象时,_ret = 1 + 2 = 3,然后 _i 变为 3
  3. 创建第三个 Sum 对象时,_ret = 3 + 3 = 6,然后 _i 变为 4
  4. 创建第四个 Sum 对象时,_ret = 6 + 4 = 10,然后 _i 变为 5
  5. 创建第五个 Sum 对象时,_ret = 10 + 5 = 15,然后 _i 变为 6

因此,当 n5 时,Sum_Solution 的返回值将是 15

总结

  • 这段代码利用了静态成员变量来保持状态,并使用嵌套类来实现一个简单的累加器。
  • Sum 类的对象创建过程中,会累加一个全局的计数器 _ret,每次创建对象时都会增加 _i 的值,并将 _i 加到 _ret 上。
  • 最终返回的是 _ret 的值,即所有 Sum 对象创建过程中累加的总和。

KY258 日期累加

日期累加_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=N7T8https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d?tpId=40&&tqId=31013&rp=1&ru=/activity/oj&qru=/ta/kaoyan/question-ranking

#include <iostream>
#include <ostream>
#include <assert.h>
using namespace std;

class Data {
    friend ostream& operator<< (ostream& out, const Data& d); // 声明友元函数用于输出 Data 对象
    
  public:
    // 构造函数,允许默认参数初始化
    Data(int year = 1, int month = 1, int day = 1) : _year(year), _month(month),
        _day(day)
    {}
    
    // 获取某个月的天数
    int Getmonthday(int year, int month) {
        assert(month > 0 && month < 13); // 断言月份在合理范围内
        
        // 数组 a 包含每个月的天数
        int a[] = { -1, 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; // 闰年二月有 29 天
        } else {
            return a[month]; // 非闰年或其他月份的天数
        }
    }
    
    // 重载赋值运算符
    Data& operator=(const Data& d) {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        return *this;
    }
    
    // 重载减法运算符,用于从当前日期中减去一定天数
    Data& operator-=(int day) {
        if (day < 0) {
            return *this += (-day); // 如果天数为负,转换为加法运算
        }
        
        _day -= day; // 减少天数
        
        // 如果日期小于等于 0,则向前移动月份和年份
        while (_day <= 0) {
            --_month;
            if (_month == 0) { // 如果月份变为 0,则设置为 12 月并减少一年
                _month = 12;
                --_year;
            }
            _day += Data::Getmonthday(_year, _month); // 添加当前月份的天数
        }
        
        return *this;
    }
    
    // 重载加法运算符,用于向当前日期添加一定天数
    Data& operator+=(int day) {
        if (day < 0) {
            return *this -= (-day); // 如果天数为负,转换为减法运算
        }
        
        _day += day; // 增加天数
        
        // 如果日期超过当前月份的最大天数,则向后移动月份和年份
        while (_day > Data::Getmonthday(_year, _month)) {
            _day -= Data::Getmonthday(_year, _month); // 减去当前月份的天数
            ++_month;
            // 如果月份超过 12,则设置为 1 月并增加一年
            if (_month == 13) {
                ++_year;
                _month = 1;
            }
        }
        
        return *this;
    }
    
    // 重载加法运算符,用于创建一个新的 Data 对象,该对象表示当前日期加上一定的天数
    Data operator+(int d)const {
        Data tmp = *this; // 创建一个副本
        tmp += d; // 调用重载的加法运算符
        return tmp; // 返回新的日期
    }
    
    
  private:
    int _year; // 年份
    int _month; // 月份
    int _day; // 日期
};

// 重载输出运算符,用于将 Data 对象输出到标准输出流
ostream& operator<< (ostream& out, const Data& d) {
    // 格式化输出日期
    if (d._month < 10 && d._day < 10) {
        out << d._year << "-" << '0' << d._month << "-" << '0' << d._day << endl;
    } else if (d._month < 10) {
        out << d._year << "-" << '0' << d._month << "-" << d._day << endl;
    } else if (d._day < 10) {
        out << d._year << "-" << d._month << "-" << '0' << d._day << endl;
    } else {
        out << d._year << "-" << d._month << "-" << d._day << endl;
    }
    
    return out;
}

int main() {
    int year = 0, month = 0, day = 0, addDay = 0, n = 0;
    cin >> n; // 读取测试案例的数量
    
    // 循环处理每个测试案例
    for (int i = 0; i < n; i++) {
        cin >> year >> month >> day >> addDay; // 读取年份、月份、日期和要添加的天数
        Data d(year, month, day); // 创建 Data 对象 d
        Data tmp = (d + addDay); // 创建新的 Data 对象 tmp,表示 d 日期加上 addDay 天后的日期
        cout << tmp; // 输出新的日期
    }
    
    return 0;
}

类 Data

成员变量
  • _year: 表示年份。
  • _month: 表示月份。
  • _day: 表示日期。
成员函数
  • 构造函数 Data: 接受三个整数参数,分别代表年、月、日,默认值分别为 1、1、1。
  • Getmonthday: 一个非成员函数,用于获取某个月的天数。如果是闰年并且是二月,则返回 29 天;否则返回对应月份的天数。
  • operator=: 重载赋值运算符,用于复制一个 Data 对象到另一个 Data 对象。
  • operator-=: 重载减法运算符,用于从当前日期中减去一定天数。
  • operator+=: 重载加法运算符,用于向当前日期添加一定天数。
  • operator+: 重载加法运算符,用于创建一个新的 Data 对象,该对象表示当前日期加上一定的天数。
友元函数
  • operator<<: 重载输出运算符,用于将 Data 对象输出到标准输出流。

主函数 main

主函数首先读取一个整数 n,表示接下来会有 n 组日期数据和要添加的天数。对于每一组数据,它读取年份、月份、日期以及要添加的天数,创建一个 Data 对象 d 并使用 operator+ 来创建一个新的 Data 对象 tmp,最后输出 tmp 的值。

代码工作原理

  1. 输入: 从标准输入读取一个整数 n,表示有 n 组测试数据。
  2. 循环读取: 对于每组数据,读取年、月、日和要添加的天数。
  3. 创建对象: 使用读取的年、月、日创建一个 Data 对象 d
  4. 日期加法: 使用 Data 类的 operator+ 来创建一个新的 Data 对象 tmp,表示原始日期加上指定天数后的新日期。
  5. 输出: 输出新日期 tmp

示例

假设输入如下数据:

13
22023 1 1 31
32024 2 28 2
42021 12 31 1
  • 第一组数据表示从 2023 年 1 月 1 日开始加 31 天,结果应该是 2023 年 2 月 1 日。
  • 第二组数据表示从 2024 年 2 月 28 日开始加 2 天(2024 年是闰年),结果应该是 2024 年 3 月 1 日。
  • 第三组数据表示从 2021 年 12 月 31 日开始加 1 天,结果应该是 2022 年 1 月 1 日。

程序将输出:

12023-02-01
22024-03-01
32022-01-01

HJ73 计算日期到天数转换

计算日期到天数转换_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=N7T8https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded?tpId=37&&tqId=21296&rp=1&ru=/activity/oj&qru=/ta/huawei/question-ranking

#include <iostream>
using namespace std;

int a[] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 数组 a 存储每个月的天数,第一个元素为 -1 以便索引与月份对应

int main() {
    int year, month, day;
    cin >> year >> month >> day; // 读取年份、月份和日期
    
    int sum = 0; // 初始化累计天数为 0
    
    // 循环累加从 1 月到输入月份前一天的所有月份的天数
    for (int i = 1; i < month; i++) {
        sum += a[i];
    }
    
    sum += day; // 加上输入的日期
    
    // 判断是否为闰年,并且月份大于 2 月
    if (month > 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) {
        ++sum; // 如果是闰年且月份大于 2 月,则累加一天
    }
    
    cout << sum << endl; // 输出累计的天数
    
    return 0;
}

代码结构

  1. 数组 a[]: 定义了一个整型数组 a[],用于存储每个月的天数。数组的第一个元素为 -1,这是因为数组下标从 0 开始,而月份是从 1 开始的,所以数组的索引与月份的对应关系需要进行调整。

  2. 主函数 main:

    • 输入年份 year、月份 month 和日期 day
    • 初始化一个整型变量 sum 为 0,用于累计天数。
    • 使用一个循环来累加从 1 月到输入月份前一天的所有月份的天数。
    • 将输入的日期 day 加到 sum 中。
    • 如果输入的月份大于 2 月,并且年份是闰年(满足闰年的条件:能被 4 整除但不能被 100 整除,或者能被 400 整除),则累加一天,以考虑闰年的额外一天。
    • 输出累计的天数 sum

代码解析

  1. 数组初始化:

int a[] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1. 数组 a[] 的第一个元素为 -1,表示不使用该值,真正的月份从下标 1 开始,这样可以直接使用 a[month] 来获取对应的天数。

  2. 读取输入

cin >> year >> month >> day;
  1. 从标准输入读取年份、月份和日期。

  2. 计算累计天数:

int sum = 0;
for (int i = 1; i < month ;i++) {
    sum += a[i];
}
sum += day;
  1. 通过循环累加每个月的天数,直到到达输入的月份之前。之后加上输入的日期。

  2. 判断闰年:

if (month > 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) {
    ++sum;
}

示例运行

假设输入为 2024 3 15,则程序的运行步骤如下:

  1. 从 1 月到 2 月的天数为 31 + 29(2024 是闰年,二月有 29 天)。
  2. 加上 3 月的 15 天。
  3. 最终输出为 31 + 29 + 15 = 75

KY111 日期差值

日期差值_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=N7T8https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c?tpId=62&&tqId=29468&rp=1&ru=

#include<assert.h>
#include <iostream>
using namespace std;

class Data {
  public:
    Data(const int& year, const int& month, const int& day) {
        _year = year;
        _month = month;
        _day = day;
    }

    //==
    bool operator==(const Data& d) const {
        return _year == d._year &&
               _month == d._month &&
               _day == d._day;
    }
    //!=
    bool operator!=(const Data& d) const {

        return !(*this == d);
    };

    bool operator<(const Data& d) const {
        if (_year < d._year) {
            return true;
        } else if (_year == d._year) {
            if (_month < d._month) {
                return true;
            } else if (_month == d._month) {
                return  _day < d._day;
            }
        }
        return false;
    }
//<=
bool operator<=(const Data& d) const {
	
	return *this < d || *this == d;
}
//>
bool operator>(const Data& d) const {
	return !(*this <= d);
};

    Data& operator=(const Data& d) {
        if (*this != d) {
            _year = d._year;
            _month = d._month;
            _day = d._day;
        }
        return *this;
    }

    int Getmonthday(int year, int month) {
        assert(month > 0 && month < 13);
        int a[] = { -1, 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;
        } else {
            return a[month];
        }

    }
    //-=
    Data& operator-=(int day) {

        if (day < 0) {
            return *this += (-day);
        }
        _day -= day;
        while (_day <= 0) {
            --_month;
            if (_month == 0) {
                _month = 12;
                --_year;
            }
            _day += Data::Getmonthday(_year, _month);
        }
        return *this;
    }


    Data& operator+=(int day) {
        if (day < 0) {
            return *this -= (-day);
        }

        _day += day;
        if (_day > Data::Getmonthday(_year, _month)) {
            _day -= Data::Getmonthday(_year, _month);
            ++_month;
            //判断月份边界
            if (_month == 13) {
                ++_year;
                _month = 1;
            }
        }
        return *this;
    }


    Data operator++() {
        *this += 1;
        return *this;
    }

    int operator-(const Data& b) {
        Data Max = *this;
        Data Min = b;
        int flag = 1;
        if (*this < b) {
            Max = b;
            Min = *this;
            flag = -1;
        }
        int n = 0;
        while (Min != Max) {
            ++Min;
            ++n;
        }
        return n * flag;
    }


  private:
    int _year;
    int _month;
    int _day;
};

int main() {
    int year1, year2, month1, month2, day1, day2;
    scanf("%4d%2d%2d",&year1,&month1,&day1);
    Data a(year1, month1, day1);
    scanf("%4d%2d%2d",&year2,&month2,&day2);
    Data b(year2, month2, day2);
    if(a > b){
    cout << (a - b )+1 << endl;

    }else if(b > a){

    cout << (b - a)+1 << endl;
    }
    else {
    cout<< 0 <<endl;
    }
}

类 Data

成员变量
  • _year: 表示年份。
  • _month: 表示月份。
  • _day: 表示日期。
成员函数
  • 构造函数 Data: 接受三个整数参数,分别代表年、月、日。
  • operator==: 重载等号运算符,用于比较两个 Data 对象是否相等。
  • operator!=: 重载不等号运算符,用于比较两个 Data 对象是否不相等。
  • operator<: 重载小于号运算符,用于比较两个 Data 对象的先后顺序。
  • operator<=: 重载小于等于号运算符,用于比较两个 Data 对象的先后顺序。
  • operator>: 重载大于号运算符,用于比较两个 Data 对象的先后顺序。
  • operator=: 重载赋值运算符,用于复制一个 Data 对象到另一个 Data 对象。
  • Getmonthday: 一个非成员函数,用于获取某个月的天数。如果是闰年并且是二月,则返回 29 天;否则返回对应月份的天数。
  • operator-=: 重载减法运算符,用于从当前日期中减去一定天数。
  • operator+=: 重载加法运算符,用于向当前日期添加一定天数。
  • operator++: 重载前置递增运算符,用于向当前日期添加一天。
  • operator-: 重载减法运算符,用于计算两个日期之间的天数差。

主函数 main

主函数首先读取两个日期的数据,创建两个 Data 对象 ab,然后比较这两个日期,并输出它们之间相差的天数加一。

代码工作原理

  1. 输入: 从标准输入读取两个日期,格式为 YYYY MM DD
  2. 创建对象: 使用读取的年、月、日创建两个 Data 对象 a 和 b
  3. 比较日期: 使用 operator> 来比较两个日期的先后顺序。
  4. 计算差值: 使用 operator- 来计算两个日期之间的天数差,并输出这个差值加一。

KY222 打印日期

打印日期_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=N7T8https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b?tpId=69&&tqId=29669&rp=1&ru=/activity/oj&qru=/ta/hust-kaoyan/question-ranking

#include <iostream>
#include <ostream>
#include <assert.h>
using namespace std;

class Data {
    friend ostream& operator<< (ostream& out, const Data& d);
  public:
    Data(int year, int month = 1, int day = 0) : _year(year), _month(month),
        _day(day)
    {}
    int Getmonthday(int year, int month) {
        assert(month > 0 && month < 13);
        int a[] = { -1, 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;
        } else {
            return a[month];
        }

    }
    //重载

    Data& operator=(const Data& d) {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        return *this;
    }
    //-=
    Data& operator-=(int day) {

        if (day < 0) {
            return *this += (-day);
        }
        _day -= day;
        while (_day <= 0) {
            --_month;
            if (_month == 0) {
                _month = 12;
                --_year;
            }
            _day += Data::Getmonthday(_year, _month);
        }
        return *this;
    }

    Data& operator+=(int day) {
        if (day < 0) {
            return *this -= (-day);
        }

        _day += day;
        while (_day > Data::Getmonthday(_year, _month)) {
            _day -= Data::Getmonthday(_year, _month);
            ++_month;
            //判断月份边界
            if (_month == 13) {
                ++_year;
                _month = 1;
            }
        }
        return *this;
    }

    Data operator+(int d)const {
        Data tmp = *this;
        tmp += d;
        return tmp;
    }



  private:
    int _year;
    int _month;
    int _day;
};

ostream& operator<< (ostream& out, const Data& d) {
    if (d._month < 10 && d._day < 10) {
        out << d._year << "-" << '0' << d._month << "-" << '0' << d._day << endl;
    } else if (d._month < 10) {
        out << d._year << "-" << '0' << d._month << "-" << d._day << endl;
    } else if (d._day < 10) {

        out << d._year << "-" << d._month << "-" << '0' << d._day << endl;
    } else {

        out << d._year << "-" << d._month << "-" << d._day << endl;

    }

    return out;
}
int main() {
    int year = 0, n = 0;

    while (cin >> year >> n) {

        Data d(year);
        Data tmp = (d + n);
        cout << tmp;

    }
}

类 Data

成员变量
  • _year: 表示年份。
  • _month: 表示月份。
  • _day: 表示日期。
成员函数
  • 构造函数 Data: 接受三个整数参数,分别代表年、月、日,默认值分别为 1、1、0。
  • Getmonthday: 一个非成员函数,用于获取某个月的天数。如果是闰年并且是二月,则返回 29 天;否则返回对应月份的天数。
  • operator=: 重载赋值运算符,用于复制一个 Data 对象到另一个 Data 对象。
  • operator-=: 重载减法运算符,用于从当前日期中减去一定天数。
  • operator+=: 重载加法运算符,用于向当前日期添加一定天数。
  • operator+: 重载加法运算符,用于创建一个新的 Data 对象,该对象表示当前日期加上一定的天数。
友元函数
  • operator<<: 重载输出运算符,用于将 Data 对象输出到标准输出流。

主函数 main

主函数读取一个年份 year 和一个整数 n,创建一个 Data 对象 d 并使用 operator+ 来创建一个新的 Data 对象 tmp,表示原始日期加上 n 天后的新日期,最后输出 tmp 的值。

代码工作原理

  1. 输入: 从标准输入读取一个年份 year 和一个整数 n
  2. 创建对象: 使用读取的年份创建一个 Data 对象 d
  3. 日期加法: 使用 Data 类的 operator+ 来创建一个新的 Data 对象 tmp,表示原始日期加上 n 天后的新日期。
  4. 输出: 输出新日期 tmp

  • 9
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

普通young man

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值