• hihoCoder2月29日(字符串模拟)


    时间限制:2000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期)。

    只有闰年有2月29日,满足以下一个条件的年份为闰年:

    1. 年份能被4整除但不能被100整除

    2. 年份能被400整除

    输入

    第一行为一个整数T,表示数据组数。

    之后每组数据包含两行。每一行格式为"month day, year",表示一个日期。month为{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"}中的一个字符串。day与year为两个数字。

    数据保证给定的日期合法且第一个日期早于或等于第二个日期。

    输出

    对于每组数据输出一行,形如"Case #X: Y"。X为数据组数,从1开始,Y为答案。

    数据范围

    1 ≤ T ≤ 550

    小数据:

    2000 ≤ year ≤ 3000

    大数据:

    2000 ≤ year ≤ 2×109

    样例输入
    4
    January 12, 2012
    March 19, 2012
    August 12, 2899
    August 12, 2901
    August 12, 2000
    August 12, 2005
    February 29, 2004
    February 29, 2012
    样例输出
    Case #1: 1
    Case #2: 0
    Case #3: 1
    Case #4: 3

    本题涉及到求两个时段之间闰年的公式
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    bool isLeap(int y)
    {
        if((y%4==0&&y%100!=0)||y%400==0)
            return true;
        return false;
    }
    struct Date{
        char mon[20];
        int day;
        long long year;
        Date()
        {
            memset(mon,0,sizeof(mon));
        }
    };
    Date Beg;
    Date End;
    LL Leaps(LL y)
    {
        return y/4-y/100+y/400;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        int cas=1;
        while(cas<=T)
        {
            scanf("%s",Beg.mon);
            scanf("%d,",&Beg.day);
            scanf("%lld",&Beg.year);
            scanf("%s",End.mon);
            scanf("%d,",&End.day);
            scanf("%lld",&End.year);
            LL res=0;
            if(isLeap(Beg.year))
            {
                if(!strcmp("January",Beg.mon)||!strcmp("February",Beg.mon))
                    res++;
            }
            if(isLeap(End.year))
            {
                if(!strcmp("February",End.mon)&&End.day==29||(strcmp("January",End.mon)!=0&&strcmp("February",End.mon)!=0))
                {
                    res++;
                }
            }
            if(End.year==Beg.year&&isLeap(Beg.year)&&isLeap(End.year))    
                res--;
            if(End.year-1>Beg.year)
                res+=(Leaps(End.year-1)-Leaps(Beg.year));
            printf("Case #%d: %lld
    ",cas++,res);
        }
        
        return 0;
    }

     选一个基准日期,求两日期到基准日期距离的差。

    #include <iostream>
    #include <cstring>
    using namespace std;
    char month[15][20]={"Null","January","February","March","April","May","June","July", "August", "September", "October", "November","December"};
    int d1,m1,y1;
    int d2,m2,y2;
    char mon1[20],mon2[20];
    int sMon(char s[])
    {
        for(int i=2;i<=12;i++)
        {
            if(strcmp(month[i],s)==0)
            {
                return i;
            }
        }
    }
    bool isLeap(int year)
    {
        if((year%4==0&&year%100!=0)||year%400==0)
        {
            return true;
        }
        return false;
    }
    int leaps(int year)
    {
        return year/4-year/100+year/400;
    }
    int main()
    {
        int T;
        cin>>T;
        for(int cas=1;cas<=T;cas++)
        {
            int lp1=0;
            int lp2=0;
            cin>>mon1>>d1;
            cin.get();
            cin>>y1;
            cin>>mon2>>d2;
            cin.get();
            cin>>y2;
            m1=sMon(mon1);
            m2=sMon(mon2);
            lp1+=leaps(y1-1);
            lp2+=leaps(y2-1);
            if(isLeap(y1))
            {
                if(m1>2)
                {
                    lp1++;
                }
            }
            if(isLeap(y2))
            {
                if((m2==2&&d2==29)||m2>2)
                {
                    lp2++;
                }
            }
            cout<<"Case #"<<cas<<": "<<lp2-lp1<<endl;    
        }
        return 0;
    }
  • 相关阅读:
    WINFORM 設計時 未能加载文件或程序集問題解決
    Remove row from generic datatable in C#(The given DataRow is not in the current DataRowCollection)
    questa.sim in the linux
    the io_printf
    how to search new file in linux
    the rld control core
    window's chkdsk
    tq
    the init state machine
    brazen out
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5215731.html
Copyright © 2020-2023  润新知