码迷,mamicode.com
首页 > 编程语言 > 详细

欧拉计划(python) problem 19

时间:2015-04-16 10:27:29      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:python

Counting Sundays

Problem 19

You are given the following information, but you may prefer to do some research for yourself.

  • 1 Jan 1900 was a Monday.
  • Thirty days has September,
    April, June and November.
    All the rest have thirty-one,
    Saving February alone,
    Which has twenty-eight, rain or shine.
    And on leap years, twenty-nine.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?


Answer:
171
Completed on Thu, 16 Apr 2015, 02:05

Go to the thread for problem 19 in the forum.

python code:

def IsLeapYear(year):
    if year%4==0:
        if year%100==0:
            if year%400==0:
                return 1
            else:
                return 0
        return 1
    else:
        return 0

def MonthNumList(year):
    List=[31,28,31,30,31,30,31,31,30,31,30,31]
    if IsLeapYear(year)==1:
        List[1]=29
    return List

startDay=1
count=0
for year in range(1900,2001):
    monthNum=MonthNumList(year)
    for month in range(0,12):
        startDay=(startDay+monthNum[month])%7
        if year>1900 and startDay==0:
            count=count+1

print(count)


欧拉计划(python) problem 19

标签:python

原文地址:http://blog.csdn.net/zhangzhengyi03539/article/details/45070393

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!