2个回答
展开全部
看代码:
#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; // 因为有 return,所以不需要 break
case 4:
case 6:
case 9:
case 11:
return 30;
default: // 2 月
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 argc, char** argv)
{
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;
}
运行:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询