import datetime count = 0 for y in range(1901,2001): for m in range(1,13): if datetime.datetime(y,m,1).weekday() == 6: count += 1 print count
datetime此功能很好用。省去了计算的麻烦。
count = 0 days = 1 year = 365 normal = [31,28,31,30,31,30,31,31,30,31,30,31] leap = [31,29,31,30,31,30,31,31,30,31,30,31] for i in range(1900,2001): if i % 4 ==0 and i != 1900: year = 366 temp = leap else: year = 365 temp = normal for j in range(12): if days%7 == 0: count += 1 days += temp[j] else: days += temp[j] print(count)
project euler 19: Counting Sundays
原文:http://www.cnblogs.com/zhourong1104/p/5459532.html