Commit def375df by BellCodeEditor

auto save

parent 0f53c1f3
Showing with 57 additions and 0 deletions
#一个函数在内部直接或间接调用函数本身,我们把这种情况称为递归。
#递归的过程分为两步:
#将问题一层一层的传递下去,叫做递;
#到达最后一层后(边界),利用最后一层的答案,将问题一步步解决,这个过程叫做归。
#递归做累加 1+2+3+4+……+num
def sum_number(num):
#递归的边界(出口)
if num == 1:
return 1
else:
temp = num + sum_number(num-1)
return temp
result = sum_number(999)
print(result)
\ No newline at end of file
#请计算出第几列斐波那契数列会大于20
def func(n):
if n<=2:
return 1
else:
value = func(n-1) + func(n-2)
return value
for i in range(0,20):
if func(i) > 20:
print('第',i,'列斐波那契数列会大于20')
break
\ No newline at end of file
#求阶乘 1*2*3*4****n n * n-1 * n-2 * n-3 * …… * 1
#找出阶乘值大于等于600的数
def func(n):
if n == 1:
return 1
else:
value = n * func(n-1)
return value
for i in range(1,20):
if func(i) >= 600:
print(i)
break
def power(x,y):
if y == 0:
return 1
else:
return x * power(x,y-1)
print(power(2,3))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment