2019年9月2日 星期一

遞迴演算法

遞迴演算法
定義:演算法(函式)中有呼叫自己(Self Calling)的敘述

from http://notepad.yehyeh.net/Content/DS/CH02/1.php
from https://medium.com/ccclub/ccclub-python-for-beginners-tutorial-11ed5d300d3d
===========

def factorial(n):
    if n==0 or n==1:
        return 1
    else:
        return n*factorial(n-1)
print(factorial(5))

===========
def f(n):
    if (n==1):
        re=1
    else:
        re=n*f(n-1)
    print(n,"階乘等於",re)
    return re
n=int(input("enter number?"))
result=f(n)
print(n,"階乘等於",result)
==============from  https://sites.google.com/site/zsgititit/home/python-cheng-shi-she-ji/di-hui-python

沒有留言:

張貼留言