设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    . @7 X' Q! K: c3 {
    ' \2 x; H. }) `1 T0 S解释的不错
    ! G- x6 i0 w, m1 x9 V8 P1 K
      i; z# |" N' ^7 M5 E递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。. D( a$ g. @+ ~) x" K- \, h* {
    6 h1 R& F) O1 L  f/ t
    关键要素# {- C2 E  v  ~% O% M
    1. **基线条件(Base Case)**' }) U* l3 x4 @; F: N0 O
       - 递归终止的条件,防止无限循环/ v( C# |( m5 ^$ I) o
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    - V- V5 B& {- E0 E, _! s' p; d5 J4 C/ L9 u
    2. **递归条件(Recursive Case)**# U  K' c6 D4 P, [0 I& T3 T
       - 将原问题分解为更小的子问题+ j) ^) W; b4 q& r. a
       - 例如:n! = n × (n-1)!  ^' u7 J( \$ N) x( S! c1 K$ E: W
    9 l5 K: ?$ I, U. U; R
    经典示例:计算阶乘/ V( f+ Q0 j6 |
    python/ A" j" k6 F+ z/ t
    def factorial(n):
    7 O* k& J- `' P+ n0 r    if n == 0:        # 基线条件7 C) F4 Z- e) l" m+ y
            return 1
    - L( X+ F) r  h2 N  j2 A    else:             # 递归条件
    2 b1 D, V/ o" Y' r' @) D  {        return n * factorial(n-1)6 y5 h! a* Z0 X. ^$ K) V
    执行过程(以计算 3! 为例):5 E, P- q" |' h. a
    factorial(3)
    4 k# O5 O" @  A  Q3 * factorial(2); m7 |! D& c8 ~/ `& k6 N8 H7 m
    3 * (2 * factorial(1))
    ! A6 a/ `9 N7 p5 W3 * (2 * (1 * factorial(0))). x9 {" M/ L, _$ Y/ o; D
    3 * (2 * (1 * 1)) = 6
    . G. o  B! G1 K( m5 h1 Z; l  Q5 U
    递归思维要点/ t$ ]0 u( m( N5 \
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    8 J* o$ J' O; m! f* I' _$ h2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    6 _3 S; [' U0 f8 }/ D' u! Z3. **递推过程**:不断向下分解问题(递)6 a$ {2 K+ u/ |( e5 V
    4. **回溯过程**:组合子问题结果返回(归)4 t2 `2 D1 k3 R% T( b

    2 x, p2 V# y* x 注意事项! o( q8 n0 K" @1 v6 C
    必须要有终止条件
    ) I7 B8 G, b, x) v; _# T5 [递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    6 Z$ w6 p! s: Y* x! E& K7 s某些问题用递归更直观(如树遍历),但效率可能不如迭代$ K  f: I1 O9 k) y3 R5 t
    尾递归优化可以提升效率(但Python不支持)
    0 e* C+ ^9 l/ U- }# y/ z
    + [: I. L; G/ \1 I 递归 vs 迭代
    , T+ E8 f3 ~* l& y6 T/ \! I7 a4 T|          | 递归                          | 迭代               |2 h3 Q$ e/ P; I4 ^
    |----------|-----------------------------|------------------|- H! u5 o* j% \$ w: F  `5 X. {
    | 实现方式    | 函数自调用                        | 循环结构            |
      H6 u( }, H& u| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    5 K0 Z5 A3 g; w4 V| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |: l$ z+ H8 E* s9 F% C6 C* |
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    % @% u- C6 H$ p+ f8 Q# G9 p# B. \8 X7 }: c' x
    经典递归应用场景
    - A: C( q6 [  m5 M5 e1. 文件系统遍历(目录树结构)
    # _$ |. M: K7 |9 v) _9 ~2. 快速排序/归并排序算法. p, S- ]5 F. w0 G. q- u
    3. 汉诺塔问题
    4 x4 `+ g/ G( E3 a4. 二叉树遍历(前序/中序/后序)
    * C5 T) e" ^8 r2 H+ C5. 生成所有可能的组合(回溯算法)$ G- h! V% E0 m" x) ]/ x

    0 t. u# E  F3 G; v- ?# W2 g试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    3 小时前
  • 签到天数: 3178 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    # G. E' Y7 w  x2 S# z4 F1 H我推理机的核心算法应该是二叉树遍历的变种。
    4 y  F/ P% ]  B! G' z( |) d另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:# W' R! b' k! B& a; t, K$ J2 o' t
    Key Idea of Recursion
    4 ^" q( w* J3 A1 T6 z) h  [' \' \6 D1 s) P
    A recursive function solves a problem by:' g/ \9 a1 P( j( k& ~( N! i2 A
    1 A" E. }2 I- V' S, Q) [* S( q
        Breaking the problem into smaller instances of the same problem.8 U" a# C: ^6 {, g3 ~) C
    9 Q; {0 v- G, J
        Solving the smallest instance directly (base case).9 Q) N$ m. d; y2 |

    5 W$ [$ z  P4 S/ P/ G! f    Combining the results of smaller instances to solve the larger problem.
    7 h; E* A2 J# t6 T* {2 l7 h( S( ^
    Components of a Recursive Function. O: R+ {: _3 i. s% T) z9 D
    ! t8 N4 @6 m. M, A- S- |* Q
        Base Case:9 J# [, R9 g5 t+ B$ q( M

    ; N: V9 z% i3 }; A; |/ L        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    , {; T3 o. a' z4 p# k
    * d9 c* }- @0 l$ k8 P! Z% r" y2 j  z3 m        It acts as the stopping condition to prevent infinite recursion.# |3 G% f9 G4 D- W8 R) h
    0 ]5 ?" F+ R$ F: V7 f7 x! z
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.+ f/ F' B4 [9 Y* X

    % L: R# d9 }+ W, ^+ j; |( ]    Recursive Case:0 U* R5 X( m5 N. @
    7 ?! d- t( {9 t2 W) p
            This is where the function calls itself with a smaller or simpler version of the problem.
    5 \$ P+ t6 a, i6 V  F6 o- A
    . I1 d! b# C3 q& r5 w, X; u  T        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).! f+ I- K; y0 g# C

    ) r9 I: X4 Y+ h; _; KExample: Factorial Calculation
    6 q: N$ t9 @- ^2 m$ v. L
    3 `, r* h6 {3 \0 G7 V& }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:
    4 O7 [1 g5 S6 U$ H& {+ S9 Y6 b
    ( W1 d/ q( `9 x( }" |    Base case: 0! = 1
    ( v) k8 L$ R) _" j& k: D- A" K. @% K+ E9 X+ P
        Recursive case: n! = n * (n-1)!1 m: U' N" }8 G9 z2 A! V

    + h) L5 A% q8 d  L" w# s! eHere’s how it looks in code (Python):
    ! d; m4 U8 u. [+ c6 L. Vpython9 S- B- b8 }7 I7 p& d2 V+ a
    ; [1 u9 \7 J7 C' W) @. y: w
    8 V% x6 l4 k. }1 t  A- r) F4 i
    def factorial(n):
    ' _% Z$ U1 k% @' W    # Base case
    9 C2 ^+ |1 Z% v4 G5 A    if n == 0:
    " }* f/ C5 v9 Z; ]- X5 Q        return 15 T( [. Z/ [2 F: s, i
        # Recursive case2 ~- ?0 Q( Q2 ~9 O
        else:; y# S$ P. e% u
            return n * factorial(n - 1)9 c3 v' S4 C; l9 F" t

    . I8 `) b, p2 s# Example usage
    ' C# ]5 l4 R) X5 Z; j8 p* yprint(factorial(5))  # Output: 1207 l; e0 G. W, P4 ]" D  V# d
    2 O3 v8 S  Y! L/ ]7 A. H& v
    How Recursion Works
    ! @. k0 @3 }( P+ O! n( `6 B& J; J, u0 r' U4 M6 j
        The function keeps calling itself with smaller inputs until it reaches the base case.
    7 q+ Z+ n# a) Y# p( \) Q
    % J' p. Z: F2 [' A    Once the base case is reached, the function starts returning values back up the call stack.
    # I% G) \9 T6 ]( ~7 N" {- I+ e6 E) U; M- R1 ~* c) _, G2 @
        These returned values are combined to produce the final result.3 e& m: [( W& Y4 G4 H
    # Y+ O8 {9 H) ~
    For factorial(5):
      Z5 _) l1 _5 `( q) J9 |/ i
    , N! q9 P, j3 T/ m. Y- v) Y5 r
    + v# N5 X) G4 i- \3 U; V8 jfactorial(5) = 5 * factorial(4)
    3 M! U1 [# c8 n8 i, r  tfactorial(4) = 4 * factorial(3)
    + n# h( A: [: {. c7 t, ]factorial(3) = 3 * factorial(2)
    6 e. q: Z4 i5 ?3 \7 D: L# V$ Pfactorial(2) = 2 * factorial(1); q; p1 A8 L/ |3 ?
    factorial(1) = 1 * factorial(0)6 {2 Z6 H/ f1 n6 B1 r7 X) @
    factorial(0) = 1  # Base case
    * _6 H2 V  e: Q& i- e* m' S" D$ q1 D* G  s) H2 R( n7 W
    Then, the results are combined:
    4 b( ?1 P! I$ W) M& m" E
    7 F% n; e% ?; {" J- o
    ) G, Q5 E# ]5 u' Efactorial(1) = 1 * 1 = 1" q3 u4 u# z, A
    factorial(2) = 2 * 1 = 2
    ) C0 b! m/ z8 J' d6 p" v' hfactorial(3) = 3 * 2 = 6
    ; N* U  ]) q6 |5 tfactorial(4) = 4 * 6 = 24
    0 y, m7 E7 ^8 o. o& Y7 Sfactorial(5) = 5 * 24 = 120, `/ j9 }& e9 H" p' y
    , T7 T6 D3 K- k% `& x/ r9 V
    Advantages of Recursion
    3 p& u1 w( X5 u  ]. s* O/ T# S; D3 v. l+ m1 }
        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).% {2 ~1 Q! `: O: Y% l

    ' D- `$ d" B( J4 _1 y    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    7 B2 ^4 s2 k, e$ d; K4 K$ N. d" N
    ) M- ~' ?* f. r+ H! h* J/ \Disadvantages of Recursion
    . l; c6 v9 w) [! U$ P2 E6 ^7 G3 c" g- h. \2 q
        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.2 z9 s" s: r: |0 ^3 r
    5 U. G9 h' T4 j$ d6 \2 R6 c
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).2 t8 L7 e- \" M% e3 o3 z) K
    2 e3 I  e  {* l+ g3 Y. T9 m
    When to Use Recursion' u1 J2 y1 o& x& @6 Q' q

    ) m! {& p4 l+ n1 K$ U    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).7 O% F$ H0 p; v: E6 K
    $ }/ B; F7 @; j
        Problems with a clear base case and recursive case.
    ; n; E8 I2 s$ I) Z) I" C# N
    . S' K& [% n2 q8 m) ^7 U/ VExample: Fibonacci Sequence) y% u% W# }# i2 X& m  I% _
    * H. j* r! h% m
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:: }1 Y2 n  |* V( e1 D1 \3 l- Z

      ?/ @4 E/ j% b( A2 i7 u% u    Base case: fib(0) = 0, fib(1) = 14 R9 F1 D- l$ Z) u1 e0 O% O  M
    ; V) W7 Y) S" f; Z0 e/ N
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    5 b6 X0 |9 \$ F+ D$ K" T8 i0 W
    : B2 a! c+ V, |; J( I6 K( vpython
    & e5 a9 L# o" N7 y: x1 Y& t; @2 p8 I" }$ ]2 o
    2 T) Z  q, w: P% J9 p
    def fibonacci(n):
    0 Y/ a7 h& ]2 d2 e( x    # Base cases
    6 u% S- ^3 c! r# o* t3 W    if n == 0:
      x$ w1 i5 J( h; t1 K7 o        return 0
    + q3 r  K# @  ~( a7 O    elif n == 1:
    ; O  w! s; r0 ?+ N$ Y        return 1: q# d" `; T- B) n  L3 u# ^
        # Recursive case5 @0 ~3 N) {0 p6 l; H
        else:9 C4 P9 N# ^& k) X8 X5 `0 s
            return fibonacci(n - 1) + fibonacci(n - 2)1 U" N' \7 h' o

    4 |: y$ ^( q* U6 y# Example usage: h0 M% k7 b- l
    print(fibonacci(6))  # Output: 8# \* y; B- S( x( s8 F- o0 M
    ! ?" u1 x1 S6 @3 h6 U5 N8 K
    Tail Recursion# u2 C% i% {* u( r  V

    ) J7 z) w' ^7 ^1 y8 CTail 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).0 k. S4 d' I% W/ m6 n( L

    ! l2 C# t( Z$ A! i; N2 UIn 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, 2026-2-19 10:25 , Processed in 0.056424 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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