How Many Nines ZOJ - 3950

博客围绕日期格式 YYYY - MM - DD 展开,探讨在给定日期区间 [Y1 - M1 - D1, Y2 - M2 - D2] 内数字9出现的总次数,需考虑闰年情况。给出输入输出格式及示例,还提醒因含大I/O文件,建议用更快I/O方法。

How Many Nines ZOJ - 3950

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
借鉴了别的博客,万分感谢大佬

#include <stdio.h>
int is_leap(int n)//判断闰年
{
    if((n%400==0)||(n%4==0&&n%100!=0))
        return 1;
    return 0;
}
int fin9(int n)//s数字中9的个数
{
    int z=0;
    while(n>0)
    {
        if(n%10==9) z++;
        n=n/10;
    }
    return z;
}
int num[10008];//每年中9的总个数
void QN()
{
    int i;
    for(i=2000; i<=9999; i++)
    {
        if(is_leap(i))
        {
            num[i]+=fin9(i)*366;//年中的9
        }
        else num[i]+=fin9(i)*365;
        num[i]=num[i]+30+11*3;//9月月中的9和除2月外每月的天中的9
        if(is_leap(i))//2月中天的9
            num[i]+=3;
        else num[i]+=2;
    }
}
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()
{  
     QN();//放输入前面不然会超时
    int T,y1,y2,m1,m2,d1,d2,i;
    scanf("%d",&T);
    while(T--)
    {
        int sum=0;
        scanf("%d%d%d %d%d%d",&y1,&m1,&d1,&y2,&m2,&d2);
//**********同年**********************************
        if(y1==y2)
        {
            int days=0;
            //×××××××××那部分完整月××××××××××××
            for(i=m1+1; i<m2; i++)
            {
                if(is_leap(y1)&&i==2)
                {
                    sum+=3;
                }
                else if(!is_leap(y1)&&i==2)
                    sum+=2;

                else if(i==9) sum+=33;
                else sum+=3;
                if(is_leap(y1))
                    days+=mon2[i];
                else days+=mon1[i];
            }
            //×××××××××非完整月××××××××××
            if(m1==m2)
            {
                for(i=d1; i<=d2; i++)
                {
                    sum+=fin9(i);
                    if(m1==9)
                        sum++;
                    days++;
                }
                sum+=days*fin9(y1);
                printf("%d\n",sum);
            }
            else
            {
                if(is_leap(y1))
                {
                    for(i=d1; i<=mon2[m1]; i++)
                    {
                        sum+=fin9(i);
                        if(m1==9)
                            sum++;
                        days++;
                    }
                }
                else
                {
                    for(i=d1; i<=mon1[m1]; i++)
                    {
                        sum+=fin9(i);
                        if(m1==9)
                            sum++;
                        days++;
                    }

                }
                for(i=1; i<=d2; i++)
                {
                    sum+=fin9(i);
                    if(m2==9)
                        sum++;
                    days++;
                }
                sum+=days*fin9(y1);
                printf("%d\n",sum);
            }
        }
//**********************不同年***********************
        else
        {
            sum=0;
            for(i=y1+1; i<y2; i++) //那部分完整年
                sum+=num[i];
            //不完整年
            int days1=0,days2=0;//分别代表两个不完整年中的那部分不完整月
            for(i=m1+1; i<=12; i++) //y1不完整年中的完整月
            {
                if(is_leap(y1)&&i==2)
                    sum+=3;
                else if(!is_leap(y1)&&i==2)
                    sum+=2;
                else if(i==9) sum+=33;
                else sum+=3;
                if(is_leap(y1))
                    days1+=mon2[i];
                else days1+=mon1[i];
            }
            if(is_leap(y1))
            {
                for(i=d1; i<=mon2[m1]; i++) //中的不完整月
                {
                    sum+=fin9(i);
                    if(m1==9)
                        sum++;
                    days1++;
                }
            }
            else
            {
                for(i=d1; i<=mon1[m1]; i++)
                {
                    sum+=fin9(i);
                    if(m1==9)
                        sum++;
                    days1++;
                }
            }
            sum+=days1*fin9(y1);

            for(i=1; i<m2; i++) //y2年中的完整月
            {
                if(is_leap(y2)&&i==2)
                {
                    sum+=3;
                }
                else if(!is_leap(y2)&&i==2)
                    sum+=2;
                else  if(i==9) sum+=33;
                else sum+=3;
                if(is_leap(y2))
                    days2+=mon2[i];
                else days2+=mon1[i];
            }
            for(i=1; i<=d2; i++) //中的不完整月
            {
                sum+=fin9(i);
                if(m2==9)
                    sum++;
                days2++;
            }
            sum+=days2*fin9(y2);
            printf("%d\n",sum);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值