c语言编程:计算生日是星期几

编写一个程序,输入年月日,就能回答那天是星期几。
2024-12-31 22:47:25
推荐回答(2个)
回答1:

#include
int main()
{
int day,mn,yr,i,days=0,s,k;
int mont[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char wek[][9]={ {'S','u','n','d','a','y'},
{'M','o','n','d','a','y'},
{'T','u','e','s','d','a','y'},
{'W','e','d','n','s','d','a','y'},
{'T','h','u','r','s','d','a','y'},
{'F','r','i','d','a','y'},
{'S','a','t','u','r','d','a','y'}
};
printf("Inpute the date (year-month-day):");
scanf("%d-%d-%d",&yr,&mn,&day);
if (yr%4==0||yr%100==0||yr%400==0)
mont[2]=29;
else
mont[2]=28;
for (i=0;idays+=mont[i];
days+=day;
s=yr-1+(int)((yr-1)/4)-(int)((yr-1)/100)+(int)((yr-1)/400)+days;
k=s%7;
printf("%d-%d-%d is %s.",yr,mn,day,wek[k]);
return 0;
}

回答2:

#include

int
main()
{
int
day,mn,yr,i,days=0,s,k;
int
mont[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char
wek[][9]={
{'S','u','n','d','a','y'},
{'M','o','n','d','a','y'},
{'T','u','e','s','d','a','y'},
{'W','e','d','n','s','d','a','y'},
{'T','h','u','r','s','d','a','y'},
{'F','r','i','d','a','y'},
{'S','a','t','u','r','d','a','y'}
};
printf("Inpute
the
date
(year-month-day):");
scanf("%d-%d-%d",&yr,&mn,&day);
if
(yr%4==0||yr%100==0||yr%400==0)
mont[2]=29;
else
mont[2]=28;
for
(i=0;idays+=mont[i];
days+=day;
s=yr-1+(int)((yr-1)/4)-(int)((yr-1)/100)+(int)((yr-1)/400)+days;
k=s%7;
printf("%d-%d-%d
is
%s.",yr,mn,day,wek[k]);
return
0;
}