25:计算两个日期之间的天数
总时间限制: 1000ms
内存限制: 65536kB 描述
给定两个日期,计算相差的天数。比如2010-1-1和2010-1-3相差2天。
输入 共两行: 第一行包含三个整数startYear,startMonth,startDay,分别是起始年、月、日。 第二行包含三个整数endYear,endMonth,endDay,分别是结束年、月、日。 相邻两个整数之间用单个空格隔开。
年份范围在1~3000。保证日期正确且结束日期不早于起始日期。
输出 输出一个整数,即是两个日期相差的天数。 样例输入
2008 1 12009 1 1
样例输出
366
提示 闰年被定义为能被4整除的年份,但是能被100整除而不能被400整除的年是例外,它们不是闰年。闰年的2月份有29天。
1 #include<iostream> 2 using namespace std; 3 int bgyear,bgmonth,bgday; 4 int enyear,enmonth,enday; 5 int month[21]={0,31,28,31,30,31,30,31,31,30,31,30,31};//非闰年 6 int rmonth[21]={0,31,29,31,30,31,30,31,31,30,31,30,31};//闰年 7 int flag=1; 8 int tot=0; 9 int main()10 {11 cin>>bgyear>>bgmonth>>bgday;12 cin>>enyear>>enmonth>>enday;13 for(int i=bgyear;i<=enyear+1;i++)//寻找年数上的差异 14 {15 if((i%4==0&&i%100!=0)||(i%400==0))16 {17 for(int j=1;j<=12;j++)18 {19 if(i==bgyear&&j<bgmonth)20 continue;//寻找开始月份21 for(int k=1;k<=rmonth[j];k++)22 {23 if(i==enyear&&j==enmonth&&k==enday)24 {25 cout<<tot;26 return 0;27 }28 if(k<bgday&&flag==1)29 continue;30 else31 {32 flag=0;33 tot++;34 }35 36 }37 38 }39 }//闰年 40 else41 {42 43 for(int j=1;j<=12;j++)44 {45 if(i==bgyear&&j<bgmonth)46 continue;//寻找开始月份47 for(int k=1;k<=month[j];k++)48 {49 if(i==enyear&&j==enmonth&&k==enday)50 {51 cout<<tot;52 return 0;53 }54 if(k<bgday&&flag==1)55 continue;56 else57 {58 flag=0;59 tot++;60 }61 62 }63 64 }65 }//非闰年 66 }67 cout<<tot;68 return 0;69 }
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/167960.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有