设为首页收藏本站

爱吱声

 找回密码
 注册
搜索
查看: 1694|回复: 3
打印 上一主题 下一主题

[科技前沿] 突然想到让deepseek来解释一下递归

[复制链接]
  • TA的每日心情
    开心
    2025-9-8 05:08
  • 签到天数: 3 天

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 2 D% X" |2 B+ n% f

    5 H; X9 M$ C& T  W0 w' W8 w解释的不错; Q- m! J2 G& d6 o

    : \+ P2 \0 u4 w' g" C3 s递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。' @+ a. U) k7 m( b, Q% C2 n
    5 L. X+ v% R$ v& ^" T" G+ ?5 M
    关键要素8 o, ^5 G. P. d9 v2 c) T
    1. **基线条件(Base Case)**2 b# [1 r3 z1 y$ L- k, n
       - 递归终止的条件,防止无限循环. l" n8 a6 J$ O9 a* f# x, u$ u
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1+ |3 ^  u# D2 V0 Y! ]
    3 g4 Y0 \! q1 I: V
    2. **递归条件(Recursive Case)**" }4 U: u% D; S
       - 将原问题分解为更小的子问题
    ' X, g5 A  @7 Q1 O$ @8 b   - 例如:n! = n × (n-1)!3 E2 B8 j' A7 w
    , Y  j6 \" Z" t  P: g9 O9 S$ f
    经典示例:计算阶乘
    7 G1 W6 M+ w, w9 F$ L8 }( B- Vpython
    , d5 w$ o' k4 k' ldef factorial(n):% J0 J% t1 m! i. a
        if n == 0:        # 基线条件* g$ |; U! a4 h+ I  G  M
            return 13 L. z* w: c8 z+ Z$ @1 E
        else:             # 递归条件% y5 ?/ p0 [9 Z2 }& a9 \& G. F
            return n * factorial(n-1)
      ^0 n/ L" ]2 G$ Q/ r执行过程(以计算 3! 为例):8 r/ H: O2 w$ I# g* D2 E  ?- a
    factorial(3)
    - o) x! g3 {$ n0 x* w/ Q3 * factorial(2)
    # J2 C% A* M) C3 * (2 * factorial(1))$ t1 l2 R6 ]6 F6 e0 ~
    3 * (2 * (1 * factorial(0)))" y4 O" D* k4 r8 L* P5 j. f
    3 * (2 * (1 * 1)) = 60 k8 E$ O- I3 k! u

    $ K+ W, l+ t5 }% k4 ]% k, m 递归思维要点
    6 R! F; o7 V% e1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    9 X; D1 _8 N$ Y. Y2. **栈结构**:每次调用都会创建新的栈帧(内存空间)- q2 i6 |: [6 M8 D2 q8 _$ v
    3. **递推过程**:不断向下分解问题(递)" M& U: c$ L$ n. |1 Q5 g
    4. **回溯过程**:组合子问题结果返回(归)
    4 X, }  l8 `  y8 A! ]
    ; Y$ b1 u7 ]1 y5 G/ n+ ^ 注意事项" c8 H3 s% r) z- K1 ?
    必须要有终止条件
    ; H1 H% t: Z& R  ?( m8 }- s0 O) \递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ! w* j2 Z6 b& m1 ~某些问题用递归更直观(如树遍历),但效率可能不如迭代
    * ^# N, h! ]8 N3 _3 t尾递归优化可以提升效率(但Python不支持)
    3 `* J: A- [  H: K, N8 n  D  K# `2 l* j8 v; I5 L  U, I# H3 V+ Z
    递归 vs 迭代0 k9 @1 X$ a. Q& q( U( b
    |          | 递归                          | 迭代               |1 I7 L! L& ^2 s
    |----------|-----------------------------|------------------|
    # O( N0 i8 h/ ]; P3 o| 实现方式    | 函数自调用                        | 循环结构            |
    7 g" ^) V: X3 \3 e" T; Y' s| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    5 N5 i# }$ i/ f2 @| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |/ H2 Z3 r7 }2 v, C; e' Q7 j
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |) ^% f6 k: W* g( f* i+ j/ n

    $ Y0 x+ ]7 n7 S2 a0 q) ?2 n 经典递归应用场景: a! w" {$ _* b. L
    1. 文件系统遍历(目录树结构)6 x4 Z! K0 o$ e
    2. 快速排序/归并排序算法
    2 t; N5 i' v0 x( Z2 J/ I; Y3. 汉诺塔问题1 B& G+ t- |/ W0 y, b8 S
    4. 二叉树遍历(前序/中序/后序)$ h- z2 d9 k* M  g+ ?9 D. ^
    5. 生成所有可能的组合(回溯算法)
    4 M+ K. e  p1 l& }
    0 x2 ^6 ]3 g6 C" T. S试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

    参与人数 3爱元 +26 收起 理由
    pcb + 4
    老票 + 16 涨姿势
    住在乡下 + 6 给力

    查看全部评分

  • TA的每日心情
    擦汗
    1 小时前
  • 签到天数: 3132 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,6 X. C2 z* j! a; k* [
    我推理机的核心算法应该是二叉树遍历的变种。# O6 \# C5 U8 y7 h4 ?7 e
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过10层的,如果输入变量多的话,搜索宽度很大,但对那时的286-386DOS系统,计算压力也不算大。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    板凳
    发表于 2025-2-2 00:45:59 | 只看该作者
    Recursion in programming is a technique where a function calls itself in order to solve a problem. It is a powerful concept that allows you to break down complex problems into smaller, more manageable subproblems. Here's a detailed explanation:* r) r- I" w: \$ e8 }  ~
    Key Idea of Recursion
    % \0 i% ^) t. b  i  p: l: O6 F3 T% C8 @% E6 p# ?
    A recursive function solves a problem by:, q" N% H/ w. Z. v

    0 w1 I. w. d0 Y7 G    Breaking the problem into smaller instances of the same problem.9 A" u: o8 U* ]) j( N
    * m1 ]( |; x, Y. _% j- Y! t) b' k
        Solving the smallest instance directly (base case).
    2 w& ]5 I7 z* N, u1 |, t1 g6 s5 W0 E
        Combining the results of smaller instances to solve the larger problem.( A/ q& G1 H3 S

    1 O+ t, y' ~4 ^% nComponents of a Recursive Function
    8 c6 K9 n& U, k. f1 h+ I8 }0 \
    8 P. S: U4 c* @" ?    Base Case:
    " \1 T  u+ T- ]7 m9 ~
    ' ?% M" e6 ?% G; }+ U! _1 J        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.9 E- j8 ~& T/ ^

    : r7 m1 @- u! `) c! V        It acts as the stopping condition to prevent infinite recursion.5 W4 T" |* W0 h2 G) ^% [$ g
    5 C: c" g( w0 T$ s) [* a
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    . Y; h& G" s- y+ Q! l& h$ ~
    5 O2 L/ T) N) N    Recursive Case:
    . J5 J, D* W/ s
    , B3 K) \* u: h) Y% P1 @% O  G& [! i, @        This is where the function calls itself with a smaller or simpler version of the problem.' h! `. k3 F7 T5 `1 G; |

      a5 h7 `4 s5 i* q3 ]  j+ L        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).& q9 v/ @( {" I; d; k

    2 w" `. |8 x9 s: `# s2 I2 }Example: Factorial Calculation  E0 s& t4 `, j
    * w# \% |+ ~! Z, }) X2 y
    The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n. It can be defined recursively as:% _6 A* E$ o8 t2 {; R  z

    7 N  \+ u# Y: i0 `: s    Base case: 0! = 1
    ! G1 ~0 |$ n( \' n5 @$ t- V+ T/ u5 x
        Recursive case: n! = n * (n-1)!
    % i$ ?4 h- X+ g0 ]. l, d1 U6 f
    # C) s2 R/ m7 z" SHere’s how it looks in code (Python):
      O0 ]4 d) N( }) m2 ypython
    . B# _7 M6 |2 c9 L$ U- S- i8 ^! q% M7 N+ W' p4 ^

    ( x1 S( d9 h+ b+ pdef factorial(n):
    , d  \! J& ^1 I    # Base case
    ' d7 b4 \  Q3 `% F) A    if n == 0:+ e) N; ^  h4 A& R( e( G7 L
            return 1
    * ~9 n# {. _- a1 l4 Y2 k; W    # Recursive case
    . V# P+ ~  B1 z, m8 W9 H& `    else:; t' g4 Z7 X7 o
            return n * factorial(n - 1)
    8 o9 a2 }9 x- i) f4 d. J# h0 ~6 v- r, d
    # Example usage
    4 n8 q# q, g. f1 }# d. ]) {6 uprint(factorial(5))  # Output: 120
    ! j/ S$ Y% k1 i* J! m+ @' R
    . `7 A8 u, b/ l0 r) ]# h4 O* \7 mHow Recursion Works  T7 O( Z' r1 T- F% x
    % g3 ^& T& D4 _: G7 e
        The function keeps calling itself with smaller inputs until it reaches the base case.
    3 C( ]$ }+ M) v2 @: J$ e$ }( T1 k& w  C3 S
        Once the base case is reached, the function starts returning values back up the call stack.
    $ r, N- T' E! c$ ?8 h2 t" a7 Q" G% [% N+ ~/ q6 ]. @
        These returned values are combined to produce the final result.1 [# P- J" @8 Q  T- b6 F

    3 ?3 j' Z* Y3 M% QFor factorial(5):
    ) O! L8 b& T6 L, ~( E$ L' k! z, A
    2 \% l# L' t( @  _( I1 t
    factorial(5) = 5 * factorial(4)5 M- Y4 I% ^$ L. j
    factorial(4) = 4 * factorial(3)
    ' H5 }" S! x/ a2 ~1 t* t4 ?factorial(3) = 3 * factorial(2)2 V+ w) ?  Q( [* w
    factorial(2) = 2 * factorial(1). Y- X# @' T4 K* \
    factorial(1) = 1 * factorial(0). S% R" F3 I* u& R/ K/ G3 T
    factorial(0) = 1  # Base case
    1 ?' ^5 y' A1 I7 g2 V( t3 U
    , N  p2 V7 S$ Y' L& r" lThen, the results are combined:
    " P% w- {* m; e8 W
    6 b8 n# d1 P5 P& [8 i) n, X, f! m: |) _; j- e
    factorial(1) = 1 * 1 = 1! ?# u+ q! f5 o) y
    factorial(2) = 2 * 1 = 21 ~5 g# R4 t/ v8 V8 i
    factorial(3) = 3 * 2 = 6
    * j! t0 P0 n1 S9 V1 J/ sfactorial(4) = 4 * 6 = 24
    ( R5 Q! ]/ H5 o9 p( Dfactorial(5) = 5 * 24 = 1204 ], B1 h# g9 n: D! O. ~# K

    ! H0 O) h" n5 s' oAdvantages of Recursion
    / O. Y* k4 J5 n
    7 {- w  H- q( e) v8 t    Simplicity: Recursive solutions are often more intuitive and easier to write for problems that have a natural recursive structure (e.g., tree traversals, divide-and-conquer algorithms).
    - K8 F5 o* _5 V2 p
    0 y( {/ O" ^) }0 }1 f    Readability: Recursive code can be more readable and concise compared to iterative solutions.5 D1 I  U, U' S9 L6 w! R# B+ N, s6 S- q% L
    $ {% e' M- G$ F8 W
    Disadvantages of Recursion8 Y) g0 K' x6 i; C3 N7 W3 e

    ; z% T$ [. t/ o6 T# n: H    Performance Overhead: Each recursive call adds a new layer to the call stack, which can lead to high memory usage and potential stack overflow for deep recursion.
    , h: [- E9 ~) z0 p/ M
    9 t) h# R$ W+ g" b    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    - n' p- t* T( F
    % k3 k/ L$ b/ Q( EWhen to Use Recursion  X" P- z; V: s- n% {
    + ~4 C: h6 O- z# p0 ]& w
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    1 c* k& y! G, _6 o
    ) k+ b' M; v6 A: ?: c1 A  ?    Problems with a clear base case and recursive case.
    ) ?+ t: w# j, y, v1 Z% v' i1 C/ V% }+ J, h- S/ Q
    Example: Fibonacci Sequence8 \: B# J  J) G( k( d- e% b

    1 O3 I3 n8 v2 d6 uThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    1 I8 Z9 Y4 g+ v5 K: r2 y
    . V; C0 [% C3 [% ^* j$ O3 q- _    Base case: fib(0) = 0, fib(1) = 18 u0 M2 p8 ~! e7 R, p% Y5 d
    - t/ }) ?, x; q! B7 p2 o1 Y& m- d
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    9 b1 H- f# @# j* k5 j  }! n# t" y* Z
    python
    ! @8 ^1 e6 ^3 \2 u; b/ T$ {# a9 }1 |7 W! G& M
    % v& J4 V/ |  z" u4 A& E
    def fibonacci(n):
    2 w8 V; {# G0 ?9 t) s' E2 b    # Base cases6 w% T, n( r. K& \( u; P1 L
        if n == 0:1 _* ~' `# p3 U& n
            return 0- r. J0 F2 P. U- R: p# i: t  x* Q1 c
        elif n == 1:
    , L4 e" Z% Z8 I: c        return 1$ v$ I( v5 F+ w3 }0 ^0 ~
        # Recursive case
    / }& ?% B, @0 x9 [    else:
    % u' a( Y2 I- b2 R2 Y        return fibonacci(n - 1) + fibonacci(n - 2)6 p5 c8 `. M6 t6 ?- x( K5 @

    . z: D5 \9 r9 j. [. r8 {& }& P# Example usage
    * U/ F! `* f5 p% |- H9 Wprint(fibonacci(6))  # Output: 85 g% V8 @: I6 W7 c8 y6 P
    0 R+ ^! m( w/ _* \$ O: ?
    Tail Recursion
    2 f# m% ]7 J$ s1 M9 c0 \3 x0 v9 L' q, o- \$ M
    Tail recursion is a special case of recursion where the recursive call is the last operation in the function. Some programming languages optimize tail-recursive functions to avoid stack overflow, but not all languages (e.g., Python does not optimize tail recursion).# p4 X0 l9 j% U
    + y  v& v2 }! @' Q$ {; O0 [6 \3 L
    In summary, recursion is a fundamental concept in programming that allows you to solve problems by breaking them into smaller, self-similar subproblems. It’s important to define a base case to avoid infinite recursion and to understand the trade-offs between recursion and iteration.
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    地板
    发表于 2025-2-2 00:47:27 | 只看该作者
    我还让Deepseek 给我讲讲Linux Kernel Driver 现在的开发流程,让一个老同志复习复习,快忘光了。
    回复 支持 反对

    使用道具 举报

    手机版|小黑屋|Archiver|网站错误报告|爱吱声   

    GMT+8, 2025-12-31 15:46 , Processed in 0.035276 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表