-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_C1928.cpp
103 lines (95 loc) · 1.98 KB
/
3_C1928.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* P91 日期差距计算
*/
#include <iostream>
using namespace std;
// 数组表示[平年, 闰年]每个月的天数
int month[12][2] = {
{31, 31},
{28, 29},
{31, 31},
{30, 30},
{31, 31},
{31, 31},
{31, 31},
{31, 31},
{30, 30},
{31, 31},
{30, 30},
{31, 31},
};
// 判断是否是闰年:可以被4整除,而不能被100整除,或者可以被400整除
int isLeapYear(int year)
{
// 闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
return 1;
}
// 平年
return 0;
}
// calculate function
int calculateFunc(int timeMin, int timeMax)
{
if (timeMin == timeMax)
{
return 0;
}
if (timeMax < timeMin)
{
// change
int temp = timeMax;
timeMax = timeMin;
timeMin = temp;
}
int ymin = timeMin / 10000;
int ymax = timeMax / 10000;
int mmin = (timeMin % 10000) / 100;
int mmax = (timeMax % 10000) / 100;
int dmin = timeMin % 100;
int dmax = timeMax % 100;
// 通过不断增加timeMin,使其与timeMax值相等
// start
int step = 1;
while (ymin < ymax || mmin < mmax || dmin < dmax)
{
// day ++
dmin += 1;
// 进位
if (dmin == month[mmin][isLeapYear(ymin)] + 1)
{
mmin += 1;
dmin = 1;
}
if (mmin == 12 + 1)
{
ymin += 1;
mmin = 1;
}
// step ++
step += 1;
}
return step;
}
int main()
{
int time1, time2;
cout << "Input time1(YYYYMMDD): ";
cin >> time1;
if (time1 % 100 < 1 || time1 % 100 > 31)
{
cout << "invalid format." << endl;
return 0;
}
cout << "Input time2(YYYYMMDD): ";
cin >> time2;
if (time2 % 100 < 1 || time2 % 100 > 31)
{
cout << "invalid format." << endl;
return 0;
}
int result = calculateFunc(time1, time2);
cout << "difference: " << result << endl;
return 0;
}