标签:
给定两个日期,计算这两个日期之间有多少个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<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) typedef long long ll; ll mul(ll a,ll b){return (a*b)%F;} ll add(ll a,ll b){return (a+b)%F;} ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;} void upd(ll &a,ll b){a=(a%F+b%F)%F;} int T; char s1[12][50]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"}; char s[10000]; int d,y; void get_init(int &m,int &d,int &y) { scanf("%s %d, %d",s,&d,&y); Rep(i,12) if (strcmp(s,s1[i])==0) { m=i+1;break; } } bool is_r(int y) { if ((y%4==0&&y%100!=0)||y%400==0) return 1; return 0; } int is_pre(int m,int d) { if (m<=2) return 1; return 0; } int calc(int y) { return y/4-y/100+y/400; } int main() { freopen("day.in","r",stdin); // freopen("day.out","w",stdout); scanf("%d",&T); For(kcase,T) { int m1,d1,y1; get_init(m1,d1,y1); int m2,d2,y2; get_init(m2,d2,y2); int ans=0; if (y1==y2) { if (is_r(y1) && ( m1<=2 ) && ( m2>2 || (m2==2 && d2 >= 29 ) ) ) ans+=1; } if (y1<y2) { if (is_r(y1) && ( m1<=2 ) ) ans+=1; if (is_r(y2) && ( m2>2 || (m2==2 && d2 >= 29 ) ) ) ans+=1; ans+=calc(y2-1)-calc(y1); } printf("Case #%d: %d\n",kcase,ans); } return 0; }
标签:
原文地址:http://blog.csdn.net/nike0good/article/details/45150939