详细分析见 http://www.cnblogs.com/lyy289065406/archive/2011/07/31/2122851.html
#include <stdio.h> #include <iostream> using namespace std; int flagRun=0; int sum; int Month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //平年月 int Lmonth[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; //闰年月 enum week{Sun,Mon,Tue,Wed,Thu,Fri,Sat}; //星期 int leap(int year){ if(year<1582){ if(year%4==0) return 1; else return 0; }else if(((year%4==0)&&(year%100!=0))||(year%400==0)||(year==1700)) return 1; else return 0; } int computeDay(int year,int month){ int day=1; int i; for(i=1;i<year;i++){ if(leap(i)) day+=366; else day+=365; } for(int j=1;j<month;j++) if(leap(i)) day+=Lmonth[j]; else day+=Month[j]; if(year>1752) day-=11; else if(year==1752 && month>9) day-=11; return day; } int main(){ int test; cin>>test; while(test--){ int ys,ms,ye,me; int *pm; int i,j; cin>>ys>>ms>>ye>>me; int luck=0,good=0; int day= computeDay(ys,ms); if(((day+5)%7<=Mon) || ((day+5)%7==Sat)) //计算起始ys年ms月1号为星期几,判断是否为good month good++; //1年1月1号是星期六,而原本默认是星期日,因此+5调整 for(i=ys;i<ye;i++){ if(leap(i)) pm=Lmonth; else pm=Month; if(i==ys) j=ms; else j=1; for(;j<13;j++){ day+=*(pm+j); if(i==1752&&j==9) day-=11; if(((day+5)%7<=Mon)||((day+5)%7==Sat)){ good++; luck++;//当j+1月good month,第j月必为luck month } } } /* 计算第ye年的good,luck month */ if(leap(i)) pm=Lmonth; else pm=Month; if(i==ys)//即ys=ye j=ms; else j=1; for(;j<=me;j++){ day+=*(pm+j); if(i==1752&&j==9) day-=11; if(((day+5)%7<=Mon)||((day+5)%7==Sat)){ if(j!=me) good++; luck++;//当j+1月good month,第j月必为luck month } } cout<<luck<<' '<<good<<endl; } system("PAUSE"); return 0; }