题目链接:http://ac.jobdu.com/problem.php?pid=1043
此类日期问题有一个通用解法,就如同计算机里计算时间一样,统一换算成此时距离原点时间的距离,然后根据距离换算成当前时间,思路比较简单。
#include <stdio.h>
#include <string.h>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0
int dayOfMonth[13][2]={
0,0,
31,31,
28,29,
31,31,
30,30,
31,31,
30,30,
31,31,
31,31,
30,30,
31,31,
30,30,
31,31
};
struct Date{
int day;
int month;
int year;
void nextday(){
day++;
if(day>dayOfMonth[month][ISYEAP(year)]){
day=1;
month++;
if(month>12){
month=1;
year++;
}
}
}
};
char weekname[7][10]={
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
int buf[3001][13][32];
char monthname[13][15]={
"",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
int main(){
Date temp;
int cnt=0;
temp.day=1;
temp.month=1;
temp.year=0;
while(temp.year!=3001){
buf[temp.year][temp.month][temp.day]=cnt;
temp.nextday();
cnt++;
}
int d,m,y;
char s[20];
while(scanf("%d%s%d",&d,s,&y)!=EOF){
for(m=1;m<=12;m++){
if(strcmp(s,monthname[m])==0){
break;
}
}
int days=buf[y][m][d]-buf[2014][2][26];
int num=(days%7+7+3)%7;
puts(weekname[num]);
}
return 0;
}