已知,1900年1月1日是星期1,用户随机输入年月日,计算星期几
代码如下(未改进),欢迎批评指正
year=int(input(‘year‘)) month=int(input(‘month‘)) day=int(input(‘day‘)) if year >=1900:#大于等于1900 ryear =1900 sum =0 while ryear < year:#1901 if ((ryear % 4 == 0) and (ryear %100 !=0))or(ryear % 400 == 0):#云年 sum+=2 else: sum+=1 ryear+=1 #计算月份移动 mon = month r_month = 1 while r_month < month:#计算到mon月份1号 if r_month in [1,3,5,7,8,10,12]: sum +=3 sum = sum % 7 elif r_month in [4,6,9,11]: sum +=2 else: if (year % 4 == 0 and year %100 != 0) or (ryear % 400 == 0): sum +=1 else: sum +=0 r_month+=1 #计算天 sumday = day -1 sum+=sumday sum = sum % 7 #根据移动进行判断 if sum ==1: print(‘星期2‘) elif sum ==2: print(‘星期3‘) elif sum ==3: print(‘星期4‘) elif sum ==4: print(‘星期5‘) elif sum ==5: print(‘星期6‘) elif sum ==6: print(‘星期7‘) else: print(‘星期1‘) else:#计算小于1990年 ye = year year +=1 sum = 0 while year<1900: if (year % 4 == 0 and year %100 !=0) or(year % 400 == 0):#闰年 sum+=2 else: sum +=1 year+=1 #计算月份移动 mo=month month =month+1 while month <=12:#计算到mon月份1号 if month in [1,3,5,7,8,10,12]: sum +=3 elif month in [4,6,9,11]: sum +=2 else: if (year % 4 == 0 and year %100!=0) or(year % 400 == 0): sum +=1 else: sum +=0 month+=1 #计算天 if mo in [1,3,5,7,8,10,12]: sum += 31-day+1 elif mo in [4,6,9,11]: sum += 30-day+1 else: if (year % 4 == 0 and year %100 !=0) or (year % 400 == 0): sum += 29-day+1 else: sum += 28-day+1 sum = sum % 7 #根据移动进行判断 if sum ==1: print(‘星期7‘) elif sum ==2: print(‘星期6‘) elif sum ==3: print(‘星期5‘) elif sum ==4: print(‘星期4‘) elif sum ==5: print(‘星期3‘) elif sum ==6: print(‘星期2‘) else: print(‘星期1‘) print(‘game over‘)
星期几
原文地址:https://www.cnblogs.com/rain-chenwei/p/rain_fall_in_love_with_python.html
时间: 2024-11-10 08:24:41