zoukankan      html  css  js  c++  java
  • 2月29日(编程之美2015资格赛)

    时间限制: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 <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    typedef struct Detailday
    {
        string month;
        int day;
        int year;
    }   Detailday;
    
    bool isSpecialyear(int year)
    {
        bool flag = false;
        if(year%400==0||(year%4==0&&year%100!=0))
            flag = true;
        return flag;
    }
    
    int main()
    {
        int num;
        cin>>num;
        Detailday* data = new Detailday[2*num];
        char tempmonth[20];
        int tempday,tempyear;
        for(int i = 0;i < 2*num; i++)
        {
            scanf("%s %d,%d",&tempmonth,&tempday,&tempyear);
            data[i].month = tempmonth;
            data[i].day = tempday;
            data[i].year = tempyear;
        }
        //for(int i = 0;i < 2*num; i++)
        //cout<<data[i].month<<endl;
    
        for(int i = 0;i < num; i++)
        {
            int count = 0;
            int head = 0,rear = 0;
            for(int j = data[2*i].year;j <= data[2*i+1].year; j++)
                if(isSpecialyear(j))
                {
                    head = j;
                    break;
                }
    
            for(int j = data[2*i+1].year;j >= data[2*i].year; j--)
                if(isSpecialyear(j))
                {
                    rear = j;
                    break;
                }
            //cout<<head<<" he "<<rear<<endl;
            if(head==0&&rear==0)
            {
                cout<<"Case #"<<(i+1)<<": "<<0<<endl;
                continue;
            }
            if(head<=rear)
            {
                //cout<<"head: "<<head<<" rear: "<<rear<<endl;
                for(int k = head;k <= rear; k += 4)
                    if(isSpecialyear(k))
                     {
                         count++;
                         //cout<<k<<" ";
                     }
                //cout<<endl;
                if(isSpecialyear(data[2*i].year)&&data[2*i].month!="January"&&data[2*i].month!="February")
                    count--;
                if(isSpecialyear(data[2*i+1].year)&&((data[2*i+1].month=="January")||(data[2*i+1].month=="February"&&data[2*i+1].day<=28)))
                    count--;
            }
            cout<<"Case #"<<(i+1)<<": "<<count<<endl;
        }
        return 0;
    }
    

      

    态度决定高度,细节决定成败,
  • 相关阅读:
    28. Implement strStr()(KMP字符串匹配算法)
    60. Permutation Sequence(求全排列的第k个排列)
    47. Permutations II (全排列有重复的元素)
    46. Permutations (全排列)
    3. Longest Substring Without Repeating Characters(最长子串,双指针+hash)
    Python 一行代码实现并行
    1. Two Sum
    236. Lowest Common Ancestor of a Binary Tree(最低公共祖先,难理解)
    ssm项目配置多个数据源
    SpringMVC架构实现原理
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/4435671.html
Copyright © 2011-2022 走看看