其实第三道题还是蛮简单的,思考一下就可知,这道题用循环就可以解了,只是需要注意闰月的情况。代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool judge(int y){
return ((y%100 !=0)&&(y%4 == 0))||(y%400==0);
}
int main (){
int n=0,year=0,month=0,day=5;
int ans[7];
memset(ans,0,sizeof(ans));
int i=0,j=0,t=0,k=0;
cin>>n;
ans[day]++;
for (i=0;i<n;i++){
year=1900+i;
for (j=1;j<=12;j++){
if ((i==0)&&(j==1)){ //1900.1.13
continue;
}
else {
k = (j!=1) ? j-1 : 12;
if ((k==4)||(k==6)||(k==9)||(k==11)){
t=30%7;
}
else if (k==2){
t = judge(year) ? 29%7 : 28%7;
}
else {
t=31%7;
}
day=(day+t)%7;
ans[day]++;
}
}
}
cout<<ans[5]<<" "<<ans[6]<<" "<<ans[0]<<" "<<ans[1]<<" "<<ans[2]<<" "<<ans[3]<<" "<<ans[4]<<" "<<endl;
}