给出某年某月,某日,求出这天是星期几,不要用复杂的数学方法,一个c语言代码
时间: 2023-04-07 10:05:37 浏览: 97
#include <stdio.h>
int main() {
int year, month, day;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
printf("请输入日期:");
scanf("%d", &day);
int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; //每个月的天数
int sum = 0;
for(int i = 1; i < month; i++) {
sum += days[i];
}
sum += day;
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { //判断是否是闰年
if(month > 2) {
sum++;
}
}
int weekday = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + sum; //计算星期几
weekday %= 7;
char* weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; //星期几的字符串数组
printf("%d年%d月%d日是%s\n", year, month, day, weekdays[weekday]);
return 0;
}
阅读全文