#include <stdio.h>
#include <math.h>
/*
用到了公式:s = floor( (y-1) + (y-1)/4 - (y-1)/100 + (y-1)/400 + c )
最后 s%7 即为星期
c 为从元旦到这天的天数,y为年份
该公式可以计算1582年以后的日期
*/
int daysofmonth(int m);
int compute(int y, int m, int d);
int year, month, day;
int main()
{
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
printf("请输入几号:");
scanf("%d", &day);
int weekday = compute(year, month, day);
printf("今天是星期%d\n", weekday);
return 0;
}
int daysofmonth(int m)
{
switch (m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;
case 4:
case 6:
case 9:
case 11:return 30;
case 2:if (((year%4==0 && year%100!=0)||year%400==0))
return 29;
else
return 28;
default: return 0;
}
}
int compute(int y, int m, int d)
{
double s, days;
days = 0;
for(int i=1; i<m; i++)
days += daysofmonth(m);
days += d;
s = floor((y-1) + (y-1)/4 - (y-1)/100 + (y-1)/400 + days);
return (int)s%7;
}
11-06