给定两个日期,计算这两个日期之间有多少个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;
}
原文:http://www.cnblogs.com/lxk2010012997/p/4435671.html