zoukankan      html  css  js  c++  java
  • [ Python ] 递归函数

    一、 什么是递归函数

        如果一个函数在内部调用自身本身,这个函数就是递归函数。
        在递归函数中,是一个有去有回的过程。

    二、递归函数的说明

    def fact(n):
            if n == 1:
                    return 1
            return n * fact(n - 1)
    
    print(fact(5))
    
    # fact(5)
    # ===> 5 * fact(4)
    # ===> 5 * (4 * fact(3))
    # ===> 5 * (4 * (3 * fact(2)))
    # ===> 5 * (4 * (3 * (2 * fact(1))))
    # ===> 5 * (4 * (3 * (2 * 1)))
    # ===> 5 * (4 * (3 * 2))
    # ===> 5 * (4 * 6)
    # ===> 5 * 24
    # ===> 120
    

        通过上面的实例,可以总结出:
            递归函数就是在函数内部调用自身本身,直到自身返回的是一个具体的值而不是函数,然后从内向外逐一返回函数的值,因此递归是一个有去有回两个过程组成的;

    三、一个递归函数的实例

    menu = {
            '陕西省': {
                    '西安':{
                            '未央区':{},
                            '莲湖区':{},
                            '高新区':{},
                    },
                    '咸阳':{
                            '秦都区':{},
                            '渭城区':{}
                    }
            },
            '四川省': {
                    '成都':{
                            '锦江区':{},
                            '青羊区':{},
                            '金牛区':{},
                    },
                    '绵阳':{
                            '涪城区': {},
                            '游仙区': {},
                    }
            }
    }
    
    def threeTL(dic):
            while True:
                    for key in dic: print(key)
                    choice = input('>>>').strip()
                    if choice == 'b' or choice == 'q': return choice
                    elif choice in dic and dic[choice]:
                            res = threeTL(dic[choice])
                            if res == 'q': return 'q'
                    elif not choice or choice not in dic:
                            continue
    
    threeTL(menu)
    menu_three.py

    该实例是选择省打印城市,输入城市打印区, 当用户输入 b 返回上一层,当用户输入 q 退出整个程序,这里最难理解的就是 b 和 q 的用法;

    分析如下:

    (1)正常递归深入过程:

    # def threeTL(dic):
    #     while True:
    #         for key in dic: print(key) # 陕西省、四川省
    #         choice = input('>>>').strip()   # 用户输入:陕西省
    #         if choice == 'b' or choice == 'q': return choice
    #         elif choice in dic and dic[choice]:
    #             res = threeTL(dic[choice])  # 递归返回 threeTL(dic['陕西省'])
    #             if res == 'q': return 'q'
    #         elif not choice or choice not in dic:
    #             continue
    #
    # def threeTL(dic): # dic = dic['陕西省']
    #     while True:
    #         for key in dic: print(key) # 西安、咸阳
    #         choice = input('>>>').strip()   # 用户输入:西安
    #         if choice == 'b' or choice == 'q': return choice
    #         elif choice in dic and dic[choice]:
    #             res = threeTL(dic[choice])  # 递归返回 threeTL(dic['陕西省']['西安'])
    #             if res == 'q': return 'q'
    #         elif not choice or choice not in dic:
    #             continue
    #
    # def threeTL(dic): # dic = dic['陕西省']['西安']
    #     while True:
    #         for key in dic: print(key) # 未央莲湖高新
    #         choice = input('>>>').strip()   # # 用户输入:未央区
    #         if choice == 'b' or choice == 'q': return choice
    #         elif choice in dic and dic[choice]:
    #             res = threeTL(dic[choice])  
    #             if res == 'q': return 'q'
    #         elif not choice or choice not in dic: # 因为未央区没有值,进入下一次循环
    正常递归深入过程
    threeTL(menu)
    -->threeTL(menu['陕西省'])
    -->threeTL(menu['陕西省'])-->threeTL(menu['陕西省']['西安'])
    -->threeTL(menu['陕西省'])-->threeTL(menu['陕西省']['西安'])-->threeTL(menu['陕西省']['西安']['未央区'])
    

    (2)当用户输入 ' b ':

    def threeTL(dic): # dic = dic['陕西省']['西安']
            while True:
                    for key in dic: print(key) # 未央莲湖高新
                    choice = input('>>>').strip()   # # 用户输入:b
                    if choice == 'b' or choice == 'q': return choice # 返回'b',程序退出
                    elif choice in dic and dic[choice]:
                            res = threeTL(dic[choice])  
                            if res == 'q': return 'q'
                    elif not choice or choice not in dic:
                            continue
    
    def threeTL(dic): # dic = dic['陕西省']
            while True:
                    for key in dic: print(key) # 第二次while循环打印 <西安、咸阳>(第一次while循环是在递归函数深入的时候执行)
                    choice = input('>>>').strip()   # 第二次while循环到此,用户输入 b
                    if choice == 'b' or choice == 'q': return choice # 返回'b',程序退出
                    elif choice in dic and dic[choice]:
                            res = threeTL(dic[choice])  # res = 'b', 本次if循环没有匹配到res退出,进行下一次while循环
                            if res == 'q': return 'q'
                    elif not choice or choice not in dic:
                            continue
    
    def threeTL(dic):
            while True:
                    for key in dic: print(key) # 第二次while循环打印 <陕西省、四川省>(第一次while循环是在递归函数深入的时候执行)
                    choice = input('>>>').strip()   # 第二次while循环到此,用户输入 b
                    if choice == 'b' or choice == 'q': return choice # 返回'b',整个程序退出,递归结束
                    elif choice in dic and dic[choice]:
                            res = threeTL(dic[choice])  # 递归返回 threeTL(dic['陕西省'])
                            if res == 'q': return 'q'
                    elif not choice or choice not in dic:
                            continue
    输入'b',代码执行过程

    (3)当用户输入 'q'

    def threeTL(dic): # dic = dic['陕西省']['西安']
            while True:
                    for key in dic: print(key) # 未央莲湖高新
                    choice = input('>>>').strip()   # # 用户输入:q
                    if choice == 'b' or choice == 'q': return choice # 返回'q',程序退出
                    elif choice in dic and dic[choice]:
                            res = threeTL(dic[choice])  
                            if res == 'q': return 'q'
                    elif not choice or choice not in dic:
                            continue
    
    def threeTL(dic): # dic = dic['陕西省']
            while True:
                    for key in dic: print(key) # <西安、咸阳>
                    choice = input('>>>').strip()    
                    if choice == 'b' or choice == 'q': return choice 
                    elif choice in dic and dic[choice]:
                            res = threeTL(dic[choice])  # res = 'q'
                            if res == 'q': return 'q'    # 匹配到 res='q' 返回 'q' 程序退出
                    elif not choice or choice not in dic:
                            continue
    
    def threeTL(dic): # dic = dic['陕西省']['西安']
            while True:
                    for key in dic: print(key) # 未央莲湖高新
                    choice = input('>>>').strip()   # # 用户输入:q
                    if choice == 'b' or choice == 'q': return choice # 返回'q',程序退出
                    elif choice in dic and dic[choice]:
                            res = threeTL(dic[choice])  
                            if res == 'q': return 'q'
                    elif not choice or choice not in dic:
                            continue
    输入'q',代码执行过程
  • 相关阅读:
    hdu 3666 差分约束系统
    hdu 1198农田灌溉
    常微分方程(阿諾爾德) Page 45 相空間,相流,運動,相曲線 註記
    高等微積分(高木貞治) 1.4節 例2
    常微分方程(阿諾爾德) Page 45 相空間,相流,運動,相曲線 註記
    解析函數論 Page 29 命題(2) 函數模的有界性
    高等微積分(高木貞治) 1.4節 例2
    解析函數論 Page 29 命題(1) 有界閉集上的一致連續性
    解析函數論 Page 29 命題(3) 模的下界的可達性
    解析函數論 Page 29 命題(2) 函數模的有界性
  • 原文地址:https://www.cnblogs.com/hukey/p/9336233.html
Copyright © 2011-2022 走看看