我需要在21世纪的每个复活节星期日打印。我需要确定月份:d
的一天不超过30; 4月(4月);如果更大,那么我需要将其转换为5月的适当日期。例如,d=32
将是m=5, d=2
或5月2日。
import calendar
import datetime
def easter():
for y in range(2001, 2101):
m2 = 2 * (y % 4)
m4 = 4 * (y % 7)
m19 = 19 * (y % 19)
v2 = (16 + m19) % 30
v1 = (6 * v2 + m4 + m2) % 7
p = v1 + v2
d = 3 + p
print ('Easter Sunday for the year', y, 'is',
datetime.date(2015, m, 1).strftime('%B'),
'{}.'.format(int(d)))
easter()
答案 0 :(得分:2)
您只需要进行一次调整:如果一天超过30天,则将月份从4月增加到5月,并将天数减少30:
if d <= 30:
m, d = 4, d
else:
m, d = 5, d-30
print("Easter Sunday for the year", y, "is",
datetime.date(y, m, d).
strftime('%B'), '{}.'.format(int(d)))
部分输出,包括临界情况:
Easter Sunday for the year 2073 is April 30.
Easter Sunday for the year 2074 is April 22.
Easter Sunday for the year 2075 is April 7.
Easter Sunday for the year 2076 is April 26.
Easter Sunday for the year 2077 is April 18.
Easter Sunday for the year 2078 is May 8.
...
Easter Sunday for the year 2088 is April 18.
Easter Sunday for the year 2089 is May 1.