由日期推星期(基姆拉尔森计算公式)

题意:给定年月日,问该天是星期几。

思路:运用基姆拉尔森计算公式立即得。

代码:

#include<cstdio>
#include<cstring> 
using namespace std;
int week(int y,int m,int d){
	if(m<3) y--,m+=12;
	return (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7;//注意0代表星期天
}
char  s[10][10]={"天","一","二","三","四","五","六"};
int main(){
	int y,m,d;
	while(~scanf("%d%d%d",&y,&m,&d))
		printf("%d\n",week(y,m,d)),printf("%d年%d月%d日是星期%s.\n",y,m,d,s[week(y,m,d)]);
	return 0;
}