-
题目描述:
-
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.
-
样例输入:
-
9 October 2001 14 October 2001
-
样例输出:
-
Tuesday Sunday
-
提示:
-
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
代码:
#include <stdio.h>
#include <string.h>
#define ISLEAP(x) (x%4==0&&x%100!=0)||(x%400==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 year;
int month;
int day;
void nextDay() {
day++;
if(day>dayOfMonth[month][ISLEAP(year)]) {
month++;
day = 1;
if(month>12) {
month = 1;
year++;
}
}
}
};
int buf[3001][13][32];
char monthName[13][20] = {
" ",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
char weekName[7][20] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Tursday",
"Friday",
"Saturday"
};
int main() {
Date tmp;
tmp.year=1000;
tmp.month = 1;
tmp.day = 1;
int count=0;
while(tmp.year<3001) {
buf[tmp.year][tmp.month][tmp.day] = count;
tmp.nextDay();
count++;
}
int y,d;
char m[20];
int i,inter;
while(scanf("%d %s %4d",&d,m,&y)!=EOF) {
//2013/7/14 is Sunday
for(i=1;i<13;i++) {
if(strcmp(m,monthName[i])==0)
break;
}
inter = buf[y][i][d]-buf[2013][7][14];
inter = (inter % 7 + 7) % 7;
puts(weekName[inter]);
}
return 0;
}
1.存储月份名与周几的名字。char month[13][20] , month[1]为"January"。
2.寻找月份字符串所对应在数组中的位置。使用strcmp函数。strcmp(m,monthName[i])。不能用if(m==monthName[i])!
strcmp的用法:
int strcmp ( const char * str1, const char * str2 );
EXAMPLE:
#include <stdio.h>
#include <string.h>
int main ()
{
char szKey[] = "apple\n";
char szInput[80];
do {
printf ("Guess my favorite fruit? ");
fgets (szInput,80,stdin);
} while (strcmp (szKey,szInput) != 0);
puts ("Correct answer!");
return 0;
}
3.日期以7为循环周期,故对7取模,又因为有可能得出负数,故再加7,最后再取模。(inter%7+7)%7