Description
给定 2 个日期 yyyy.mm.dd 求两个日期间相差的天数。
Input
第 1 行为一个正整数T,表示测试数。
对于每个测试点,第 1 行与第 2 行分别有两个日期 yyyy.mm.dd。
Output
对于每个测试点,输出一行数字,表示相差的天数。
Sample Input
2
2000.02.28
2000.03.01
6297.01.21
2351.11.27
2000.02.28
2000.03.01
6297.01.21
2351.11.27
Sample Output
2
2
1440938
2
1440938
解题思路:
将年月日转化为总的天数,就是从0年0月0日到输入的年月日的天数,其中需要计算总共有多少个闰年,是闰年的就加一。各个月份的天数用两个数组表示,一个数组表示普通年份,一个数组表示闰年。最后再将这两个值想减,得到间隔天数。
附源码:
1
#include
<
iostream
>
2 using namespace std;
3
4 bool runnian( int year)
5 {
6 if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0 ))
7 {
8 return true ;
9 }
10 else return false ;
11 }
12 int year1,mouth1,day1,year2,mouth2,day2;
13
14 int longmouth[ 12 ] = { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
15
16 int shortmouth[ 12 ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
17
18 long days( int year, int mouth, int day)
19 {
20 long amounts = 0 ;
21 int i,j;
22 for (i = 0 ;i < year;i ++ )
23 {
24 if (runnian(i))amounts ++ ;
25 }
26 amounts += year * 365 ;
27 if (runnian(year))
28 {
29 for (i = 0 ;i < mouth - 1 ;i ++ )
30 amounts += longmouth[i];
31 }
32 else
33 {
34 for (i = 0 ;i < mouth - 1 ;i ++ )
35 amounts += shortmouth[i];
36 }
37 amounts += day;
38 return amounts;
39 }
40
41 int main()
42 {
43 int t;
44 long d;
45 char str[ 11 ];
46 cin >> t;
47 while (t -- )
48 {
49 cin >> str;
50 year1 = (str[ 0 ] - ' 0 ' ) * 1000 + (str[ 1 ] - ' 0 ' ) * 100 + (str[ 2 ] - ' 0 ' ) * 10 + (str[ 3 ] - ' 0 ' );
51 mouth1 = (str[ 5 ] - ' 0 ' ) * 10 + (str[ 6 ] - ' 0 ' );
52 day1 = (str[ 8 ] - ' 0 ' ) * 10 + (str[ 9 ] - ' 0 ' );
53 cin >> str;
54 year2 = (str[ 0 ] - ' 0 ' ) * 1000 + (str[ 1 ] - ' 0 ' ) * 100 + (str[ 2 ] - ' 0 ' ) * 10 + (str[ 3 ] - ' 0 ' );
55 mouth2 = (str[ 5 ] - ' 0 ' ) * 10 + (str[ 6 ] - ' 0 ' );
56 day2 = (str[ 8 ] - ' 0 ' ) * 10 + (str[ 9 ] - ' 0 ' );
57 if ((d = days(year1,mouth1,day1) - days(year2,mouth2,day2)) > 0 )
58 cout << d << endl;
59 else cout <<- d << endl;
60 }
61 return 1 ;
62 }
2 using namespace std;
3
4 bool runnian( int year)
5 {
6 if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0 ))
7 {
8 return true ;
9 }
10 else return false ;
11 }
12 int year1,mouth1,day1,year2,mouth2,day2;
13
14 int longmouth[ 12 ] = { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
15
16 int shortmouth[ 12 ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
17
18 long days( int year, int mouth, int day)
19 {
20 long amounts = 0 ;
21 int i,j;
22 for (i = 0 ;i < year;i ++ )
23 {
24 if (runnian(i))amounts ++ ;
25 }
26 amounts += year * 365 ;
27 if (runnian(year))
28 {
29 for (i = 0 ;i < mouth - 1 ;i ++ )
30 amounts += longmouth[i];
31 }
32 else
33 {
34 for (i = 0 ;i < mouth - 1 ;i ++ )
35 amounts += shortmouth[i];
36 }
37 amounts += day;
38 return amounts;
39 }
40
41 int main()
42 {
43 int t;
44 long d;
45 char str[ 11 ];
46 cin >> t;
47 while (t -- )
48 {
49 cin >> str;
50 year1 = (str[ 0 ] - ' 0 ' ) * 1000 + (str[ 1 ] - ' 0 ' ) * 100 + (str[ 2 ] - ' 0 ' ) * 10 + (str[ 3 ] - ' 0 ' );
51 mouth1 = (str[ 5 ] - ' 0 ' ) * 10 + (str[ 6 ] - ' 0 ' );
52 day1 = (str[ 8 ] - ' 0 ' ) * 10 + (str[ 9 ] - ' 0 ' );
53 cin >> str;
54 year2 = (str[ 0 ] - ' 0 ' ) * 1000 + (str[ 1 ] - ' 0 ' ) * 100 + (str[ 2 ] - ' 0 ' ) * 10 + (str[ 3 ] - ' 0 ' );
55 mouth2 = (str[ 5 ] - ' 0 ' ) * 10 + (str[ 6 ] - ' 0 ' );
56 day2 = (str[ 8 ] - ' 0 ' ) * 10 + (str[ 9 ] - ' 0 ' );
57 if ((d = days(year1,mouth1,day1) - days(year2,mouth2,day2)) > 0 )
58 cout << d << endl;
59 else cout <<- d << endl;
60 }
61 return 1 ;
62 }