用户输入年和月,第一行为星期的缩写,每周从周日开始
year = int(input())
month = int(input())
days = 0 #统计天数
count = 0 #用于打印时的计数
k = 0 #用于循环
m=1 #用于计算天数
def year1(n): #判断是否为闰年,
if (n%4==0 and n%100!=0) or n%400==0 :
return True
else:
return False
def print1(month,count): #打印日历的函数
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
for i in range(1,32): #打印31天
print(i,"\t",end='')
count+=1
if count%7==0:print("\n") #每逢7的倍数则换行
elif month == 2:
if year1(year) == True:
for i in range(1, 30):
print(i, "\t", end='')
count += 1
if count % 7 == 0: print("\n")
else:
for i in range(1, 29):
print(i, "\t", end='')
count += 1
if count % 7 == 0: print("\n")
else:
for i in range(1, 31):
print(i, "\t", end='')
count += 1
if count % 7 == 0: print("\n")
if year<2021: #在2021年之前,本题使用2021年的一月一号做基准
for i in range(year,2021):#把输入的年到2021年中的天数求和
if year1(i) == True:
days = days + 366
else:
days = days + 365
while month<=12: #对该月到一月的天数求和
if month==1 or month==3 or month==5 or month==7 or month==8 or month==10 or month==12:
days = days + 31
elif month==2:
if year1(year)==True:
days = days+29
else:
days = days+28
else:
days = days+30
month = month+1
if year1(year)==True: #减去输入那年的天数
days=days-366
else:
days=days-365
print("Sum\tMon\tTue\tWed\tThu\tFri\tSat") #打印
if (days%7)<=5:
for j in range(0,5-(days%7)):
print("\t",end='')
count = count+1
else:
print("\t"*6)
print1(month,count)
if year>=2021:
for i in range(2021,year+1):
if year1(i) == True:
days = days + 366
else:
days = days + 365
while m<month:
if m==1 or m==3 or m==5 or m==7 or m==8 or m==10 or m==12:
days = days + 31
elif m==2:
if year1(year)==True:
days = days+29
else:
days = days+28
else:
days = days+30
m=m+1
if year1(year)==True:
days=days-366
else:
days=days-365
print("Sum\tMon\tTue\tWed\tThu\tFri\tSat")
if (days % 7) <= 1:
for j in range(0, 5 + (days % 7)):
print("\t",end='')
count+=1
else:
for k in range(0,(days%7)-2):
print("\t",end='')
count+=1
print1(month,count)
|