【每日刷题】Day80
🥕个人主页:开敲🍉
🔥所属专栏:每日刷题🍍
🌼文章目录🌼
1. 求1+2+3+...+n_牛客题霸_牛客网 (nowcoder.com)
2. 计算日期到天数转换_牛客题霸_牛客网 (nowcoder.com)
3. 日期差值_牛客题霸_牛客网 (nowcoder.com)
1. 求1+2+3+...+n_牛客题霸_牛客网 (nowcoder.com)
//思路:利用每次创建类类型对象都会调用构造函数的特性,在构造函数中实现等差数列和的功能。因此需要我们创建n个类类型对象。
#include <regex>
class Solution {
public:
class Sum
{
public:
//构造函数,其中实现等差数列和功能
//注意:这里的Sum是内部类,内部类默认是外部类的友元类,因此可以调用Solution的所有成员。
Sum()
{
x+=y;
y++;
}
};
int Sum_Solution(int n)
{
//创建类类型数组,其中创建n个类类型对象,用于多次调用Sum的构造函数
Sum arr[n];
return x;
}
private:
static int x;
static int y;
};
int Solution::x = 0;
int Solution::y = 1;
2. 计算日期到天数转换_牛客题霸_牛客网 (nowcoder.com)
//注:这里的解题思路仅供参考,因为实现了一个日期类
//思路:实现一个日期类,完成 日期+=天数、日期-=天数、日期与日期的比较、日期-日期等功能。调用日期-日期函数,将要求的日期减去相同年份的1月1号再+1,即可求出该日期是一年中的第多少天。
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year,int month,int day)
{
_year = year;
_month = month;
_day = day;
}
//- 运算符重载
int operator-(const Date& d);
//+= 运算符重载
Date& operator+=(int day);
//-= 运算符重载
Date& operator-=(int day);
//< 运算符重载
bool operator<(const Date& d);
//!= 运算符重载
bool operator!=(const Date& d);
//==运算符重载
bool operator==(const Date& d);
//> 运算符重载
bool operator>(const Date& d);
//>= 运算符重载
bool operator>=(const Date& d);
//<= 运算符重载
bool operator<=(const Date& d);
private:
int _year;
int _month;
int _day;
};
int GetMonthDay(int year,int month)
{
int arr[14] = {-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;
return arr[month];
}
//<重载函数定义
bool Date::operator<(const Date& d)
{
return !(*this > d || *this == d);
}
//>重载函数定义
bool Date::operator>(const 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;
}
//>=重载函数定义
bool Date::operator>=(const Date& d)
{
return !(*this < d);
}
//<=重载函数定义
bool Date::operator<=(const Date& d)
{
return !(*this > d);
}
//!=重载函数定义
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
//==重载函数定义
bool Date::operator==(const Date& d)
{
return _year == d._year && _month == d._month && _day == d._day;
}
//日期-日期
int Date::operator-(const Date& d)
{
int ans = 0;
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
while (min != max)
{
min += 1;
ans++;
}
return ans * flag;
}
//日期+=天数
Date& Date::operator+=(int day)
{
if (day < 0)
{
*this -= day;
return *this;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
//日期-=天数
Date& Date::operator-=(int day)
{
if (day < 0)
{
*this += -day;
return *this;
}
_day -= day;
while (_day <= 0)
{
_month--;
if (!_month)
{
_month = 12;
_year--;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
int main()
{
int year = 0;
int month = 0;
int day = 0;
cin>>year>>month>>day;
Date d1(year,month,day);
Date flag(year,1,1);
printf("%d",d1-flag+1);
return 0;
}
3. 日期差值_牛客题霸_牛客网 (nowcoder.com)
//注:此处解题思路同样仅供参考,理由同上
//思路:实现一个日期类,调用日期-日期函数求得两日期的差值。
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year,int month,int day)
{
_year = year;
_month = month;
_day = day;
}
bool operator>(const Date& d);
bool operator==(const Date& d);
bool operator<(const Date& d);
bool operator>=(const Date& d);
bool operator<=(const Date& d);
bool operator!=(const Date& d);
Date& operator+=(int day);
Date& operator-=(int day);
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
//获取月份天数
int GetMonthDay(int year,int month)
{
int arr[14] = {-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;
}
return arr[month];
}
//>运算符重载
bool Date::operator>(const 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;
}
//==运算符重载
bool Date::operator==(const Date& d)
{
return _year==d._year&&_month==d._month&&_day==d._day;
}
//<运算符重载
bool Date::operator<(const Date &d)
{
return !(*this>d||*this==d);
}
//≥运算符重载
bool Date::operator>=(const Date &d)
{
return !(*this<d);
}
//≤运算符重载
bool Date::operator<=(const Date &d)
{
return !(*this>d);
}
//!=运算符重载
bool Date::operator!=(const Date &d)
{
return !(*this==d);
}
//日期+=天数
Date& Date::operator+=(int day)
{
_day+=day;
while(_day>GetMonthDay(_year,_month))
{
_day-=GetMonthDay(_year, _month);
_month++;
if(_month==13)
{
_month = 1;
_year++;
}
}
return *this;
}
//日期-=天数
Date& Date::operator-=(int day)
{
_day-=day;
while(_day<=0)
{
_month--;
if(!_month)
{
_month = 12;
_year--;
}
_day+=GetMonthDay(_year, _month);
}
return *this;
}
//日期-日期
int Date::operator-(const Date &d)
{
int ans = 0;
int flag = 1;
Date max = *this;
Date min = d;
if(*this<d)
{
max = d;
min = *this;
flag = -1;
}
while(min!=max)
{
min+=1;
ans++;
}
return ans*flag;
}
int main()
{
int year1,month1,day1,year2,month2,day2;
scanf("%4d %2d %2d",&year1,&month1,&day1);
scanf("%4d %2d %2d",&year2,&month2,&day2);
Date d1(year1,month1,day1);
Date d2(year2,month2,day2);
printf("%d",d2-d1+1);
return 0;
}