十三号星期五
数天天,简单又暴力
口诀:扫日子,跟星期,查月数,追年份
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//四六九冬(立冬11月)三十天,平年二月永不差
int weekday[7];//0是周一
int get(int year,int month)//判断这个月,有多少天
{
if(month != 2) return months[month];
if(year % 100 && year % 4 == 0 || year % 400 == 0)
return 29;
return 28;
}
int main()
{
int n;
scanf("%d",&n);
int week = 0;
int year = 1900 , month = 1, day = 1;
while(year < 1900 + n)
{
if(day == 13) weekday[week] ++;
week = (week + 1) % 7;
day ++;
if(get(year , month) < day) month ++ , day = 1;
if(month > 12) month = 1,year ++;
}
for(int i = 5,j = 0;j < 7;i = (i+1)%7,j ++)
cout<<weekday[i]<<' ';
cout<<endl;
}
模拟每一年,枚举每一月第一天
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//四六九冬(立冬11月)三十天,平年二月永不差
int weekday[7];//0是周一,下面用到取模偏移,逢7换0即为周一,所以往前偏移
int main()
{
int n;
scanf("%d",&n);
int days = 0;
for(int year = 1900;year < 1900 + n;year ++)
{
for(int i = 1;i <= 12;i ++)
{
weekday[(days + 12) % 7] ++;
days += month[i];//累加此月
if(i == 2)
{
if(year % 100 && year % 4 == 0 || year % 400 == 0)//符合就润一年
days ++;
}
}
}
for(int i = 5,j = 0;j < 7;i = (i+1)%7,j ++)
cout<<weekday[i]<<' ';
cout<<endl;
}