#include <stdio.h>
int is_leap_year(int year)
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
int get_days (int year, int month)
{
switch (month)
{
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;
default:
if (is_leap_year(year))
return 29;
else
return 28;
}
}
void print_week(int weekday)
{
switch (weekday)
{
case 0:
printf ("星期天\n");
break;
case 1:
printf ("星期一\n");
break;
case 2:
printf ("星期二\n");
break;
case 3:
printf ("星期三\n");
break;
case 4:
printf ("星期四\n");
break;
case 5:
printf ("星期五\n");
break;
case 6:
printf ("星期六\n");
break;
}
}
int main()
{
int year, month, day, i;
printf ("输入年月日,并用空分开:");
scanf ("%d %d %d", &year, &month, &day);
int sum = 0;
for (i = 1980; i < year; i++)
{
if (is_leap_year(i))
{
sum += 366;
}
else
{
sum += 365;
}
}
for (i = 1; i < month; i++)
{
sum += get_days(year, i);
}
sum += day + 1;
printf ("%d 年 %d 月 %d 日是:", year, month, day);
print_week (sum % 7);
return 0;
}
转自https://zhidao.baidu.com/question/1542207962877861787.html?qbl=relate_question_1&word=%D2%D1%D6%AA2018.1.1%CA%C7%D0%C7%C6%DA3%2C%CA%E4%C8%EB%D2%BB%B8%F6%C8%D5%C6%DA%2C%C5%D0%B6%CF%CA%C7%D0%C7%C6%DA%BC%B8%3F