我先规避了输入1月1日是星期几的算式。
用户正常输入星期几。但是我不算。而是用正常泽勒公式运算,其中日期就是1号,直接写1
u_in_year, u_in_day = 2022, 6
for m in range(3, 13):
k = u_in_year % 100
j = u_in_year // 100
h = (1 + ((26 * (m + 1)) // 10) + k + (k // 4) + (j // 4) + (5 * j)) % 7
if h == 0:
day_text = "Saturday"
elif h == 1:
day_text = "Sunday"
elif h == 2:
day_text = "Monday"
elif h == 3:
day_text = "Tuesday"
elif h == 4:
day_text = "Wednesday"
elif h == 5:
day_text = "Thursday"
elif h == 6:
day_text = "Thursday"
print(f"The %d first {u_in_year} is {day_text}" % (m))
结果如下
少了1月到2月。因为如果加1月或2月。我其他月份就全都算错。测试了很多半法所以就先不含1月和2月
然后今天四月份的星期明显不对应该是周五。这个是怎么回事呢???后来发现是把星期四多复制了一下
该题我之前是用4天才做出了。现在是半天已经很快了。但是如何解决1月和2月份的问题呢??
终于成功了。兄弟们
u_in_year, u_in_day = 2022, 6
for m in range(1, 13):
k = u_in_year % 100
j = u_in_year // 100
if m == 1 or m == 2:
m += 12
a = u_in_year - 1
k = a % 100
j = a // 100
h = (1 + ((26 * (m + 1)) // 10) + k + (k // 4) + (j // 4) + (5 * j)) % 7
if h == 0:
day_text = "Saturday星期六"
elif h == 1:
day_text = "Sunday星期日"
elif h == 2:
day_text = "Monday星期一"
elif h == 3:
day_text = "Tuesday星期二"
elif h == 4:
day_text = "Wednesday星期三"
elif h == 5:
day_text = "Thursday星期四"
elif h == 6:
day_text = "Friday星期五"
print(f"The %2d first %4d is %10s" % (m, u_in_year,day_text))
而且还有排版,美中不足的就是1月和2月。我可以处理但是没有做。而且觉得做的不会太完美,
终于成功了。我找到了两全其美的好办法了
u_in_year, u_in_day = 2022, 6
for m in range(1, 13):
k = u_in_year % 100
j = u_in_year // 100
if m == 1 or m == 2:
m += 12
a = u_in_year - 1
k = a % 100
j = a // 100
c = m - 12
h = (1 + ((26 * (m + 1)) // 10) + k + (k // 4) + (j // 4) + (5 * j)) % 7
if h == 0:
day_text = "Saturday星期六"
elif h == 1:
day_text = "Sunday星期日"
elif h == 2:
day_text = "Monday星期一"
elif h == 3:
day_text = "Tuesday星期二"
elif h == 4:
day_text = "Wednesday星期三"
elif h == 5:
day_text = "Thursday星期四"
elif h == 6:
day_text = "Friday星期五"
if 12 >= m >= 3:
c = m
print(f"The %2d first %4d is %10s" % (c, u_in_year, day_text))