2015年4月29日 dayofweek

#include <stdio.h>
#include <stdlib.h>
int DayofYear(int year, int month, int day);
#define ISYEAR(year) (year%400==0) ||(year%4==0&&year%100!=0)

int main(int argc, char *argv[])
{
/*已知1900年1月1日是星期一,输入一个年月日计算出来是星期几*/
int year, month, day;
char *week[]={"monday","tuesday","wedesday","thusday","friday","satday","sunday"};
int index, date1, date = 0;
scanf("%d%d%d",&year, &month, &day);
date1 = DayofYear(year, month, day);
if(year<1900){
printf("error\n");
return 0;
}
else
{
for(index=1900;index<year;index++){
if(ISYEAR(index))
{
date +=366;
}
else
date +=365;
}
}
printf("%s\n",week[(date+date1)%7]);

system("pause");
return 0;

}

 

int DayofYear(int year, int month, int day)
{
int sum = 0;
int index = 0;
for(index=1;index<month;index++){
switch(index){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
sum += 31;
break;
case 2:
if(ISYEAR(year)){
sum += 29;
}
else 
sum +=28;
break;
case 4:
case 6:
case 9:
case 11:
sum += 30;
break;

}
}
sum += day;
return sum;
}

 

用已知日期的星期求输入日期的星期,小技巧,将已知日期(1900年1月1日)星期一保存在week数组的第0号元素,直接用相差的天数取模就可以得到结果。

posted on 2015-04-29 20:38  IT小不点  阅读(134)  评论(0编辑  收藏  举报