题解 | #日期差值#

日期差值

https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c

#include <climits>
#include <iostream>
using namespace std;
class Date
{
	//friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	Date(int year, int month, 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);
	Date& operator+=(int day);
	Date& operator-=(int day);
	Date operator+(int day);
	Date operator-(int day);
	Date& operator++();//前置
	Date operator++(int);//后置
	Date& operator--();//前置
	Date operator--(int);//后置
	int operator-(const Date&d);
	inline int GetDay(int year,int month)//获取天数,内联函数
	{
		static int GetMonthDay[13] = { -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 GetMonthDay[month];
		}
	}
//private:
	int _year;
	int _month;
	int _day;
};
//ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in,  Date& d);
Date::Date(int year , int month , int day)
{
	_year = year;
	_month = month;
	_day = day;
}
bool Date::operator==(const Date& d)
{
	return _year == d._month
		&& _month == d._month
		&& _day == d._day;
}
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
		else if(_month==d._month)
		{
			if (_day > d._day)
			{
				return true;
			}
		}
	}
	return false;
}
bool Date::operator<=(const Date& d)
{
	return (! (*this > d)) || (*this == d);
}
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);
}
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	while (_day > GetDay(_year,_month))
	{
		_day -= GetDay(_year,_month);
		++_month;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;
}
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
		{
			--_month;
			if (_month == 0)
			{
				-- _year;
				_month = 12;
			}
			_day += GetDay(_year, _month);
		}
	if (_day > GetDay(_year, _month))
	{
		_day = GetDay(_year, _month) - _day;
	}
	return *this;
}
Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}
Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;//复用
	return tmp;
}
Date& Date::operator++()//前置
{
	*this += 1;
	return *this;
}
Date Date:: operator++(int)//后置
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
Date& Date:: operator--()//前置
{
	*this -= 1;
	return *this;
}
Date Date::operator--(int)//后置
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
int Date::operator-(const Date& d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;
		min = *this;
	}
	int day = 0;
	while (min < max)
	{
		++(min);
		++day;
	}
	return day * flag;

}
istream& operator>>(istream& in, Date& d)
{
	in >> d._year>>d._month>>d._day;
	return in;
}
int main()
{
	Date d1(2024, 1, 2);
    Date d2(0, 0, 0);
    scanf("%4d%2d%2d", &d1._year,&d1._month,&d1._day);
     scanf("%4d%2d%2d", &d2._year, &d2._month, &d2._day);
     int count = ( d1 - d2);
     cout << count<< endl;
	return 0;
}

全部评论

相关推荐

2025-11-21 03:09
已编辑
南昌大学 golang
bg普211本,走的golang后端方向。找实习经历:最近一个月投了一些日常,面了4场,都是一面挂。简历包装成分比较多,当时这个简历准备了两个星期,问AI解决什么问题用什么技术,跟其他技术对比优缺点在哪,等等。但是面试的时候一些基础的八股都答的模模糊糊,然后项目延伸的场景题一点不会。有点害怕面试,面前焦虑…本文可能带点碎碎念…省流就是因为每周面心态不行,不知道先学什么以及三天打鱼两天晒网…现在的主要问题,一个是只能依靠即时满足无法撑过枯燥的学习,另一个是难以调整心态,面试焦虑。个人背景:主包其实本来是大一开始学后端的,但是当时不知道合适的学习方法(学习路线和借助AI),也社恐不太敢问学长,走了很多弯路,也没有花很多时间在后端上面(按兴趣学的只有大二上学期写了opencamp的rustlings和learning-cxx,还有玩steam的图灵完备,剩余时间比较摆烂)。结果就是现在这鬼样子,只会写crud,差不多就是会gin&nbsp;gorm基础,会写注册登录和简单业务接口,写过几种项目结构和设计模式。缺乏自己延展的能力。计算机基础:也相当差,之前大二学的计网全忘光了,操作系统60飘过。虽然大一的时候打算法竞赛(也没什么成绩就是,省二等奖收集者),但到现在一年半没碰了,就只有dfs,并查集啥的一些很基础的题目随便写,hot100链表因为竞赛没练过相当不熟练。大二下的时候,数据库课看八股,又困又累,什么都没看进去,后面自然又是全忘光了。现在我虽然有了个概览,知道后端除了crud有缓存、微服务、分布式、消息队列等等东西,知道后端架构设计是要做权衡,性能、一致性、容灾,需要通过实验测出具体的数据来做决策,但是具体的方案不会,看基础知识是真看不进去。现在的主要问题,一个是只能依靠即时满足无法撑过枯燥的学习,另一个是难以调整心态。我高中以前一直是优等生,能够享受大部分题目都会的快感,能明确地有信心自己能做出来,解题过程需要进行推理,并且做完立刻就能得到正确反馈,其中的失败调整过程长度也在可接受范围内。(喜欢写rustlings一类的语言lab和玩《图灵完备》大概也是因为这个吧…)而现在的情景相当于我成了高三但是基础知识基本不会的状态,比我当年(会基础知识只是差做题)差多了。在这种情况下去面试也是相当痛苦,因为面试是不知道范围的。每次准备都不知道先看什么,学也学不进去。明明知道面试只是为了了解真实会问什么,但是还是很焦虑,拧巴心态。学长说去投简历面试实践是为了了解自己在哪里,别人在哪里,市场在哪里,但是我似乎还没有找到收敛的下限,只是一直失败…但是我也不能确定不面试就能学进去啊,因为我大二暑假是真的一点代码都不想碰,相当烦躁,八股也不想看。现在甚至连稍微花点时间的算法题(不能即时反馈的)都不想写了。还在纠结要不要整块时间搓项目压测试试,感觉会非常花时间。可能我项目管理也是一坨。
圆规学java:27届不着急,边投边学,克服恐惧感,你现在不敢面试,你为什么认为你暑期就勇敢了,你现在的进度其实还很早,我当时大三下才开始实习,我也很焦虑着急。永远没有准备好的时候,当下努力就是最好的加油!
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务