If we represent a date in the format YYYY-MM-DD (for example, 2017-04-09), do you know how many 9s will appear in all the dates between Y1-M1-D1 and Y2-M2-D2 (both inclusive)?
Note that you should take leap years into consideration. A leap year is a year which can be divided by 400 or can be divided by 4 but can't be divided by 100.
InputThe first line of the input is an integer T (1 ≤ T ≤ 105), indicating the number of test cases. Then T test cases follow. For each test case:
The first and only line contains six integers Y1, M1, D1, Y2, M2, D2, their meanings are described above.
It's guaranteed that Y1-M1-D1 is not larger than Y2-M2-D2. Both Y1-M1-D1 and Y2-M2-D2are between 2000-01-01 and 9999-12-31, and both dates are valid.
We kindly remind you that this problem contains large I/O file, so it's recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++.
<h4< dd="">OutputFor each test case, you should output one line containing one integer, indicating the answer of this test case.
<h4< dd="">Sample Input4 2017 04 09 2017 05 09 2100 02 01 2100 03 01 9996 02 01 9996 03 01 2000 01 01 9999 12 31<h4< dd="">Sample Output
4 2 93 1763534Hint
For the first test case, four 9s appear in all the dates between 2017-04-09 and 2017-05-09. They are: 2017-04-09 (one 9), 2017-04-19 (one 9), 2017-04-29 (one 9), and 2017-05-09 (one 9).
For the second test case, as year 2100 is not a leap year, only two 9s appear in all the dates between 2100-02-01 and 2100-03-01. They are: 2017-02-09 (one 9) and 2017-02-19 (one 9).
For the third test case, at least three 9s appear in each date between 9996-02-01 and 9996-03-01. Also, there are three additional nines, namely 9996-02-09 (one 9), 9996-02-19 (one 9) and 9996-02-29 (one 9). So the answer is 3 × 30 + 3 = 93.
Question: Given two dates, find the number of nines between the two dates;
Idea: Let a[i][j][k] be the number of 9s from January 01, 2000 to i year j month k day, first calculate all the conditions, and finally subtract the two dates. .
Code:
#include<cstdio> #include<cstdlib> #include<algorithm> #include<queue> #include<cstring> #include<iostream> #include<set> #include<queue> #include<functional> #include<string> #include<map> using namespace std; int judge(int x)//judgment how many 9s { int n; int flag = 0; while (x > 0) { n = x % 10; if (n == 9) flag++; x = x / 10; } return flag; } bool isleap(int x) { if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0) return true; else return false; } int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int leapdays[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31}; int a[10000][13][32]; intmain() { int x; int t; scanf("%d", &t); int i, j, k; int years; int y1, m1, d1, y2, m2, d2; a[2000][1][1] = 0; int pre = 0; for (i = 2000; i <= 9999; i++) { if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) { for (j = 1; j <= 12; j++) { for (k = 1; k <= leapdays[j]; k++) { x = i * 10000 + j * 100 + k;//Determine how many 9s there are on this day a[i][j][k] = judge(x) + pre; pre = a [i] [j] [k]; } } } else { for (j = 1; j <= 12; j++) { for (k = 1; k <= days[j]; k++) { x = i * 10000 + j * 100 + k; a[i][j][k] = judge(x) + pre; pre = a [i] [j] [k]; } } } } for (int i = 0; i < t; i++) { scanf("%d%d%d", &y1, &m1, &d1); scanf("%d%d%d", &y2, &m2, &d2); int x = y1 * 10000 + m1 * 100 + d1; ans = judge(x); printf("%d\n", a[y2][m2][d2] - a[y1][m1][d1]+ans); } return 0; }