25:计算两个日期之间的天数
总时间限制: 1000ms 内存限制: 65536kB描述
给定两个日期,计算相差的天数。比如2010-1-1和2010-1-3相差2天。
输入共两行:
第一行包含三个整数startYear,startMonth,startDay,分别是起始年、月、日。
第二行包含三个整数endYear,endMonth,endDay,分别是结束年、月、日。
相邻两个整数之间用单个空格隔开。
年份范围在1~3000。保证日期正确且结束日期不早于起始日期。输出输出一个整数,即是两个日期相差的天数。样例输入
样例输出
提示闰年被定义为能被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()
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 else
31 {
32 flag=0;
33 tot++;
34 }
36 }
38 }
39 }//闰年
40 else
41 {
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 else
57 {
58 flag=0;
59 tot++;
60 }
62 }
64 }
65 }//非闰年
66 }
67 cout<<tot;
68 return 0;
69 }