Python進階-演算法-遞迴

娜娜0130發表於2018-08-21
版權宣告:如需轉載,請註明轉載地址。 https://blog.csdn.net/oJohnny123/article/details/81911889

 

1、遞迴就是自己調自己

 2、在使用遞迴策略時,必須有一個遞迴出口,也就是得有一個明確的遞迴結束條件。

3、遞迴演算法效率並不是很高,而且容易棧溢位。

4、遞迴演算法寫的程式都會很簡潔。

程式碼:

def fun1(x):
    if x > 0 :
        print(x)
        fun1(x - 1)


def fun2(x):
    if x > 0 :
        fun2(x - 1)
        print(x)


fun1(5)
print(`=`*100)
fun2(5)
print(`=`*100)

 執行結果:

/Users/liaoyangyang/crc/codes-python/LearnPython/venv/bin/python /Users/liaoyangyang/crc/codes-python/LearnPython/test.py
5
4
3
2
1
====================================================================================================
1
2
3
4
5
====================================================================================================

Process finished with exit code 0

 


相關文章