Given two dates, the task is to calculate the number of days between these two dates.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The input consists of multiple sets of data. Each set of data has two lines, with each line containing three integers representing year, month, and day respectively. The input ends with EOF, and the problem guarantees that there will be no invalid data. | Output the number of days between the two dates. |
2011 10 19 2011 10 18 | 1 |
Thought Process
Convert the two dates into "days" as the smallest unit. It's important to check for leap years and determine the number of leap years occurring between the two dates. Also, consider whether the current month needs to account for the extra leap year day. Finally, output the absolute difference between the two calculated days.
Sample Code-ZeroJudge A263: Date Difference
#include <iostream>
using namespace std;
int main()
{
int y1, m1, d1;
int y2, m2, d2;
int month[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
while (cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2) {
int temp = d1;
int temp1 = d2;
int ld1, ld2;
if (m1 > 2) {
ld1 = (y1 / 4 - y1 / 100 + y1 / 400);
}
else {
ld1 = ((y1 - 1) / 4 - (y1 - 1) / 100 + (y1 - 1) / 400);
}
if (m2 > 2) {
ld2 = (y2 / 4 - y2 / 100 + y2 / 400);
}
else {
ld2 = ((y2 - 1) / 4 - (y2 - 1) / 100 + (y2 - 1) / 400);
}
d1 += y1 * 365 + ld1;
for (int i = 1; i < m1; i++) {
d1 += month[i];
}
d2 += y2 * 365 + ld2;
for (int i = 1; i < m2; i++) {
d2 += month[i];
}
cout << abs(d1 - d2) << endl;
}
return 0;
}
//ZeroJudge A263
//Dr. SeanXD