zoj3950——How Many Nines(日期模拟)
2017-04-09 22:02
411 查看
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.
求输入的两个时间段内有多少个9
先每年打表,接下来只要判断端点,时间会大大减少
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.
求输入的两个时间段内有多少个9
先每年打表,接下来只要判断端点,时间会大大减少
#include <iostream> #include <cstring> #include <string> #include <vector> #include <queue> #include <cstdio> #include <set> #include <math.h> #include <algorithm> #include <queue> #include <iomanip> #include <map> #define INF 0x3f3f3f3f #define MAXN 1005 #define Mod 99999999 using namespace std; int num[10005]; int count9(int n) { int cnt=0; while(n!=0) { if(n%10==9) cnt++; n/=10; } return cnt; } bool isleap(int y) { if((y%4==0&&y%100!=0)||y%400==0) return true; return false; } void Init() { int sum=0; for(int i=2000; i<=9999; ++i) { if(isleap(i)) sum=count9(i)*366; else sum=count9(i)*365; sum+=30; sum+=11*3; if(isleap(i)) sum+=3; else sum+=2; num[i]=sum; } } int mon1[]= {0,31,28,31,30,31,30,31,31,30,31,30,31}; int mon2[]= {0,31,29,31,30,31,30,31,31,30,31,30,31}; int main() { Init(); int t; scanf("%d",&t); while(t--) { int y1,m1,d1,y2,m2,d2; int ans=0; scanf("%d%d%d%d%d%d",&y1,&m1,&d1,&y2,&m2,&d2); if(y1==y2) { int days=0; for(int i=m1+1; i<m2; ++i) { if(i==2&&isleap(y1)) ans+=3; else if(i==2&&!isleap(y1)) ans+=2; else if(i==9) ans+=33; else ans+=3; if(!isleap(y1)) days+=mon1[i]; else days+=mon2[i]; } if(m1==m2) { for(int i=d1; i<=d2; ++i) { ans+=count9(i); days++; if(m1==9) ans++; } ans=ans+days*count9(y1); printf("%d\n",ans); } else { if(!isleap(y1)) { for(int i=d1; i<=mon1[m1]; ++i) { ans+=count9(i); days++; if(m1==9) ans++; } } else { for(int i=d1; i<=mon2[m1]; ++i) { ans+=count9(i); days++; if(m1==9) ans++; } } for(int i=1; i<=d2; ++i) { ans+=count9(i); days++; if(m2==9) ans++; } ans=ans+days*count9(y1); printf("%d\n",ans); } } else { for(int i=y1+1; i<y2; ++i) ans+=num[i]; int days1=0,days2=0; for(int i=m1+1; i<=12; ++i) { if(i==2&&isleap(y1)) ans+=3; else if(i==2&&!isleap(y1)) ans+=2; else if(i==9) ans+=33; else ans+=3; if(!isleap(y1)) days1+=mon1[i]; else days1+=mon2[i]; } if(!isleap(y1)) { for(int i=d1; i<=mon1[m1]; ++i) { ans+=count9(i); days1++; if(m1==9) ans++; } } else { for(int i=d1; i<=mon2[m1]; ++i) { ans+=count9(i); days1++; if(m1==9) ans++; } } ans=ans+days1*count9(y1); for(int i=1; i<m2; ++i) { if(i==2&&isleap(y2)) ans+=3; else if(i==2&&!isleap(y2)) ans+=2; else if(i==9) ans+=33; else ans+=3; if(!isleap(y2)) days2+=mon1[i]; else days2+=mon2[i]; } for(int i=1; i<=d2; ++i) { ans+=count9(i); days2++; if(m2==9) ans++; } ans=ans+days2*count9(y2); printf("%d\n",ans); } } return 0; }
相关文章推荐
- ZOJ - 3950 How Many Nines 年份大模拟
- ZOJ - 3950 How Many Nines (模拟
- ZOj 3950 How Many Nines (日起模拟,打表,预处理)
- 日期问题模版ZOJ Problem Set -3950 How Many NinesZOJ 找9
- ZOJ - 3950 How Many Nines(模拟)
- How Many Nines (前缀和 + 模拟日期)
- How Many Nines(ZOJ - 3950)
- How Many Nines(ZOJ-3950)
- 【ZOJ3950 The 17th Zhejiang University Programming Contest C】【简单日期模拟】How Many Nines 区间9个数计数
- ZOJ 3950 How Many Nines (预处理)
- How Many Nines(模拟)
- How Many Nines 【日期计算】
- [ZOJ2760]How Many Shortest Path(floyd+最大流)
- C - How Many Nines-模拟题
- ZOJ2760_How Many Shortest Path
- ZOJ 1760 How Many Shortest Path(最短路+网络流之最大流)
- ZOJ 2760 How Many Shortest Path 网络最大流
- ZOJ 2760 How Many Shortest Path (不相交的最短路径个数)
- 【zoj2760】【最大流】How Many Shortest Path
- 解题报告 之 ZOJ2760 How Many Shortest Path