25: calculates the number of days between two dates, and 25 calculates the number of days of a date.
25: calculate the number of days between two dates
- View
- Submit
- Statistics
- Question
-
Total time limit:
-
1000 ms
-
Memory limit:
-
65536kB
-
Description
-
Calculate the number of days for the difference given two dates. For example, the difference between and is two days.
-
Input
-
Two rows in total:
The first line contains three integers: startYear, startMonth, and startDay, which are the start year, month, and day.
The second row contains three integers: endYear, endMonth, and endDay.
Two Adjacent integers are separated by a single space.
The year range is 1 ~ 3000. Ensure that the date is correct and the end date is not earlier than the start date.
-
Output
-
Returns an integer, that is, the number of days between two dates.
-
Sample Input
-
2008 1 12009 1 1
-
Sample output
-
366
-
Prompt
-
A leap year is defined as a year that can be divided by four, but can be divided by 100 instead of 400. They are not leap years. A leap year has 29 days in January.
-
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}; // non-leap year 6 int rmonth [21] = {, 29,31, 30,31, 30,31, 31,30, 31,30, 31 }; // 7 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 ++) // find the difference in the number of years 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; // search for the start month 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} // leap year 40 else41 {42 43 for (int j = 1; j <= 12; j ++) 44 {45 if (I = bgyear & j <bgmonth) 46 continue; // find the start month 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} // non-leap year 66} 67 cout <tot; 68 return 0; 69}