5000个网站资源做外链,外贸网店,新余网站建设公司,天津做网站好的公司有哪些问题描述
描述 有两个日期#xff0c;求两个日期之间的天数#xff0c;如果两个日期是连续的我们规定他们之间的天数为两天。 输入描述#xff1a; 有多组数据#xff0c;每组数据有两行#xff0c;分别表示两个日期#xff0c;形式为YYYYMMDD 输出描述#xff1a; 每组…问题描述
描述 有两个日期求两个日期之间的天数如果两个日期是连续的我们规定他们之间的天数为两天。 输入描述 有多组数据每组数据有两行分别表示两个日期形式为YYYYMMDD 输出描述 每组数据输出一行即日期差值 示例 输入 20110412 20110422 输出 11 问题分析 这是一个处理分析问题。 解决方案 我们只需要算出输入的日期距最早的的年份总共多少天即可 小问题1我们怎么计算出距离天数 答我们可以通过最早的那一年的第一天距离两个日期的天数之和作为计算对象 小问题2怎么计算上述天数 答实现天数计算函数即可——实现两个函数分别计算平年闰年的月天数和年天数 代码
//思路我们只需要算出输入的日期距最早的的年份总共多少天即可
#include iostream
using namespace std;int GetMonthDay(int year, int month) //得到一个月的月数
{const static int day_arr[13] { 0, 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 day_arr[month];
}int GetYearhDay(int year) //得到一个年的月数
{if (year % 4 0 year % 100 ! 0 || (year % 400 0))return 366;elsereturn 365;
}int MonthSum(int year, int month, int day) //得到不满一年的月数总天数
{int sum 0;for (int i 1; i month; i){sum GetMonthDay(year, i);}return sum day;
}int SumDay(const int date1, const int date2)
{int max 0;int min 0;//找到较大的日期maxif (date1 date2){max date1;min date2;}else {max date2;min date1;}int ret_day1 0;int ret_day2 0;//返回的天数即为两个日期的差//年日期差while ((max / 10000) ! (min) / 10000){ret_day1 GetYearhDay(date1 / 10000);max - 10000;}ret_day2 MonthSum(min / 10000, (min % 10000) / 100, min % 100);ret_day1 MonthSum(max / 10000, (max % 10000) / 100, max % 100);return ret_day1 - ret_day2 1;
}int main() {int date1;int date2;cin date1;cin date2;cout SumDay(date1, date2);}