首页 > 代码库 > ZOJ 17届校赛 How Many Nines
ZOJ 17届校赛 How Many Nines
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.
Input
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-D2 are 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++.
Output
For each test case, you should output one line containing one integer, indicating the answer of this test case.
Sample Input
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
Sample Output
4 2 93 1763534
Hint
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.
一开始年份一年年搜TLE了,然后开个数组记录一下不用特判的年份就过了
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stack> 6 #include<map> 7 #include<set> 8 #include<queue> 9 #include<string> 10 #include<vector> 11 using namespace std; 12 13 int y1,y2,m1,m2,d1,d2; 14 long long res; 15 int day[2][13]={0,31,28,31,30,31,30,31,31,30,31,30,31, 16 0,31,29,31,30,31,30,31,31,30,31,30,31}; 17 18 //判断年份有几个9 19 int f(int year) 20 { 21 int a; 22 int sum=0; 23 for(int i=1;i<=4;i++) 24 { 25 a=year%10; 26 if(a==9) 27 sum++; 28 year/=10; 29 } 30 return sum; 31 } 32 33 long long y[10000]; 34 35 int main() 36 { 37 int T,i,flag_r,ynine; 38 long long temp; 39 y[1999]=0; 40 for(i=2000;i<=9999;i++) 41 { 42 //如果是闰年 43 if((i%400==0)||(i%4==0&&i%100!=0)) 44 { 45 temp=f(i)*366/*年*/+30/*月*/+12*3; 46 } 47 //如果不是闰年 48 else 49 { 50 temp=f(i)*365/*年*/+30/*月*/+12*3-1; 51 } 52 y[i]=y[i-1]+temp; 53 } 54 while(~scanf("%d",&T)) 55 { 56 while(T--) 57 { 58 scanf("%d%d%d%d%d%d",&y1,&m1,&d1,&y2,&m2,&d2); 59 res=0; 60 if(y1!=y2)//不同年份 61 { 62 //特判y1年 63 if((y1%400==0)||(y1%4==0&&y1%100!=0)) 64 flag_r=1; 65 else 66 flag_r=0; 67 ynine=f(y1); 68 //特判m1月 69 for(i=d1;i<=day[flag_r][m1];i++) 70 { 71 res+=ynine;//年 72 if(m1==9) 73 res++;//月 74 if(i%10==9) 75 res++;//日 76 } 77 //从m1+1月到12月 78 for(i=m1+1;i<=12;i++) 79 { 80 if(i==2) 81 { 82 if(flag_r==1) 83 res+=3; 84 else 85 res+=2; 86 } 87 else 88 res+=3; 89 /*日*/ 90 if(i!=9) 91 res+=ynine*day[flag_r][i]/*年*/+0/*月*/; 92 else 93 res+=ynine*day[flag_r][i]+30; 94 } 95 // 96 //判断从y1+1到y2-1年 97 res+=y[y2-1]-y[y1]; 98 //特判y2 99 if((y2%400==0)||(y2%4==0&&y2%100!=0)) 100 flag_r=1; 101 else 102 flag_r=0; 103 ynine=f(y2); 104 //从1月到m2-1月 105 for(i=1;i<=m2-1;i++) 106 { 107 if(i==2) 108 { 109 if(flag_r==1) 110 res+=3; 111 else 112 res+=2; 113 } 114 else 115 res+=3; 116 if(i!=9) 117 res+=ynine*day[flag_r][i]+0; 118 else 119 res+=ynine*day[flag_r][i]+30; 120 } 121 //特判m2月 122 for(i=1;i<=d2;i++) 123 { 124 res+=ynine; 125 if(m2==9) 126 res++; 127 if(i%10==9) 128 res++; 129 } 130 } 131 else if(m1!=m2)//同年不同月 132 { 133 if((y1%400==0)||(y1%4==0&&y1%100!=0)) 134 flag_r=1; 135 else 136 flag_r=0; 137 ynine=f(y1); 138 //特判m1月 139 for(i=d1;i<=day[flag_r][m1];i++) 140 { 141 res+=ynine; 142 if(m1==9) 143 res++; 144 if(i%10==9) 145 res++; 146 } 147 //从m1+1月到m2-1月 148 for(i=m1+1;i<=m2-1;i++) 149 { 150 if(i==2) 151 { 152 if(flag_r==1) 153 res+=3; 154 else 155 res+=2; 156 } 157 else 158 res+=3; 159 if(i!=9) 160 res+=ynine*day[flag_r][i]+0; 161 else 162 res+=ynine*day[flag_r][i]+30; 163 } 164 //特判m2月 165 for(i=1;i<=d2;i++) 166 { 167 res+=ynine; 168 if(m2==9) 169 res++; 170 if(i%10==9) 171 res++; 172 } 173 } 174 else//同年同月 175 { 176 if((y1%400==0)||(y1%4==0&&y1%100!=0)) 177 flag_r=1; 178 else 179 flag_r=0; 180 ynine=f(y1); 181 for(i=d1;i<=d2;i++) 182 { 183 if(m1==9) 184 res+=f(y1)+1; 185 else 186 res+=f(y1); 187 if(i%10==9) 188 res++; 189 } 190 } 191 printf("%lld\n",res); 192 } 193 } 194 return 0; 195 }
ZOJ 17届校赛 How Many Nines