2015编程之美资格赛第一题

来源:互联网 发布:seo教程入门教程 编辑:程序博客网 时间:2024/04/30 15:37
#include<iostream>#include<stdio.h>#include<string>#include<string.h>using namespace std;struct Date{int month;int day;int year;};//把月份由数字转换成数字型int str2IntMonth(char month[]){string months[12]={"January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November" , "December"};for(int i=0;i<12;i++){if(string(month)==months[i]){return i+1;}}return -1;}//是否闰年bool isLeapYear(int year){if((year%400==0)||((year%4==0)&&(year%100!=0)))return true;return false;}//验证日期是否合法bool isDateLegal(Date date){if(date.year>=2000&&date.year<=3000){if(date.month>=1&&date.month<=12){if(date.month==2){if(isLeapYear(date.year)){if(date.day<=29&&date.day>=1)return true;elsereturn false;}else {if(date.day<=28&&date.day>=1)return true;else return false;}}else if(date.month==1||date.month==3||date.month==5||date.month==7||date.month==8||date.month==10||date.month==12){if(date.day<=31&&date.day>=1)return true;elsereturn false;}else{if(date.day<=30&&date.day>=1)return true;elsereturn false;}}}return false;}//验证两个日期是否合法,第一个早于或等于第二个日期即合法bool isNormal(Date first,Date second){if((first.year>second.year)||((first.year==second.year)&&(first.month>second.month))||((first.year==second.year)&&(first.month==second.month)&&(first.day>second.day)))return false;return true;}//计算两个日期之间有多少个2月29int count2m29(Date first,Date second){Date cmp229={2,29,first.year};int count=0;if(!isNormal(first,second))//是否是第一个日期早于第二个日期{cout<<"第一个日期迟于第二个日期!"<<endl;return -1;}while(cmp229.year>=first.year&&cmp229.year<=second.year){if(isNormal(first,cmp229)&&isNormal(cmp229,second)){if(isLeapYear(cmp229.year)){count++;}}cmp229.year++;}return count;}int main(){int T,num;char start[50]={0},over[50]={0},monthtmp[15]={0};Date first={1,1,2000},second={1,1,2000};cin>>T;if(T<1||T>550)return -1;num=0;while(T--){memset(start,0,sizeof(char)*50);memset(over,0,sizeof(char)*50);memset(monthtmp,0,sizeof(char)*15);num++;getchar();cin.get(start,50);getchar();cin.get(over,50);sscanf(start,"%s %d, %d",monthtmp,&first.day,&first.year);first.month=str2IntMonth(monthtmp);if(!isDateLegal(first))//是否合法{cout<<"日期不合法!"<<endl;return -1;}memset(monthtmp,0,sizeof(char)*15);sscanf(over,"%s %d, %d",monthtmp,&second.day,&second.year);second.month=str2IntMonth(monthtmp);if(!isDateLegal(second))//是否合法{cout<<"日期不合法!"<<endl;return -1;}printf("Case #%d: %d\n",num,count2m29(first,second));}return 0;}

时间限制: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

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