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.
The 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="">For each test case, you should output one line containing one integer, indicating the answer of this test case.
<h4< dd="">4 2017 04 09 2017 05 09 2100 02 01 2100 03 01 9996 02 01 9996 03 01 2000 01 01 9999 12 31
4 2 93 1763534
题意:给出两个日期,问两个日期间出现了多少个9(包括两个给定的日期)
思路:预处理出2000-01-01---9999-12-31所有日期的出现的9的情况
#include<iostream>
#include<stdio.h>
using namespace std;
int n_leap[]={0,31,28,31,30,31,30,31,31,30,31,30,31},leap[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int sum[10005][15][40];
int sy,sm,sd,ey,em,ed;
bool is_leap(int y){
if(y%4==0&&y%100||y%400==0) return true;
return false;
}
int cnt(int n){
int num=0;
while(n){
if(n%10==9) num++;
n/=10;
}
return num;
}
void add(int &y,int &m,int &d){
d++;
if(is_leap(y)){
if(d>leap[m]) d=1,m++;
if(m>12) m=1,y++;
}
else{
if(d>n_leap[m]) d=1,m++;
if(m>12) m=1,y++;
}
}
void init(){
int nowy=2000,nowm=1,nowd=1,nexty=2000,nextm=1,nextd=1;
sum[nowy][nowm][nowd]=0;
while(nexty<10001){
sum[nexty][nextm][nextd]=sum[nowy][nowm][nowd]+cnt(nowy)+cnt(nowm)+cnt(nowd);
nowy=nexty,nowm=nextm,nowd=nextd;
add(nexty,nextm,nextd);
}
}
int main(){
init();
int t;scanf("%d",&t);
while(t--){
scanf("%d%d%d%d%d%d",&sy,&sm,&sd,&ey,&em,&ed);
add(ey,em,ed);
printf("%d\n",sum[ey][em][ed]-sum[sy][sm][sd]);
}
}