金狮镖局 Design By www.egabc.com
一、题目要求与分析
根据输入的年和月,打印该月的日历。如图所示:
题目分析:复杂的问题往往很简单,只需要找到拆分点即可,就像这种题可以分为三个函数:
为了方便起见,我们在这里直接把函数的名字给定一下:
"htmlcode">
def day(y, m, d):#计算y年m月d日是星期几 y0 = y - (14 - m)//12 x = y0 + y0//4 - y0//100 + y0 //400 m0 = m + 12*((14 - m)//12) - 2 d0 = (d + x + 31*m0//12) % 7 return d0 #注意,周日是0 def isLeapYear(year): #判断year年是否闰年 isLeapYear = True if year % 4 ==0: #整除可不是等于0,天 if year % 100 ==0: if year % 400 ==0: isLeapYear = True else: isLeapYear = False else: isLeapYear = True else: isLeapYear = False #注意是一个等号还是两个等号 return isLeapYear def calendar(y, m): #打印y年m月日历 print(' {}年{}月'.format(y,m)) print('Su\tM\tTu\tW\tTh\tF\tSa') # 请在下面编写代码 # ********** Begin ********** # month_1 = [1,3,5,7,8,10,12] month_2 = [4,6,9,11] momth_number = 0 if isLeapYear(y): #计算这个月有多少天 if m == 2: month_number = 29 else: if m in month_1: month_number = 31 if m in month_2: month_number = 30 else: if m == 2: month_number = 28 else: if m in month_1: month_number = 31 if m in month_2: month_number = 30 table = day(y, m, 1) #空格 排列输出 for j in range (1,month_number + 1): #注意加一 if j == 1: print("\t" * table,end = '') j = str(j) print(j + "\t",end = '') j = eval(j) if (j + day(y, m, 1)) % 7 == 0: print("\r") print("\r") for (y,m) in [(2020,12), (2017,2), (2016,2)]: calendar(y, m) print('---------------------------')
三、我们来逐个fenxi
(1) day(y, m, d)函数
这个函数没有什么实质性的技术含量,因为这里涉及到一个数学的问题,比如,直接用数学公式,下边我提供一下本函数用的三个数学公式:
假如给定了y,m,d,则上边的d0就是要求的星期几。
注意:星期日代表的数字是0,切记,但别问我咋知道的。
函数就这样出来了:
def day(y, m, d): y0 = y - (14 - m)//12 x = y0 + y0//4 - y0//100 + y0 //400 m0 = m + 12*((14 - m)//12) - 2 d0 = (d + x + 31*m0//12) % 7 return d0
(2) disLeapYear(year)函数
关于判断闰年这件事想必很好知晓,这里我提供一张流程结构图仅供参考:
"htmlcode">
def isLeapYear(year): #判断year年是否闰年 isLeapYear = True if year % 4 ==0: #整除可不是等于0,天 if year % 100 ==0: if year % 400 ==0: isLeapYear = True else: isLeapYear = False else: isLeapYear = True else: isLeapYear = False #注意是一个等号还是两个等号 return isLeapYear
(3) calendar(y, m)函数
这里要解决两个问题,也就是本次程序的核心:
1 . 这个月有多少天?
"htmlcode">
table = day(y, m, 1) #求空格数 for j in range (1,month_number + 1): #注意加一 if j == 1: print("\t" * table,end = '') j = str(j) print(j + "\t",end = '') j = eval(j) if (j + day(y, m, 1)) % 7 == 0: print("\r") print("\r")
注意:
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无Python实例教程之检索输出月份日历表的评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年09月24日
2024年09月24日
- 国风大师纯音系列《古筝仙子 常静演奏 十指弹一》1CD[FLAC][919MB]
- DasKapital-OneMustHaveChaosInsidetoGiveBirthtoaDancingStar(2024)[24-44.1]WAV
- 王菲.1999-《只爱陌生人》日本东芝版[低速原抓WAV+CUE]
- 张小英1987-金奖金曲第一集[新加坡版][WAV+CUE]
- 鹿晗.2015-重启【海蝶】【FLAC分轨】
- 张芸京.2012-小女孩【金牌大风】【WAV+CUE】
- 余天.2002-雄狮魅力金曲2CD(南方金点系列)【南方】【WAV+CUE】
- 银霞.1982-《结·SAYYESMYBOY》台湾复刻版[WAV+CUE]
- 梁汉文1993-火热动感[引进版][WAV+CUE]
- 梁汉文2008-EDMONDHITS48[香港]3CD[WAV+CUE]
- 国风大师纯音系列《童丽-[渭城曲]》1CD[WAV分轨][593.5MB]
- 国风大师纯音系列《许菱子《烧筝2》古筝曲集》1CD[WAV分轨][1.3GB]
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】