http://poj.org/problem?id=3393
伤不起呀 题目也太长了吧
不过也学到了点常识
解释见代码注释
#include<iostream> #include<cmath> #include<string> #include<algorithm> #include<queue> #include<cstring> #include<cstdio> using namespace std; const int K=700000000; int Days[15]={0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int Adddays(int ,int); int Findwhichday(int year,int month)//求出 某年某月第一天是周几 { int Y=2006,M=7,Theday=6;//以 2006 年 7 月 第一天是 周六 为标准进行计算 int sum=0; if(year<Y||(year==Y&&month<M))//分两种情况,所求时间在参考时间前还是后 { while(year<Y||(Y==year&&month<M)) { sum+=Adddays(year,month); ++month; if(month==13) {++year;month=1;} } return (Theday+K-sum)%7; } else { while(Y<year||(Y==year&&M<month)) { sum+=Adddays(Y,M); ++M; if(M==13) {++Y;M=1;} } return (sum+Theday)%7; } } int Adddays(int year,int month)//判断某年某月为多少天 { if(year==1752&&month==9) return 19; if(month!=2) return Days[month]; if(year<1582||year==1700) { if(year%4==0) return Days[month]+1; else return Days[month]; } else { if(year%400==0||(year%4==0&&year%100!=0)) return Days[month]+1; else return Days[month]; } } int main() { int Startyear,Startmonth; int Endyear,Endmonth; int T; scanf("%d",&T); while(T--) { int Luckmonth=0,Goodmonth=0; scanf("%d %d %d %d",&Startyear,&Startmonth,&Endyear,&Endmonth); int WhichDay=Findwhichday(Startyear,Startmonth);//开始年月分第一天为周几 //cout<<WhichDay<<endl; while(Startyear<Endyear||(Startyear==Endyear&&Startmonth<=Endmonth)) { if(WhichDay==1||WhichDay==0||WhichDay==6) { ++Goodmonth; } WhichDay+=Adddays(Startyear,Startmonth); if((WhichDay-1)%7==5||(WhichDay-1)%7==6||(WhichDay-1)%7==0) { ++Luckmonth; } WhichDay%=7; ++Startmonth; if(Startmonth==13) { Startmonth=1; ++Startyear; } } printf("%d %d\n",Luckmonth,Goodmonth); } return 0; }