ZOJ - 3950 How Many Nines(模拟)


题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3950点击打开链接


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.

直接模拟就是 方法可能比较麻烦 

看到大牛的方法 也可以用三维数组记录从起始点开始每一天的9的个数打表 这里就不贴了


#include <bits/stdc++.h>
using namespace std;
int sumr=66;
int sump=65;
int ans=0;
int year[10000];
int yearsum[10000];
void debug()
{
    cout << ans << endl;
}
int judgerp(int x)
{
    if(x%400==0)
        return 1;
    if(x%4==0&&x%100!=0)
        return 1;
    return 0;
}
int cnt(int y1,int y2,int m1,int m2,int d1,int d2)
{
    ans=0;
    ans+=(yearsum[y2-1]-yearsum[y1-1]);
    //debug();
    for(int i=1;i<m1;i++)
    {
        if(i==2&&!judgerp(y1))
            ans-=2;
        else if(i==9)
            ans-=33;
        else
            ans-=3;
    }
    if(d1>9&&d1<=19)
    {
        ans-=1;
    }
    else if(d1>19&&d1<=29)
    {
        ans-=2;
    }
    else if(d1>29)
    {
        ans-=3;
    }
    if(m1==9)
        ans-=d1-1;
    for(int i=1;i<m2;i++)
    {
        if(i==2&&!judgerp(y2))
            ans+=2;
        else if(i==9)
            ans+=33;
        else
            ans+=3;
    }
    if(d2>=9&&d2<19)
    {
        ans+=1;
    }
    else if(d2>=19&&d2<29)
    {
        ans+=2;
    }
    else if(d2>=29)
    {
        ans+=3;
    }
    if(m2==9)
        ans+=d2;

    int day=0;
    for(int i=1;i<m1;i++)
    {
        if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
            day+=31;
        else if(i==2)
        {
            if(!judgerp(y1))
                day+=28;
            else
                day+=29;
        }
        else
            day+=30;
    }
    day+=d1-1;
    int num=year[y1];
    ans-=(day*num);
    day=0;
    for(int i=1;i<m2;i++)
    {
        if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
            day+=31;
        else if(i==2)
        {
            if(!judgerp(y2))
                day+=28;
            else
                day+=29;
        }
        else
            day+=30;
    }
    day+=d2;
    num=year[y2];
    ans+=(day*num);
    return ans;
}
int main()
{
    for(int i=2000;i<10000;i++)
    {
        int mid=i;
        int num=0;
        while(mid!=0)
        {
            if((mid%10)==9)
            {
                num++;
            }
            mid/=10;
        }
        year[i]=num;
    }
    int ansans=0;
     for(int i=2000;i<10000;i++)
    {
        if(judgerp(i))
            ansans+=sumr;
        else
            ansans+=sump;
        int num=year[i];
        if(judgerp(i))
            ansans+=(366*num);
        else
            ansans+=(365*num);
        yearsum[i]=ansans;
    }
    int nn=0;
    scanf("%d",&nn);
    for(int j=0;j<nn;j++)
    {
        int y1,y2,m1,m2,d1,d2;
        scanf("%d%d%d%d%d%d",&y1,&m1,&d1,&y2,&m2,&d2);
        if(y1>y2||(y1==y2&&m1>m2)||(y1==y2&&m1==m2&&d1>d2))
        {
            swap(y1,y2);
            swap(m1,m2);
            swap(d1,d2);
        }
        printf("%d\n",cnt(y1,y2,m1,m2,d1,d2));
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值