描述
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 2005, 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
题目大意:
编写一个程序,计算给定的日期是周几。
输入:
单行包含日期d、月份M和年份y(1000≤y≤3000)。月名是以大写字母开头的对应英文名。
输出:
输出单行,其中包含与日期对应的星期几的英文名称,从大写字母开始。所有其他字母必须是小写。输入/输出中的月份和星期名称:January, February, March, April, May, June, July, August, September, October, November, December Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
知识点: 暴力求解 模拟 日期问题
解法:可以以当天为基准,向前数天数或向后数天数。
AC代码:
#include <stdio.h>
#include <string.h>
int main() {
int d, m, y;
char str[100];
char monthName[13][50] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
char weekName[7][50] = {"Sunday", "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int monthDay[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
while(scanf("%d %s %d", &d, str, &y) != EOF) {
for(m = 1; m <= 12; m ++)
if(strcmp(str, monthName[m]) == 0)
break;
int begyear, begmonth, begday;
int endyear, endmonth, endday;
int beforeflag;
int totalday = 0;
if(y < 2024 || y == 2024 && m < 3 || y == 2024 && m == 3 && d < 9) {
begyear = y;
begmonth = m;
begday = d;
endyear = 2024;
endmonth = 3;
endday = 9;
beforeflag = 1;
}
else {
begyear = 2024;
begmonth = 3;
begday= 9;
endyear = y;
endmonth = m;
endday = d;
beforeflag = 0;
}
if(begyear % 400 == 0 || begyear % 100 != 0 && begyear % 4 == 0)
monthDay[2] = 29;
else
monthDay[2] = 28;
while(1) {
if(begyear == endyear && begmonth == endmonth && begday == endday)
break;
totalday ++;
begday ++;
if(begday > monthDay[begmonth]) {
begmonth ++;
begday = 1;
if(begmonth > 12) {
begyear ++;
if(begyear % 400 == 0 || begyear % 100 != 0 && begyear % 4 == 0)
monthDay[2] = 29;
else
monthDay[2] = 28;
begmonth = 1;
}
}
}
if(beforeflag)
// (x + totalDay) % 7 = 6 - >
printf("%s\n",weekName[(13 - totalday % 7) % 7]);
else
printf("%s\n",weekName[(6 + totalday) % 7]);
}
return 0;
}