设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    & o- W; u$ T3 O( F/ O9 D- C: y1 j/ l
    解释的不错
    , |- V% F2 k. ~5 t2 f8 x  G( G# j' K$ H8 Z1 ], S' b+ }
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    3 d- N; l2 E8 e+ {0 o5 Y4 c, q0 e9 M$ A  c3 Z
    关键要素
    4 Z/ Y5 l1 c5 ~1. **基线条件(Base Case)**
    , E/ F5 W& W# L   - 递归终止的条件,防止无限循环
    6 T2 w4 a4 c  J4 F   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    & Y9 Q, _/ a) o9 b: ]5 f
    6 Q" V3 U* b. |$ U: \" K2. **递归条件(Recursive Case)**
    " H) b5 Q' ^& j$ a   - 将原问题分解为更小的子问题1 o1 Y# O0 h9 q& u2 E# x* H5 q
       - 例如:n! = n × (n-1)!
    : F9 M: }" ~% ^
    + G+ g# T' Q0 ]4 Y3 e 经典示例:计算阶乘/ O9 i/ O' b$ A8 e: @! k; }+ J; i6 n% I
    python, J+ A$ o3 x7 m: f) T* i6 }
    def factorial(n):7 o+ E# F- u; v1 J& I
        if n == 0:        # 基线条件
    ' p: n- S! w* e- j        return 15 M. a4 E' d) P, y" v" b
        else:             # 递归条件
    + A7 c2 Z* ]- k1 [9 n. [% J& Y        return n * factorial(n-1)
    6 T' x) w5 R/ e9 E执行过程(以计算 3! 为例):. S3 |3 ^* @8 ?; [! r- m
    factorial(3)
    8 ]. o4 e6 `! O8 n* T! d- T" K1 F3 * factorial(2)6 c  g( J: p. G4 e' m2 D
    3 * (2 * factorial(1))
    * d5 l& H7 m8 z/ t5 P3 * (2 * (1 * factorial(0))), t: D% b* c! [% \# |
    3 * (2 * (1 * 1)) = 6+ s% p2 G/ {& X
    7 P, t$ L/ E1 D
    递归思维要点
    ; D3 x* [5 T5 k$ {7 Y) F1. **信任递归**:假设子问题已经解决,专注当前层逻辑) V0 X$ t- Q7 f1 _+ h* `. q& B
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    . n5 c' d. M, `. l7 u7 e3. **递推过程**:不断向下分解问题(递)
    * W5 |8 A( H6 w$ d) t' p4. **回溯过程**:组合子问题结果返回(归)
    , I8 {" s* u( ^2 L+ u$ O
    $ D8 H  M5 T4 m. L. w( u* D 注意事项
    : i* z5 G8 `% Y" i5 c$ l必须要有终止条件6 D% \: O. a1 d1 |, F5 O
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    4 L% t, S& _) x. }8 I4 e某些问题用递归更直观(如树遍历),但效率可能不如迭代2 F; ~" ?4 R- N% T) q& F! q
    尾递归优化可以提升效率(但Python不支持)
    * t$ j2 E/ g) F4 s+ W$ W( w
    ' F8 j% I! n7 {) c6 i 递归 vs 迭代
    4 s3 F4 w7 K& i& l|          | 递归                          | 迭代               |( P3 ]4 F; R. {' z
    |----------|-----------------------------|------------------|) d: T, c8 O' o! `" f
    | 实现方式    | 函数自调用                        | 循环结构            |) ?5 Z7 l" Z9 P; ~* A: x* s; R& {
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    3 l* i$ }6 p+ ?8 m/ w5 w" V| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    0 z" _, _$ S7 Y4 |1 l| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |- n' b" [) {) t' H* x
    9 {1 P0 Q. C" Z  p3 F- T
    经典递归应用场景0 C* n3 I: C9 a( X. o9 @
    1. 文件系统遍历(目录树结构)
      z# L3 K9 w$ z6 G6 j8 t" `9 a3 \; v' Z2. 快速排序/归并排序算法3 `2 O( D5 K2 l# z& e- p
    3. 汉诺塔问题
    ( R2 j6 D* ^7 A  O8 k/ F! d4. 二叉树遍历(前序/中序/后序)6 g7 y8 Z& [% G* P- F- O1 K) Z* F
    5. 生成所有可能的组合(回溯算法)4 Z3 r3 ^3 P" d0 T5 _8 x8 I

    1 a. c4 a1 r1 O试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    昨天 08:45
  • 签到天数: 3124 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    4 T4 k; y; P& _! g+ _! \7 x( F% B我推理机的核心算法应该是二叉树遍历的变种。* B  a4 G# `, ^- t6 A2 a
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:) K+ ?& h& z/ j& ^; L
    Key Idea of Recursion
    # y4 Y# w/ d5 F' n- _- z$ R- L
    A recursive function solves a problem by:; K& L9 A. @" h% w. E% {
    , @, H, u& A3 }- W. D* k
        Breaking the problem into smaller instances of the same problem.& u1 T% a! R: ]7 E6 Z5 z

    & Z& J; _3 k0 r, p8 H    Solving the smallest instance directly (base case).
    % Q* Y* A$ [0 I  }# ?; c& r  y% A  m* x" h8 n! V% a* a- E2 }
        Combining the results of smaller instances to solve the larger problem.8 l+ Q8 V4 C& p

    + |4 `' [, c+ H% V9 uComponents of a Recursive Function
    # d% a6 I1 H' C/ m, [! \3 \( |( |' R
        Base Case:
    0 r* Q% c7 |5 C7 n- t! D1 X; T: l
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion./ ]$ I0 _' J' O

    7 H3 E: }) `' S8 n        It acts as the stopping condition to prevent infinite recursion.
    . @7 k! T. F$ Z+ O+ @' x8 ~, [" O6 [, Z& b. U$ {8 F
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    4 a& |; r" @+ c. H3 J
    6 i+ F) s( ^3 |1 U* ~    Recursive Case:
    ! |: J& B) b8 v5 M4 G6 T+ m2 o6 E2 A4 g- W
            This is where the function calls itself with a smaller or simpler version of the problem.: c1 @+ ]4 Z# W  g' x3 \/ R/ I- O
    5 J7 M' h3 U" `2 ]4 h
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).* F& R9 l2 |# a- Z
    - b" S; c3 |& h+ N3 @: I
    Example: Factorial Calculation
    ) T( g* K' x1 t8 v; E
    ) n5 S$ F. _8 O' I: eThe 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:
    0 S* l$ Z: d& H( z- w) ?: _1 n4 @7 {/ ?
        Base case: 0! = 1
    8 O/ R* F8 o5 _) ^' k) |' X/ G4 f! F1 J% h8 W; R, B
        Recursive case: n! = n * (n-1)!- s% i+ R- q" Q  F$ J

    $ h- Z6 J3 d; ^7 D! i$ m2 l, BHere’s how it looks in code (Python):
    4 x( h$ o1 p/ l. `5 kpython& \; _* `( t1 h3 V. s

    % m1 t5 p9 P$ o7 W$ t7 x- t
    ( @/ `  @* V, Idef factorial(n):
    8 ?1 n* J0 x9 b8 B$ M7 w    # Base case) {+ `6 Q, {' ?" Y/ w+ L' S
        if n == 0:
    . G$ k; ^0 o/ G        return 1
    5 z; V; s. P7 {, B+ J    # Recursive case
    ) j7 i' }2 n! V4 R" C+ b    else:% {0 ?2 K! b* p. f; [
            return n * factorial(n - 1)% e$ u6 m8 h4 z) ?; A6 z! I+ p

    4 Z( z6 m' j: Z# Example usage
    5 W- m, Z* N6 w! v5 l- l. ^print(factorial(5))  # Output: 120) Y* r* d+ W: V; A0 A
    , i+ b' v# d% B- u, g# r, x
    How Recursion Works4 w- Z: O1 O' k9 B/ |2 \0 K7 W
    # Q5 j/ R6 @. N  j/ s6 O* D
        The function keeps calling itself with smaller inputs until it reaches the base case.- L( _! r/ a; Q& }- i- t9 u- y. a# i

    + `1 Z$ q4 {6 j    Once the base case is reached, the function starts returning values back up the call stack.+ U$ H9 Y7 K0 ^  U3 f5 E

    6 }! k6 S. s" J4 B) T    These returned values are combined to produce the final result.
    & j" P. O( M" r6 y) V) H  @* K/ y1 z5 T; I0 ^( l' d  b( e
    For factorial(5):$ Z' A- m: U* M+ h5 w+ s
    : Z0 H+ A/ S- c+ ]

    3 w( V! ?4 K! z, f* mfactorial(5) = 5 * factorial(4)
    & V9 E0 Q3 ^5 wfactorial(4) = 4 * factorial(3)' b5 X/ {6 r- ^* r- G0 h9 @# T
    factorial(3) = 3 * factorial(2)
    . Z  K4 C  B! p: Nfactorial(2) = 2 * factorial(1)& @! B0 q  p0 q& y% d: h
    factorial(1) = 1 * factorial(0)
    # ~3 Z+ H( ~" b/ ffactorial(0) = 1  # Base case1 V& O' _' E$ Q. j6 C
    9 @$ w" q# h; f4 p1 T
    Then, the results are combined:
    + z- Z, q& w+ q$ _, b; k$ q# H3 j, v& }8 l/ G: |/ v: X& {

    : M, ?& c1 K9 y+ f6 b5 p/ Afactorial(1) = 1 * 1 = 1
    ( [# P1 F* K- m' F# M0 g4 K2 Z) Afactorial(2) = 2 * 1 = 2" E' r' d7 p" ^: f
    factorial(3) = 3 * 2 = 61 a/ W0 r5 n& x  Q
    factorial(4) = 4 * 6 = 246 ]: J- o- M* q) u2 F% w
    factorial(5) = 5 * 24 = 120% C; n9 t/ h- f' V* M& Z( \7 Q) g
    1 D* Q5 M. S2 c/ J6 _. l9 p
    Advantages of Recursion
    + @8 s& u! C4 d( N8 i7 ~
    0 d( y' d4 H; x+ W* z    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).1 A2 N' o/ f* |0 V  x/ Y

    ' h/ f# m. }4 U; h. Z5 h* x4 a0 L7 v    Readability: Recursive code can be more readable and concise compared to iterative solutions.& ?+ T3 ~9 B% \9 ?
    . f5 M5 a! |: ]5 p  @
    Disadvantages of Recursion
    9 C2 ?, L& u" A1 z* [3 S2 ]- [5 c$ V! ^
        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+ G6 S1 i. S7 g* ]
    2 n' A, P: i! T7 F( g    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).4 _! \- \, D" t5 `
    ' Y( ~3 h" O8 @" }( u, w$ D
    When to Use Recursion
    ) |) w+ ~2 x/ N
    # p/ e% w: S: t! C% n$ r/ k* m* j    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).6 t7 s3 G" `& l5 t/ g9 v" x

    ; G) E4 y+ `" J& z( i) N( e    Problems with a clear base case and recursive case.8 }) a. o9 u8 y; z4 ?+ B
    % y+ a$ M. v& @9 L. u6 `
    Example: Fibonacci Sequence. w/ G4 C4 N& [8 T- }" u
    ; \% d6 F  j- F( K2 `0 f
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:7 y7 G0 `4 t" e

    # I3 d0 y# w* [# N    Base case: fib(0) = 0, fib(1) = 1! K+ v) ?: S: c8 t+ i

    ) _3 G! x# S- w+ C    Recursive case: fib(n) = fib(n-1) + fib(n-2)0 ^# }& ?$ {/ }/ k
    9 {- g0 ~/ z  L$ A
    python
    # ~5 q. X; z- N3 g; y2 v
    9 p* S& ~0 r0 I( p, ^8 P) ^
    ( @0 y' Y6 D8 R4 T' o/ M4 Hdef fibonacci(n):
    $ e/ [2 m2 u/ V. q! b    # Base cases
      W, e0 P+ o* Y3 d  i    if n == 0:
    " P" D2 w! P8 m$ T        return 0. M) q% @/ M! R* i9 O# o
        elif n == 1:/ \6 ?, g+ h3 U, k+ a- T! }
            return 1' s: U# _6 a  S8 q
        # Recursive case
    # U" K# n. D$ h5 R/ [- ?/ ^    else:
    , S& e& _) X  D, m! X        return fibonacci(n - 1) + fibonacci(n - 2)
    * R/ B- i0 h# F) M  C; S
    # h7 L1 ]6 q/ I- y+ g# Example usage
    # E* K5 \4 S4 y8 H* kprint(fibonacci(6))  # Output: 86 g% h- T. B4 `9 r7 g

    2 `# A9 J# l5 u1 z* m& w- OTail Recursion
    # S5 Z; X* J7 p+ R/ _- f4 O& v' w/ i( y; A; K
    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).1 h9 D/ e3 B" ]

    / U8 f  T$ R; `  f  v$ jIn 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-23 13:55 , Processed in 0.037215 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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