Python_190414_test5
Python_190414_test5
program1
#计算任意个输入数字的乘积 def cmul(*a):#notice the use of changeable value s=1 for i in a: s*=i return s #notice the space signs the layer of the program print(eval("cmul({})".format(input())))
- skills
print(eval("cmul({})".format(input())))
def cmul(*a):
notice the use of changeable value
program2
- 计算斐波那契数列的值,具体功能如下:
-
获取用户输入整数N,其中,N为正整数
-
计算斐波那契数列的值
- 如果将斐波那契数列表示为fbi(N),对于整数N,值如下:
fbi(1)和fbi(2)的值是1,当N>2时,fbi(N) = fbi(N-1) + fbi(N-2)
def fbi(n): answer=1 if n==1 or n==2 else fbi(n-1)+fbi(n-2) return answer n = eval(input()) print(fbi(n))
- if-esle simple use
a, b, c = 1, 2, 3 if a>b: c = a else: c = b # means c = a if a>b else b
program3
-
question
汉诺塔问题大家都清楚,这里不再赘述。代码要求完成如下功能:
有三个圆柱A、B、C,初始时A上有N个圆盘,N由用户输入给出,最终移动到圆柱C上。每次移动步骤的表达方式示例如下:[STEP 10] A->C。其中,STEP是步骤序号,宽度为4个字符,右对齐。
请编写代码,获得输入N后,输出汉诺塔移动的步骤。
-
输入格式 : 一个整数
输出格式 : 每个步骤一行,每行参考格式如下:[STEP 10] A->C
steps = 0 def hanoi(src, des, mid, n): global steps # the func inside the func makes it necessary to use the global variable if n == 1: steps+=1#actually handle 1-> des;steps++ print("[STEP{:>4}] {}->{}".format(steps, src, des)) else: hanoi(src,mid,des,n-1)# 1~n-1 src -> mid steps+=1#actually handle n->des;steps++ print("[STEP{:>4}] {}->{}".format(steps,src,des))#n->des hanoi(mid,des,src,n-1)# 1~n-1 mid -> des N = eval(input()) hanoi("A", "C", "B", N)
global variable
:the func inside the func makes it necessary to use the global variable- only actually handle requires steps++
- python的函数---python入门学习(三)
- C++ 与 python 语法 对比
- Python Tip(51~55)题详解
- Python爬虫入门(二)requests库
- python基础实例
- Python_import Vs from module import
- 浅析python 中__name__ = '__main__' 的作用
- python yield generator 详解
- python中进程相关知识点。很强大
- Python笔记——排序算法的实现
- python爬虫之xpath
- Python__闭包函数
- Python web 开发
- python tox
- python学习笔记:第五天( 列表、元组)
- Python复习-17.10.08
- Python数据结构
- 设计模式之单例模式——Python实现
- 关于python中的 if __name__=='__main__': 的作用
- Python实现图像平移、旋转、水平镜像等