前言
本篇博客讲解一些关于C++类和对象-牛客OJ,看这篇博客之前,请先看类和对象
💓 个人主页:普通young man-CSDN博客
⏩ 文章专栏:LeetCode_普通young man的博客-CSDN博客
⏩ 本人giee: 普通小青年 (pu-tong-young-man) - Gitee.com
若有问题 评论区见📝
🎉欢迎大家点赞👍收藏⭐文章
JZ64 求1+2+3+...+n
#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
函数被调用时,会创建一个大小为n
的Sum
类型的数组arr
。数组中的每一个元素都会调用Sum
的默认构造函数,这会导致_ret
的值根据_i
的值累加,并且_i
每次都会增加。例如,如果
n
为5
,那么会发生以下过程:
- 创建第一个
Sum
对象时,_ret = 1
(因为_i
的初始值为1
),然后_i
变为2
。- 创建第二个
Sum
对象时,_ret = 1 + 2 = 3
,然后_i
变为3
。- 创建第三个
Sum
对象时,_ret = 3 + 3 = 6
,然后_i
变为4
。- 创建第四个
Sum
对象时,_ret = 6 + 4 = 10
,然后_i
变为5
。- 创建第五个
Sum
对象时,_ret = 10 + 5 = 15
,然后_i
变为6
。因此,当
n
为5
时,Sum_Solution
的返回值将是15
。总结
- 这段代码利用了静态成员变量来保持状态,并使用嵌套类来实现一个简单的累加器。
Sum
类的对象创建过程中,会累加一个全局的计数器_ret
,每次创建对象时都会增加_i
的值,并将_i
加到_ret
上。- 最终返回的是
_ret
的值,即所有Sum
对象创建过程中累加的总和。
KY258 日期累加
#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
的值。代码工作原理
- 输入: 从标准输入读取一个整数
n
,表示有n
组测试数据。- 循环读取: 对于每组数据,读取年、月、日和要添加的天数。
- 创建对象: 使用读取的年、月、日创建一个
Data
对象d
。- 日期加法: 使用
Data
类的operator+
来创建一个新的Data
对象tmp
,表示原始日期加上指定天数后的新日期。- 输出: 输出新日期
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 计算日期到天数转换
#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;
}
代码结构
数组
a[]
: 定义了一个整型数组a[]
,用于存储每个月的天数。数组的第一个元素为-1
,这是因为数组下标从 0 开始,而月份是从 1 开始的,所以数组的索引与月份的对应关系需要进行调整。主函数
main
:
- 输入年份
year
、月份month
和日期day
。- 初始化一个整型变量
sum
为 0,用于累计天数。- 使用一个循环来累加从 1 月到输入月份前一天的所有月份的天数。
- 将输入的日期
day
加到sum
中。- 如果输入的月份大于 2 月,并且年份是闰年(满足闰年的条件:能被 4 整除但不能被 100 整除,或者能被 400 整除),则累加一天,以考虑闰年的额外一天。
- 输出累计的天数
sum
。代码解析
数组初始化:
int a[] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
数组
a[]
的第一个元素为-1
,表示不使用该值,真正的月份从下标 1 开始,这样可以直接使用a[month]
来获取对应的天数。读取输入
cin >> year >> month >> day;
从标准输入读取年份、月份和日期。
计算累计天数:
int sum = 0; for (int i = 1; i < month ;i++) { sum += a[i]; } sum += day;
通过循环累加每个月的天数,直到到达输入的月份之前。之后加上输入的日期。
判断闰年:
if (month > 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) { ++sum; }
示例运行
假设输入为
2024 3 15
,则程序的运行步骤如下:
- 从 1 月到 2 月的天数为
31 + 29
(2024 是闰年,二月有 29 天)。- 加上 3 月的 15 天。
- 最终输出为
31 + 29 + 15 = 75
。
KY111 日期差值
#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
对象a
和b
,然后比较这两个日期,并输出它们之间相差的天数加一。代码工作原理
- 输入: 从标准输入读取两个日期,格式为
YYYY MM DD
。- 创建对象: 使用读取的年、月、日创建两个
Data
对象a
和b
。- 比较日期: 使用
operator>
来比较两个日期的先后顺序。- 计算差值: 使用
operator-
来计算两个日期之间的天数差,并输出这个差值加一。
KY222 打印日期
#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
的值。代码工作原理
- 输入: 从标准输入读取一个年份
year
和一个整数n
。- 创建对象: 使用读取的年份创建一个
Data
对象d
。- 日期加法: 使用
Data
类的operator+
来创建一个新的Data
对象tmp
,表示原始日期加上n
天后的新日期。- 输出: 输出新日期
tmp
。