星期几的计算问题

/*星期几计算,
使用基姆拉尔森计算公式
Week=(Day + 2*Month + 3*(Month+1)/5 + Year + Year/4 - Year/100 + Year/400) % 7
*/
#include<stdio.h>
#include<string.h>
int main(int argc, char const *argv[])
{
int y,m,d;
int r;
int ok;
scanf("%d%d%d",&y,&m,&d);

//判断是否是闰年,4年一润,100年不润,400又润
if((y%4==0&&y%100!=0)||y%400==0){
r=1;
}else{
r=0;
}

//判断输入是否有效
if((m==1||m==3||m==5||m==7||m==8||m==10||m==12)&&(d>=1&&d<=31)){
ok=1;
}else if((r==1 && m==2)&&(d>=1 && d<=29)){
ok=1;
}else if((r==0 && m==2)&&(d>=1 && d<=28)){
ok=1;
}else{
printf("%s\n","Incorrect input data");
}

//判断是星期几
if(ok==1){
//一月、二月要当作上一年的十三、十四月来参与计算
if(m==1||m==2){
y--;
m+=12;
}

int w =(d+1+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;

if(w == 0)
printf("Sunday\n");
if(w == 1)
printf("Monday\n");
if(w == 2)
printf("Tuesday\n");
if(w == 3)
printf("Wednesday\n");
if(w == 4)
printf("Thursday\n");
if(w == 5)
printf("Friday\n");
if(w == 6)
printf("Saturday\n");

}

 

return 0;
}

posted @ 2019-01-16 18:13  韩江雪  阅读(128)  评论(0编辑  收藏