EularProject 19: 星期日的个数

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:

<span style="font-family:Times New Roman;font-size:18px;">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)</span>


你可能感兴趣的:(python)