AcWing 1341. 十三号星期五
考察:模拟
思路一:
枚举每一年的每一月,看该月13号离1900年1月1日有多少天,对天数模7统计.
这里比较简单的做法是计算xxxx年x月的1日离1900年1月1日有多少天,再+12天就是13号离它多少天.
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 5 int week[8];//W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7 6 int main() 7 { 8 int n; 9 scanf("%d",&n); 10 int days = 0; 11 for(int year= 1900;year<1900+n;year++) 12 { 13 if(year%4==0&&year%100!=0) month[2] = 29; 14 if(year%400==0) month[2] = 29; 15 for(int m=1;m<=12;m++) 16 { 17 week[(days+12)%7]++; 18 days+=month[m]; 19 } 20 month[2] = 28; 21 } 22 for(int i=5,j=1;j<=7;i=(i+1)%7,j++) printf("%d ",week[i]); 23 printf("\n"); 24 return 0; 25 }
思路二:
听都没听说过的公式法: 基姆拉尔森计算公式
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 5 int week[8];//W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7 6 int get(int y,int m,int d) 7 { 8 if(m==1||m==2) y--,m+=12; 9 return (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7; 10 }//返回的6代表周六. 11 int main() 12 { 13 int n; 14 scanf("%d",&n); 15 int days = 0; 16 for(int year= 1900;year<1900+n;year++) 17 { 18 for(int m=1;m<=12;m++) 19 week[get(year,m,13)]++; 20 } 21 for(int i=6,j=1;j<=7;i=(i+1)%7,j++) printf("%d ",week[i]); 22 printf("\n"); 23 return 0; 24 }