我在解决Project Euler #19问题
在二十世纪(1901年1月1日至2000年12月31日)每月的第一个星期天有多少个?
下面是代码:
months = { "January": 31,
"February" : 28,
"March" : 31,
"April" : 30,
"May" : 31,
"June" : 30,
"July" : 31,
"August" : 31,
"September" : 30,
"October" : 31,
"November" : 30,
"December" : 31}
def countingSundays():
day = 1
sunday_count = 0
for year in xrange(1901,2001):
for m in months:
day += months[m]
if year % 4 == 0 and m == "February":
day += 1
if day % 7 == 0:
sunday_count += 1
print "Sundays:", sunday_count
程序的输出是172,这是不正确的。我搜索的答案是171。所以我想知道为什么我会得到额外的1个星期天?
https://stackoverflow.com/questions/30830522
复制相似问题