这是本人写的一篇关于计算星期几的C语言程序,但在计算(1945 8 15 星期三)(2001 7 12 星期四)等多个日期时出问题了
#include<stdio.h>
int main()
{
int year,month,day,ty,m,y,c,d,weeks;
scanf(“%d%d%d”,&year,&month,&day);
y=year;
m=month;
if(m<3)
{
m=12+m;
ty–;
}
y=ty%100;
c=ty/100;
d=day;
weeks=(y+y/4+c/4-2*c+(26*(m+1))/10+d-1)%7;
printf(“%d\n”,weeks);
}
#include<stdio.h>
int main()
{
int year,month,day,ty,m,y,c,d,weeks;
scanf(“%d%d%d”,&year,&month,&day);
y=year;
m=month;
if(m<3)
{
m=12+m;
ty–;
}
y=ty%100;
c=ty/100;
d=day;
weeks=(y+y/4+c/4-2*c+(26*(m+1))/10+d-1)%7;
printf(“%d\n”,weeks);
}
解决方案
80
y=year;改成ty=year;试试
#include<stdio.h> int main() { int year,month,day,ty,m,y,c,d,weeks; scanf("%d%d%d",&year,&month,&day); ty=year; m=month; if(m<3) { m=12+m; ty--; } y=ty%100; c=ty/100; d=day; weeks=(y+y/4+c/4-2*c+(26*(m+1))/10+d-1)%7; printf("%d\n",weeks); }