题目描述:
给你一个日期,请你设计一个算法来判断它是对应一周中的哪一天。
输入为三个整数:day
、month
和 year
,分别表示日、月、年。
您返回的结果必须是这几个值中的一个 {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
。
示例
示例 1:
输入:day = 31, month = 8, year = 2019 输出:"Saturday"
示例 2:
输入:day = 18, month = 7, year = 1999 输出:"Sunday"
示例 3:
输入:day = 15, month = 8, year = 1993 输出:"Sunday"
代码
class Solution(object):
def dayOfTheWeek(self, day, month, year):
days=4+day
"""1971年1月1日是星期五"""
for i in range(1971,year):
if i % 400 == 0 or (i % 100 != 0 and i % 4 == 0):
days=days+366
else:
days=days+365
days = days % 7
d=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
m1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
m2 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if year%400==0 or (year%100!=0 and year%4==0):
for i in range(month-1):
days=m2[i]+days
else:
for i in range(month-1):
days=m1[i]+days
print(d[days%7])
return d[days%7]