1,整理笔记
2,用递归的方法实现斐波那契数列。
得出 1,1,2,3,5,8,13……
## 列表实现
# list2 =[]
# for i in range(20):
# if i ==0 or i ==1:#第1,2项 都为1
# list2.append(1)
# else:
# list2.append(list2[i-2]+list2[i-1])
# print(list2)
# 第一种是 求出第几个数的斐波那契数列
def func1(n):
if n == 1 or n == 2:
return 1
else:
return func1(n-1) + func1(n-2)
list1 = func1(20)
print(list1)
> E:\project\python.exe C:/Users/Administrator/Desktop/四阶xpat爬虫系列/Requests/Requests01/Requests01/zuoye1.py
6765
Process finished with exit code 0
## 第二种
```python
def func(n, i=0, j=1):
i, j = j,i+j
print(i)
if n == 1:
return
func(n-1,i,j)
func(20)
E:\project\python.exe C:/Users/Administrator/Desktop/四阶xpat爬虫系列/Requests/Requests01/Requests01/zuoye1.py
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
Process finished with exit code 0
用递归的方法获取一个list的最大值和最小值,
return(最大值,最小值)
list = [8,928,3,5,8811,444,3333,22,99]
def max(list):
if len(list) == 2:
return list[0] if list[0] > list[1] else list[1]
return list[0] if list[0] > max(list[1:]) else max(list[1:])
def min(list):
if len(list) == 2:
return list[0] if list[0] < list[1] else list[1]
return list[0] if list[0] < min(list[1:]) else min(list[1:])
max_data = max(list)
print('最大值是:',max_data)
min_data = min(list)
print('最小值是:',min_data)
运行后
E:\project\python.exe C:/Users/Administrator/Desktop/四阶xpat爬虫系列/Requests/Requests01/Requests01/zuoye1.py
最大值是: 8811
最小值是: 3
Process finished with exit code 0
老师讲的:
1,斐波那契数列:
def F(n):
if n==1 or n==2:
return 1
return F(n-1)+F(n-2)
def max_min(L,start,end):
if end-start <=1:
return (max(L[start],L(end),min(L[start],L[end])
max,min1 = max_min(L,start,(start+end)//2
max,min2 = max_min(L,start,(start+end)//2+1,end)
return (max(max1,max2),min(min1,min2))
未完待续……