|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 天圆突破 于 2018-1-10 23:29 编辑
原题不贴了,我没有用小甲鱼的标准答案,而是用了笨方法,一个个的式,也能得到答案,但是想请教的是这里:
代码1:
- print('输入指定范围:')
- temp = input()
- while not temp.isdigit():
- temp = input('重新输入:')
- a = int(temp)
- i = 0
- while i <= a:
- if i%2 == 1:
- if i%3 == 2:
- if i%5 == 4:
- if i%6 == 5:
- if i%7 == 0:
- print('%d '%i,end = '')
- i += 1
复制代码
代码2:
- print('输入指定范围:')
- temp = input()
- while not temp.isdigit():
- temp = input('重新输入:')
- a = int(temp)
- i = 0
- while i <= a:
- i += 1
- if i%2 == 1:
- if i%3 == 2:
- if i%5 == 4:
- if i%6 == 5:
- if i%7 == 0:
- print('%d '%i,end = '')
- else:
- continue
- else:
- continue
- else:
- continue
- else:
- continue
- else:
- continue
复制代码
我想请教的是,代码1和代码2是不是在效率上等效?或者说代码1的if没有写else的时候,是默认带了continue的效果,还是会继续遍历?
还是说代码2的效率会优于代码1?
顺便求一个计时的函数,刚学到006还没学到后面,想计算时间一下都不会 =.=
哦,对了再补充一个问题,如果倒着写:
代码3:
- print('输入指定范围:')
- temp = input()
- while not temp.isdigit():
- temp = input('重新输入:')
- a = int(temp)
- i = 0
- while i <= a:
- if i%7 == 0:
- if i%6 == 5:
- if i%5 == 4:
- if i%3 == 2:
- if i%2 == 1:
- print('%d '%i,end = '')
- i += 1
复制代码
这样执行效率会不会变高?
请指教。
|
|