题目描述
python题解
直接调用python的datetime库,这也太好用了
import datetime
while True:
try:
month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October','November', 'December']
d, m, y = input().split()
print((datetime.datetime(int(y), month.index(m) + 1, int(d)).strftime("%A")))
except:
break
C++题解
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int monthDays[2][12] = {
{31,28,31,30,31,30,31,31,30,31,30,31}, // not leap year
{31,29,31,30,31,30,31,31,30,31,30,31} // leap year
};
bool isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
return false;
}
int main() {
int day=0,year=0,mon=0;
int n = 0,week_day=0;
string month;
vector<string>M({ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" });
vector<string> W({ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" });
while (cin >> day >> month >> year)
{
for (int i = 0; i < M.size(); ++i)
if (M[i] == month)
mon = i;
n = 0;
//从0年开始算
for (int i = 0; i < year; ++i)
n += isLeapYear(i) ? 366 : 365;
for (int i = 0; i < mon; ++i)
n += monthDays[isLeapYear(year)][i];
n += day;
n = n + 5;
week_day = n % 7;
cout << W[week_day] << endl;
}
return 0;
}