c语言,已知1年1月1日是星期一,计算输入的年月日是星期几

2024-11-26 08:23:54
推荐回答(3个)
回答1:

for(i=1;i<=year;i++)//这个for循环貌似没用 删掉!
if(i%400==0||(year%4==0&&year%100!=0))//这里错了 应该是year%400==0 不是i
sum=sum+1;
没用的变量给你删了 i xingqi
、、、、、、、、、、、
#include
void main()
{
int year,month,day,sum;
printf("请输入年 月 日:");
scanf("%d %d %d",&year,&month,&day);
switch(month)
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
}
sum=sum+day+(year-1)*365;
if(year%400==0||(year%4==0&&year%100!=0))
sum=sum+1;
if(sum%7==0)
printf("这一天是星期天\n");
else printf("这一天是星期%d\n",sum%7);
}

回答2:

#include
#include
int main()
{

int i = 0,year, month, day;
int j, sum1=1, sum2=1,sum = 0;
int flag = 1; int flag1 = 1;
printf("please input year,month,day:");
do
{
scanf_s("%d,%d,%d", &year, &month, &day);
} while (month < 1 || month>12);
int days[][12] = { {31,28,31,30,31,30,31,31,30,31,30,31}
,{31,29,31,30,31,30,31,31,30,31,30,31} };

for (j = 1; j < year; j++)
{
if (j % 4 == 0 && j % 100 != 0 || j % 400 == 0)
{
sum1 = flag * 366;
flag++;
}

else
{

sum2 = 365 * flag1;
flag1++;
}

}

if (month == 1)
;
//printf("该日期是今年的%d天\n", day);
else if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
for (i = 0; i < month - 1; i++)
{

sum = sum + days[1][i];
}

day = sum + day;

printf("%d\n", day);

}

else
{
for (i = 0; i < month - 1; i++)
{

sum = sum + days[0][i];
}

day = sum + day;

printf("%d\n", day);

}

sum = sum1 + sum2 + day;
sum = sum % 7;
switch (sum)
{
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;

default:printf("error occurred\n");

}

}

回答3:

1