模拟
思路:从1年1月1日开始遍历,依次判断是否到了下一月、下一年,统计从1年1月1日到询问日期所过天数。
months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month_name = {"January": 1, "February": 2, "March": 3, "April": 4,
"May": 5, "June": 6, "July": 7, "August": 8,
"September": 9, "October": 10, "November": 11, "December": 12}
weeks = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
def is_leap(year):
return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0 # 是否是闰年
def get_days(year, month):
s = months[month]
if month == 2:
return s + is_leap(year)
return s
while True:
try:
d, m, y = input().split()
d = int(d)
m = str(m)
y = int(y)
month = month_name[m]
days = 0
i, j, k = 1, 1, 1 # year, month, day
while i < y or j < month or k < d:
k += 1
days += 1
if k > get_days(i, j): # 到了下个月
k = 1
j += 1
if j > 12: # 跨年
j = 1
i += 1
print(weeks[days % 7])
except EOFError:
break
公式
思路:
基姆拉尔森计算公式(Kim larsen calculation formula)
W= (d+2m+3(m+1)/5+y+y/4-y/100+y/400+1)%7
w表示星期,w的取值范围是0~6,0代表星期日,1~6星期一到星期六
把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
参考链接:https://baike.baidu.com/item/%E5%9F%BA%E5%A7%86%E6%8B%89%E5%B0%94%E6%A3%AE%E8%AE%A1%E7%AE%97%E5%85%AC%E5%BC%8F
months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month_name = {"January": 1, "February": 2, "March": 3, "April": 4,
"May": 5, "June": 6, "July": 7, "August": 8,
"September": 9, "October": 10, "November": 11, "December": 12}
weeks = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
while True:
try:
d, m, y = input().split()
d = int(d)
m = str(m)
y = int(y)
month = month_name[m]
if month == 1 or month == 2:
month += 12
y -= 1
w = (d + 2 * month + 3 * (month + 1) // 5 + y + y // 4 - y // 100 + y // 400 + 1) % 7
print(weeks[w])
except EOFError:
break