#使用递归算法计算乘积
def mul(num1,num2,result):
if num1==0 or num2==0:
result+=0
elif num2<0:
result-=num1
num2+=1
result=mul(num1,num2,result)
elif num2>0:
result+=num1
num2-=1
result=mul(num1,num2,result)
return result
def main():
num1=int(input("num1:"))
num2=int(input("num2:"))
result=0
result=mul(num1, num2, result)
print(result)
if __name__=='__main__':
main()
#递归法求斐波那契数列和
def fib(n):
if n==1 or n==2:
return 1
else:
m=fib(n-1)+fib(n-2)
return m
if __name__=='__main__':
print(fib(10)) #结果为55
python使用递归算法计算乘积和斐波那契数列
最新推荐文章于 2022-11-19 23:14:23 发布