编程之美2015资格赛

题目1 : 2月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
//注意细节就ok了
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int t,date1,year1,date2,year2;
char month1[50],month2[50];
char a;

int judgeMonth(char month[]){
    if(month[0] == 'F') return 2;
    else if(month[0] == 'S') return 9;
    else if(month[0] == 'O') return 10;
    else if(month[0] == 'N') return 11;
    else if(month[0] == 'D') return 12;
    else if(month[0] == 'J'){
        if(month[1] == 'a') return 1;
        else{
            if(month[2] == 'n') return 6;
            else return 7;
        }
    }
    else if(month[0] == 'M'){
        if(month[2] == 'y' ) return 5;
        else return 3;
    }
    else{
        if(month[1] == 'u') return 8;
        else return 4;
    }
}

bool judgeYear( int year ){
    if(year % 4 != 0) return false;
    else if(year % 100 != 0) return true;
    else if(year % 400 != 0) return false;
    else return true;
}

int cmpDate( int m1, int d1, int m2, int d2){
    if(m1 > m2) return 1;
    else if(m1 == m2){
        if(d1 > d2) return 1;
        else if(d1 == d2) return 0;
        else return -1;
    }
    else return -1;
}

int main(){
    scanf("%d",&t);
    for(int item = 1; item <= t; item ++){
        cin.get();
        scanf("%s",month1);
        scanf("%d",&date1);
        cin.get();
        scanf("%c",&a);
        scanf("%d",&year1);

        cin.get();
        scanf("%s",month2);
        scanf("%d",&date2);
        cin.get();
        scanf("%c",&a);
        scanf("%d",&year2);

        int m1 = judgeMonth(month1);
        int m2 = judgeMonth(month2);

        int ans = 0;

        if(year1 == year2){
            if(!judgeYear(year1)) ans = 0;
            else{
                if(cmpDate(m1,date1,2,29) <= 0 && cmpDate(m2,date2,2,29) >= 0)
                    ans = 1;
                else
                    ans = 0;
            }
        }
        else if(year1 == year2 - 1){
            if(judgeYear(year1) && cmpDate(m1,date1,2,29) <= 0) ans ++;
            if(judgeYear(year2) && cmpDate(m2,date2,2,29) >= 0) ans ++;
        }
        else{
            if(judgeYear(year1) && cmpDate(m1,date1,2,29) <= 0) ans ++;
            if(judgeYear(year2) && cmpDate(m2,date2,2,29) >= 0) ans ++;

            year1 ++;
            year2 --;
            while(year1 % 4 != 0) year1 ++;
            while(year2 % 4 != 0) year2 --;

            if(year1 <= year2){
                while(year1 <= year2 && year1 % 100 != 0){
                    ans ++;
                    year1 += 4;
                }

                if(year1 % 400 != 0){
                    while(year1 + 100 <= year2){
                        ans = ans + 24;
                        year1 += 100;
                        if(year1 % 400 == 0) break;
                    }
                }

                while(year1 + 400 <= year2){
                    ans = ans + 97;
                    year1 += 400;
                }

                if(year1 <= year2 && judgeYear(year1)) ans++;

                while(year1 + 100 <= year2){
                    ans = ans + 24;
                    year1 += 100;
                }

                while(year1 + 4 <= year2){
                    ans++;
                    year1 += 4;
                }
            }
        }

        printf("Case #%d: %d\n",item, ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值