注册 登录
编程论坛 Python论坛

求教

srl237381 发布于 2018-12-27 20:13, 1621 次点击
python
2.计算1982 ~2048 年的复活节的计算公式如下:令a= yar%19,b= year%64,c= year%7,d=(19a+ 24%30,e= (2b +4c +6d+5)%7.复活节的日期是3月22日+d+e(可能在4月)。写一个程序,输入年份,验证它在适当的范围,然后打印出那一年 复活节的日期。
2 回复
#2
豆豆的滴2018-12-28 15:10
自己写写看呗
#3
傻眼猫咪2021-08-02 21:52
程序代码:

# 樓主,因為小弟我想練習練習,不想放過任何習題,所以上網爬文一下發現你的問題抄錯了,已更正如下:

#   年份      M   N
#
1583-1699  22   2
#
1700-1799  23   3
#
1800-1899  23   4
#
1900-2099  24   5
#
2100-2199  24   6
#
2200-2299  25   0

# a = Y mod 19
#
b = Y mod 4
#
c = Y mod 7
#
d = (19a + M) mod 30
#
e = (2b + 4c + 6d + N) mod 7

# 若 d+e < 10 則復活節在3月(d+e+22)日,反則在4月(d+e-9)日,除了兩個特殊情況:
#
(一) 若公式算出的日期是4月26日,復活節在4月19日;
#
(二) 若公式算出的日期是4月25日,同時d=28、e=6和a>10,復活節應在4月18日。

# 代碼如下:

while True:
    Y = int(input('年份: '))
    if Y < 1583 or Y > 2299: continue
    else: break

if Y < 1700: M, N = 22, 2
elif Y < 1800: M, N = 23, 3
elif Y < 1900: M, N = 23, 4
elif Y < 2100: M, N = 24, 5
elif Y < 2200: M, N = 24, 6
elif Y < 2300: M, N = 25, 0

a = Y%19
b = Y%4
c = Y%7
d = ((19*a)+M)%30
e = ((2*b)+(4*c)+(6*d)+N)%7

if (d+e) < 10: print(str(Y)+'年的復活節在 3月'+str(d+e+22)+'')
elif (d+e-9) == 26: print(str(Y)+'年的復活節在 4月19日')
elif ((d+e-9) == 25) and (d == 28) and (e == 6) and (a > 10): print(str(Y)+'年的復活節在 4月18日')
else: print(str(Y)+'年的復活節在 4月'+str(d+e-9)+'')


[此贴子已经被作者于2021-8-2 21:54编辑过]

1