题目描述:
Is Friday the 13th really an unusual event?
That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is positive and will not exceed 400.
Note that the start year is NINETEEN HUNDRED, not NINETEEN NINETY.
There are few facts you need to know before you can solve this problem:
- January 1, 1900 was on a Monday.
- Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
- Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
- The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
Do not use any built-in date functions in your computer language.
Don't just precompute the answers, either, please.
输入输出:
INPUT FORMAT
One line with the integer N.
SAMPLE INPUT (file friday.in)
20
OUTPUT FORMAT
Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.
SAMPLE OUTPUT (file friday.out)
36 33 34 33 35 35 34
题目大意:
13号又是一个星期5。13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数。给出N年的一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400. 这里有一些你要知道的: 1900年1月1日是星期一.4,6,11和9月有30天.其他月份除了2月都有31天.闰年2月有29天,平年2月有28天.年份可以被 4整除的为闰年(1992=4*498 所以 1992年是闰年,但是1990年不是闰年)以上规则不适合于世纪年.可以被400整除的世纪年为闰年,否则为平年.所以,1700,1800,1900 和2100年是平年,而2000年是闰年.
解题思路:
由于题目已经告诉了1900年1月1日是星期一,那么我们只需要用天数来取余就可以得到相应是星期几,相当于是跑了暴力,我的代码写得不是很好,还有可以优化的地方,还是贴出
"""
ID: scwswx2
LANG: PYTHON3
TASK: friday
"""
def isleapyear(year):
if (year%4)==0 and (year%100)!=0:
return True
elif (year%400)==0:
return True
else:
return False
fin = open ('friday.in', 'r')
fout = open ('friday.out', 'w')
leapyear=[0,31,29,31,30,31,30,31,31,30,31,30,31]
year=[0,31,28,31,30,31,30,31,31,30,31,30,31]
week={6:0,0:0,1:0,2:0,3:0,4:0,5:0}
n=int(fin.readline().strip('\n'))
leapday=0#间隔的天数
for i in range(n):
summonth = 0 # 间隔月份天数
curryear=1900+i;
if isleapyear(curryear):
for j in range(len(leapyear)-1):
summonth=summonth+leapyear[j]
sumday=leapday + 13 + summonth
week[sumday%7]= week[sumday%7]+1
leapday=leapday+sum(leapyear)
else:
for j in range(len(year)-1):
summonth = summonth + year[j]
sumday=leapday+13+summonth
week[sumday%7]= week[sumday%7]+1
leapday=leapday+sum(year)
print(week)
for i in week:
if i!=5:
fout.write(str(week[i])+' ')
else:
fout.write(str(week[i])+'\n')
fout.close()