来自Euler: 1900年1月1日是星期一。在二十世纪(1901年1月1日至2000年12月31日)每月的第一天有多少个星期日?
我的第一个功能是从已知的星期一开始,到2000年12月31日止,每天收集一份清单。第二个函数每七天保存一个星期日列表,从1901年的第一个星期日开始。在
def dayListBuild():
dayListBuild = [[0,0,0]]
for year in range(1900, 2001):
for month in range(1, 13):
if month in [4, 6, 9, 11]:
dayRange = 30
elif not month - 2:
if year % 4 and (not year % 100 or year % 400):
dayRange = 29
else:
dayRange = 28
else:
dayRange = 31
for day in range(1, dayRange + 1):
dayListBuild.append([year, month, day])
return dayListBuild
def sundayList():
dayList = dayListBuild()
sundayList = []
for day in range(len(dayList)):
if not (day - 6) % 7 and dayList[day][0] > 1900 and not dayList[day][2] - 1:
sundayList.append(dayList[day])
return len(sundayList)
print(sundayList())
产量:200。 我的答案在29日前取消。答案是171。 这是我的星期天名单:
^{pr2}$我不知道我做错了什么。在
我只是想分享一下我对这个问题的代码,希望它能帮助到别人,或者更好,但有人可能会批评它。在
我知道有很多打印或者一些人认为不必要的打印正在进行,这有助于我理解控制台,所以如果你要对此发表评论,请省省口气。在
你没有正确地使用模数。尝试:
相关问题 更多 >
编程相关推荐