您的位置:首页 > 其它

【C】Day Of Week

2018-02-08 15:35 155 查看

题目描述

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

输入描述:

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

输出描述:

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
示例1

输入

9 October 2001
14 October 2001

输出

Tuesday
Sunday
#include<stdio.h>
#include<string.h>
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};
int YEAR(int a){
if(a%4==0&&a%100!=0||a%400==0) return 1;
else return 0;
}
int abs(int a){
return a>0?a:-a;
}
struct DATE{
int day;
int month;
int year;
void nextday(){
day++;
if(day>DayOfMonth[month][YEAR(year)]){
day=1;
month++;
if(month>12){
month=1;
year++;
}
}
}
void yesterday(){
day--;
if(day<1){
month--;
if(month<1){
month=12;
year--;
day=31;
}
else day=DayOfMonth[month][YEAR(year)];
}
}
};
int buf[2001][13][32];
int main(){
int week=4;
DATE tmp;
tmp.year=2018;tmp.month=2;tmp.day=8;week=4;
while(tmp.year>=1000){
buf[tmp.year-1000][tmp.month][tmp.day]=abs(week);
week=(week-1)%7;
tmp.yesterday();
if(week==0) week=7;
}
tmp.year=2018;tmp.month=2;tmp.day=8;week=4;
while(tmp.year<=3000){
buf[tmp.year-1000][tmp.month][tmp.day]=week;
week=(week+1)%7;
tmp.nextday();
}
int y,m,d;
char mon[10];
while(scanf("%d%s%d",&d,mon,&y)!=EOF){
if(strcmp(mon,"January")==0) m=1;
else if(strcmp(mon,"February")==0) m=2;
else if(strcmp(mon,"March")==0) m=3;
else if(strcmp(mon,"April")==0) m=4;
else if(strcmp(mon,"May")==0) m=5;
else if(strcmp(mon,"June")==0) m=6;
else if(strcmp(mon,"July")==0) m=7;
else if(strcmp(mon,"August")==0) m=8;
else if(strcmp(mon,"September")==0) m=9;
else if(strcmp(mon,"October")==0) m=10
4000
;
else if(strcmp(mon,"November")==0) m=11;
else if(strcmp(mon,"December")==0) m=12;
else m=-1;
//printf("%d年%d月%d日\n",y,m,d);
//printf("%d",buf[y-1000][m][d]);
switch(buf[y-1000][m][d]){
case 1:printf("Monday\n");break;
case 2:printf("Tuesday\n");break;
case 3:printf("Wednesday\n");break;
case 4:printf("Thursday\n");break;
case 5:printf("Friday\n");break;
case 6:printf("Saturday\n");break;
case 7:printf("Sunday\n");break;
case 0:printf("Sunday\n");break;
}
}
return 0;
}

注:也可以运用上一篇的方法,计算出要求日期与已知日期(如8 February 2018)之间的天数差,运用天数差%7得出要求日期是星期几。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: