简单题……
#include<iostream> #include<fstream> using namespace std; int main() { int n, num[8]={0}, days; int mon[13]={0, 31,28,31,30,31,30,31,31,30,31,30,31 }; ifstream fin("friday.in"); ofstream fout("friday.out"); //cin>>n; fin>>n; days=12; n+=1900; int i, j; for(i=1900; i<n; i++) { for(j=1; j<=12; j++) { num[days%7]++; days+=mon[j]; if( j==2 && (i%100!=0&&i%4==0 || i%400==0))//注意,不应该为j==3 days+=1; } } fout<<num[5]<<" "<<num[6]; for(i=0; i<5; i++) fout<<" "<<num[i]; fout<<endl; }
而且:
另解:
//这个模板本来是求某年某月某天为星期几的;注释部分判断1752年9月3日前的 #include<iostream> #include<fstream> using namespace std; int main() { int n, i, j, a, day, num[8]={0}, month, year; ifstream fin("friday.in"); ofstream fout("friday.out"); fin>>n; n+=1900; day=13; for(i=1900; i<n; i++) { for(j=1; j<=12; j++) { year=i; month=j; //1,2月当做前一年的13,14月 if( month==1 || month==2) { year--; month+=12; } //判断是否在1752年9月3日之前 // if( year<1752 || (year==1752 && month<9) || (year==1752 && month==9 && day<3) ) // a=(day+2*month+3*(month+1)/5+year+year/4+5)%7; // else a=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7; num[a]++; } } fout<<num[5]<<" "<<num[6]; for(i=0; i<5; i++) fout<<" "<<num[i]; fout<<endl; }