本文程序用递归分别解决如下问题:
1.基本的调用递归实现顺序和倒序输出1-10的整数。 2.递归打印九九乘法表。 3.舍罕王赏麦问题(64格2倍递增求和)。 4.递归遍历文件。 5.递归实现求Fibonacci数列中的第n项。 6.循环求Fibonacci数列。(效率比递归求解更高)
def forFun_1(i):
if i <= 10:
print(i)
forFun_1(i + 1)
def forFun_3(i):
if i <= 10:
forFun_3(i + 1)
print(i)
def forFun_2(i):
if i >= 1:
print(i)
forFun_2(i - 1)
def multiplication_1(n):
if n < 10:
for m in range(1, n + 1):
print('%d * %d = %d' % (m, n, m * n), end="\t")
print()
multiplication_1(n + 1)
def multiplication_2(n):
if n >= 1:
for m in range(1, n + 1):
print("%d * %d = %d" % (m, n, m * n), end="\t")
print()
multiplication_2(n - 1)
def wheat(n):
if n > 1:
sum, rs = wheat(n - 1)
rs = rs * 2
sum = sum + rs
return sum, rs
else:
return 1, 1
import os
def displayFile(path):
for each in os.listdir(path):
absolute_path = os.path.join(path, each)
is_file = os.path.isfile(absolute_path)
if is_file:
print(each)
else:
print('++++', each, '++++')
displayFile(absolute_path)
print("++++", each, "++++")
def fib_digui_1(n):
if n > 1:
return fib_digui_2(n - 1) + fib_digui_2(n - 2)
else:
return n
def fib_digui_2(n):
if n <= 1:
return n
else:
return fib_digui_2(n - 1) + fib_digui_2(n - 2)
def fib_xunhuan_1(n):
if n > 1:
a = 0
b = 1
fibN = 0
for i in range(2, n+1):
fibN = a + b
a = b
b = fibN
return fibN
else:
return 1
def fib_xunhuan_2(n):
a = 0
b = 1
result = []
for x in range(n):
result.append(a)
a, b = b, a+b
return result
if __name__ == "__main__":
print()
print("------1-10正序输出------")
forFun_1(1)
print("------1-10倒序输出--1------")
forFun_2(10)
print("------1-10倒序输出--2------")
forFun_3(1)
print("------打印九九乘法表------")
multiplication_1(1)
print("------打印 n * n 乘法表------")
multiplication_2(9)
print("------舍罕王赏麦64个格子的总麦子数量:--------")
sum, rs = wheat(64)
print(sum)
print("------舍罕王赏麦第64个格子的麦子数量:--------")
sum, rs = wheat(64)
print(rs)
print("------递归遍历文件:--------")
displayFile(r"C:\Users\lv\Desktop\Python\Python-入门-imooc")
print("------递归--实现裴波那契数列:--------")
Fibs_1 = []
Fibs_2 = []
for i in range(15):
Fibs_1.append(fib_digui_1(i))
Fibs_2.append(fib_digui_2(i))
print(Fibs_1)
print(Fibs_2)
print("------循环--实现裴波那契数列:--------")
res = fib_xunhuan_2(15)
print(res)
Fibs_3 = []
for i in range(15):
Fibs_3.append(fib_xunhuan_1(i))
print(Fibs_3)
最终的运行结果如下:
C:\Users\lv\Desktop\Python\其他代码集\python算法\digui\venv\Scripts\python.exe C:/Users/lv/Desktop/Python/其他代码集/python算法/digui/main.py
------1-10正序输出------
1
2
3
4
5
6
7
8
9
10
------1-10倒序输出--1------
10
9
8
7
6
5
4
3
2
1
------1-10倒序输出--2------
10
9
8
7
6
5
4
3
2
1
------打印九九乘法表------
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
------打印 n * n 乘法表------
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 2 = 2 2 * 2 = 4
1 * 1 = 1
------舍罕王赏麦64个格子的总麦子数量:--------
18446744073709551615
------舍罕王赏麦第64个格子的麦子数量:--------
9223372036854775808
------递归遍历文件:--------
++++ python入门.assets ++++
image-20210911122325988.png
image-20210911122825874.png
image-20210911122901549.png
image-20210911123655527.png
image-20210911124928712.png
image-20210911215839730.png
image-20210912205833493.png
++++ python入门.assets ++++
python入门.md
------递归--实现裴波那契数列:--------
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
------循环--实现裴波那契数列:--------
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
[1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
进程已结束,退出代码为 0
|