牛客网HJ73计算日期到天数转换 牛客网KY111日期差值

题目:

计算日期到天数转换_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力https://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 main()
{
	int year, month, day;
	cin >> year >> month >> day;
	static int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int count = 0;
	for (int i = 0; i < month; i++)
	{
		count += arr[i];
	}//利用for循环来迭代到month-1个月的总天数,然后加上最后的天数
	count += day;
	if (month > 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
	{
		//月份大于2并且为闰年count++
		count++;
	}
	cout << count << endl;
	return 0;
}

法二:

#include <iostream>
using namespace std;

int main()
{
	int year, month, day;
	cin >> year >> month >> day;
	static int arr[13] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
    //用数组表示每个月到1月1日的总天数
	int count = 0;
	count = arr[month - 1];
	count += day;
	if (month > 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
	{
		//月份大于2并且为闰年count++
		count++;
	}
	cout << count << endl;
	return 0;
}

法三:

#include <iostream>
using namespace std;

int GetMonthDay(int year, int month)
{
	static int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = arr[month];
	if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
	{
		day++;
	}
	return day;
}
int main()
{
	int year, month, day;
	cin >> year >> month >> day;
	int count = 0;
	for (int i = 1; i < month; i++)
	{
		count += GetMonthDay(year, i);
	}
	count += day;
	cout << count << endl;
	return 0;
}

题二:

日期差值_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c?tpId=62&&tqId=29468&rp=1&ru=/activity/oj&qru=/ta/sju-kaoyan/question-ranking

 代码解析:

#include<iostream>
using namespace std;



class Date
{
public:
	Date(int year, int month, int day)
		:_year(year)
		, _month(month)
		, _day(day)
	{}
	int GetMonthDay(int year, int month)
	{
		static int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		int day = arr[month];
		if (month == 2 && ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
		{
			day++;
		}
		return day;
	}
	Date& operator+=(int day)
	{
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month == 13)
			{
				_year++;
				_month = 1;
			}
		}
		return *this;
	}
	Date& operator++()//前置++
	{
		*this += 1;
		return *this;
	}
	bool operator>(const Date& d)
	{
		if (_year > d._day)
		{
			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;
		}
		else
		{
			return false;
		}
	}
	bool operator==(const Date& d)
	{
		return ((_year == d._year) && (_month == d._month) && (_day == d._day));
	}
	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}
	int operator-(const Date& d)
	{
		Date max = *this;
		Date min = d;
		if (min > max)
		{
			max = d;
			min = *this;
		}
		int n = 0;
		while (min != max)
		{
			++n;
			++min;
		}
		++n;//注意看样例所给的最后还需要再++一次
		return n;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	int d1, d2;
	int y1, m1, day1;
	int y2, m2, day2;
	while (cin >> d1 >> d2)//while循环控制多组输入
	{
		y1 = d1 / 10000;
		y2 = d2 / 10000;
		//求年d/10000得到高四位
		m1 = d1 % 10000 / 100;
		m2 = d2 % 10000 / 100;
		//求月d先模上10000,模10000所得的值不会超过10000
		//也就是低4位的数,然后再将其除100,得到低4位的高2位便取得月份
		day1 = d1 % 100;
		day2 = d2 % 100;
		//求天数直接d模上100,模上100值就不可能超过一百,也就是得到一个两位数,即第四位
		//通过上面控制取数的方式可得几个结论:
		//取模运算,算出来的结果不可能超过模值,当需要取两位数时模100,取三位即模1000
		//取余运算,就好比逻辑右移运算,模10000相当于右移4位高位补0即可
		Date D1(y1, m1, day1);
		Date D2(y2, m2, day2);
		int n = D1 - D2;//运用赋值运算符重载
		cout << n << endl;
	}
	return 0;
}

法二:

相对于法一的方式只是在控制输入的地方有一些简化,运用了sscanf的C语言库函数 

#include<iostream>
using namespace std;

class Date
{
public:
	Date(int year, int month, int day)
		:_year(year)
		, _month(month)
		, _day(day)
	{}
	int GetMonthDay(int year, int month)
	{
		static int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		int day = arr[month];
		if (month == 2 && ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
		{
			day++;
		}
		return day;
	}
	Date& operator+=(int day)
	{
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month == 13)
			{
				_year++;
				_month = 1;
			}
		}
		return *this;
	}
	Date& operator++()//前置++
	{
		*this += 1;
		return *this;
	}
	bool operator>(const Date& d)
	{
		if (_year > d._day)
		{
			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;
		}
		else
		{
			return false;
		}
	}
	bool operator==(const Date& d)
	{
		return ((_year == d._year) && (_month == d._month) && (_day == d._day));
	}
	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}
	int operator-(const Date& d)
	{
		Date max = *this;
		Date min = d;
		if (min > max)
		{
			max = d;
			min = *this;
		}
		int n = 0;
		while (min != max)
		{
			++n;
			++min;
		}
		++n;//注意看样例所给的最后还需要再++一次
		return n;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	char d1[9], d2[9];//定义一个数组
	int y1, m1, day1;
	int y2, m2, day2;
	while (cin >> d1 >> d2)//while循环控制多组输入
	{
		sscanf(d1, "%4d%2d%2d", &y1, &m1, &day1);//格式化读入
		sscanf(d2, "%4d%2d%2d", &y2, &m2, &day2);
		Date D1(y1, m1, day1);
		Date D2(y2, m2, day2);
		int n = D1 - D2;//运用赋值运算符重载
		cout << n << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

~|Bernard|

你的鼓励是我写下去最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值