ACM刷题之ZOJ————How Many Nines

How Many Nines

Time Limit: 1 Second       Memory Limit: 65536 KB

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 Y1M1D1Y2M2D2, 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.



打表水过


建立一个三维数组 下标代表 年 月 日

然后从20000101 日开始 到99991231 结束

将每天的9的数量加上前一天9的数量存在当天对应的数组里

输出 输入的两个日期的差值就好

记得特判起始日期的9的数量 并加上

下面是ac代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N=1e9+7;

int ni[13]={
	0,31,28,31,30,31,30,31,31,30,31,30,31
};

int nii[13]={
	0,31,29,31,30,31,30,31,31,30,31,30,31
};

int a[10000][13][32];



int main()
{
	//freopen("f:/input.txt", "r", stdin);
	int zu,i,j,k,nian,yue,ri,nian2,yue2,ri2,sum,n,y,r,d1,d2,x,q,pre,s;
	a[2000][1][1]=0;
	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<=nii[j];k++)
				{
					s=0;
					x=10000*i+100*j+k;
					while(x>0)
					{
						q=x%10;
						x/=10;
						if(q==9) ++s; 
					}
		
					a[i][j][k]=pre+s;
					pre=a[i][j][k];
				}
			}
		}else{
			for(j=1;j<=12;j++)
			{
				for(k=1;k<=ni[j];k++)
				{
					s=0;
					x=10000*i+100*j+k;
					while(x>0)
					{
						q=x%10;
						x/=10;
						if(q==9) ++s; 
					}
					a[i][j][k]=pre+s;
					pre=a[i][j][k];
				}
			}
		}
		
		
	}
	
	scanf("%d",&zu);
	while(zu--)
	{
		scanf("%d%d%d%d%d%d",&nian,&yue,&ri,&nian2,&yue2,&ri2);
		
		
		x=10000*nian+100*yue+ri;
		s=0;
		while(x>0)
		{
			q=x%10;
			x/=10;
			if(q==9) ++s; 
		}
		
		printf("%d\n",a[nian2][yue2][ri2]-a[nian][yue][ri]+s);
		
		
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值