设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 6 r$ U7 y5 n" k9 g
    & l) ^* n' S: ^: m
    解释的不错
    9 ^7 z3 L  B( M9 \8 L- u# G5 s2 W$ d# L$ D" E/ g/ q
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。3 u9 ^/ v9 R1 _" p7 B6 i$ S# }

    3 _: c: _8 m2 A9 o3 | 关键要素
    ' h& F' w  G7 u9 r) n& X, m3 X1. **基线条件(Base Case)**4 X% V- A! f5 U2 q
       - 递归终止的条件,防止无限循环) c  Q/ O9 _1 i4 f/ Z* w
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    4 a& E1 K  q& Q' Q6 n+ r5 k
    . a  W) l' @9 H- p2. **递归条件(Recursive Case)**
    ( @* T. D' h6 k( }& Z   - 将原问题分解为更小的子问题  U" n0 t" y1 l+ w3 v; h9 f% M
       - 例如:n! = n × (n-1)!; f+ A; H! W3 O; d+ x

    ! J" v1 g$ o& x1 u, C4 A. N9 T 经典示例:计算阶乘! Z/ a) ~! Q  y: p5 l
    python
    0 K; l2 B: y- q0 g5 x; O% edef factorial(n):
    " c+ Y/ \+ G7 J    if n == 0:        # 基线条件1 p8 G. N* B% s& w" @
            return 1, W7 P8 v, G% \
        else:             # 递归条件  ]* \9 J2 c4 l
            return n * factorial(n-1)
    * @% z. b- x* P9 Q; ^' D4 d执行过程(以计算 3! 为例):
    1 o/ l& c7 ]0 h5 }factorial(3). M9 [- c: O3 m. L+ \
    3 * factorial(2)
    ! N  Q* u  w" D4 n( l7 g* M3 * (2 * factorial(1)). Y+ w( y3 L6 @9 k6 N5 ?
    3 * (2 * (1 * factorial(0)))
    * c0 r; v6 I: ]  [( q8 Y3 * (2 * (1 * 1)) = 6# y7 I* C; F+ {9 B
    . o# @# q- [+ J$ j
    递归思维要点; s, M# k+ k3 c+ ^7 D: N
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑* w7 Q" G( _% q" }- X( S
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    6 W7 L3 |) v; v+ m+ K' I3. **递推过程**:不断向下分解问题(递)
    : D: ?- w& y- b& c, d4 F4. **回溯过程**:组合子问题结果返回(归)1 A8 L) e& a( W2 G

      s7 [/ ?* Z2 M8 M 注意事项
    ; ^; \$ }; Y0 \: G1 a必须要有终止条件+ F$ t. d3 a$ F1 D5 G6 p
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    * i# s) B& A% }. Q0 ]某些问题用递归更直观(如树遍历),但效率可能不如迭代- }0 T& a- {0 M; i  r0 Y" _9 |
    尾递归优化可以提升效率(但Python不支持)
    ! o! V& \/ q8 W, s$ F# c  f
    3 Z3 M; Y, E8 ~ 递归 vs 迭代
    0 v! D+ Y1 {5 P% l' s, K: M|          | 递归                          | 迭代               |
    . L9 @9 X& _0 Y( S0 k|----------|-----------------------------|------------------|* F" }4 r4 Y7 L+ H
    | 实现方式    | 函数自调用                        | 循环结构            |
      P" {- A7 p: _| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    8 Y" v& J. a0 i0 c( l4 K; G' n| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    4 t% u5 k2 ?) C8 j8 S| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |! M* R! i/ i) \+ Y4 N; P7 R

    5 E+ c6 N8 |: {( j( G 经典递归应用场景
    . I) H0 j! z% @  v+ _" j1. 文件系统遍历(目录树结构)2 w- j! o7 \5 a1 B9 x
    2. 快速排序/归并排序算法
    ( H. b$ T2 f! A9 h3. 汉诺塔问题
    4 Y+ G" R5 {8 h' ]4. 二叉树遍历(前序/中序/后序)9 N2 S$ p. ?) X  v* }5 `
    5. 生成所有可能的组合(回溯算法)( t7 K( T& u7 f' z
    % W$ U8 E, J/ d5 e3 D  |; n3 W
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    3 小时前
  • 签到天数: 3182 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    % g/ U: |7 Q! U+ z* \我推理机的核心算法应该是二叉树遍历的变种。( P/ a$ x" T/ [% _1 U
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:. X& N% `3 V$ ~
    Key Idea of Recursion
    , |* I% o* f- q$ L% b/ e6 k/ e; }
    1 X2 l- b) I8 H6 j7 n( q4 DA recursive function solves a problem by:" o6 ~7 i4 d2 S

    9 e8 K! I1 F/ }1 ~: v9 g% r    Breaking the problem into smaller instances of the same problem.+ T3 x  f8 }4 |1 r" J( S7 {5 ~
    * z  M- ^. f: j0 Y, j% j! h/ T
        Solving the smallest instance directly (base case).2 R2 m! @" H, t- f9 z1 t

    ! v$ |5 n! |' n0 W    Combining the results of smaller instances to solve the larger problem.
    - w2 ~9 B! }) ^0 t% i% ~: f, k/ ~1 q2 D5 G
    Components of a Recursive Function; g& }1 _  u* M
    & W& D5 O: b' _- i: V
        Base Case:8 _! U9 j+ ]2 j0 M

    & O$ @% V& L. R8 ?        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.1 W  i9 ^6 J0 j! ^  l/ q

    ! T: t( L( m0 e; H3 L3 [        It acts as the stopping condition to prevent infinite recursion.% @/ K$ h- \% C$ K$ Q6 X  w

    * E5 y$ O2 v, b8 h4 z* g        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.. q8 p3 U; a% H
    " K% |8 K$ o' X- U" k. M' b, C
        Recursive Case:* d0 a* o+ |) ]  E! b! E
    % w8 z* D* b, J$ Q
            This is where the function calls itself with a smaller or simpler version of the problem.% i. Z, `  k, w: x! @7 _2 b- s: r' h
    4 g' l$ T* E2 U& g9 z' Q
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    $ O3 B7 u' Z# z2 e/ m0 k$ ]. g8 p8 z/ B
    Example: Factorial Calculation  @( }- l% a9 L' w2 N9 n

    $ g* B2 w  n) U+ G2 VThe 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:
    $ w$ o$ {8 t+ P; V+ w0 l% R2 X
    + C# Z6 m* l+ h7 L    Base case: 0! = 1
    ( E  i3 b8 q  G6 w( d+ Z
    ' K) l1 @# i( Y    Recursive case: n! = n * (n-1)!) z3 g7 e, n. _( G" j
    ) ^) G: X# }0 ?
    Here’s how it looks in code (Python):
    7 v: D4 L: A7 e1 N# W* g1 Hpython
    : C; g7 B! S- j! i) A* Q* N- c1 _3 E0 f. }* c" \$ I% ^6 ?
    ' p, _- J) c5 q& W
    def factorial(n):
    6 q1 E% \' ?% V& Y% m, w! q$ u    # Base case
    ) R: ]$ H; o* i" j  P    if n == 0:
    2 v+ Y* O9 ~! h: Z+ @        return 1
    ' d* z1 [0 x. `$ l    # Recursive case
    0 }+ ^$ B5 q' @/ o    else:
    $ ~* h$ S3 V& e% B        return n * factorial(n - 1)0 L0 H' O- G! T$ l: X$ X5 e
    " z7 o9 h+ V5 O* {7 Z$ s2 Q" Y
    # Example usage4 G* r* ]7 L- Z  \* N
    print(factorial(5))  # Output: 120
    1 S: K* T/ v+ k
    0 L6 F( u. d3 c! C' ]3 F, SHow Recursion Works
    * O, I7 M$ i, |' X! P. B5 U/ m% e$ ?% w1 ], N, Q
        The function keeps calling itself with smaller inputs until it reaches the base case.
    + n% h5 p$ L7 O6 j! J, L9 p
    $ W' I3 P# h; C: D6 s/ O    Once the base case is reached, the function starts returning values back up the call stack.
    / T9 r! g3 l- L2 N" f* p5 M6 O
    + G& f& ?4 H2 \, u2 E3 Y    These returned values are combined to produce the final result.
    - E+ r8 N5 A: [7 \
    & r6 e- {( G5 p% K! o# Y5 |For factorial(5):
    $ d' D# D& o/ _9 z! G: h" J( V& B3 d9 ]: M5 F6 O" @8 e

    , V' t  ?$ ?" l6 {/ W9 h$ \factorial(5) = 5 * factorial(4), ?! A) [5 j/ O+ G" U
    factorial(4) = 4 * factorial(3)  z. z: c, e! h: i% I
    factorial(3) = 3 * factorial(2)+ K+ W% k3 Z( h; Q/ n7 M
    factorial(2) = 2 * factorial(1)
    $ Q6 Q( r2 g6 H8 }' j& xfactorial(1) = 1 * factorial(0)
    " G; E/ X! j6 F- C0 i7 E* Ffactorial(0) = 1  # Base case
    % g, |. D3 D7 t* O/ x  Q
    / G( m+ q" u9 M. g0 BThen, the results are combined:5 F* f* P, |3 [( U7 c4 Z' {
    # N/ s$ F1 C  U
    $ ^3 |8 Z8 t- z8 E5 v
    factorial(1) = 1 * 1 = 17 j% g- K  M* Z
    factorial(2) = 2 * 1 = 2
    - d6 g, r4 h1 [8 z  l) ifactorial(3) = 3 * 2 = 6. A7 i. w; d/ C. `4 w! |4 r( _# y
    factorial(4) = 4 * 6 = 24
    5 i' x! T9 @( _4 U& o; X0 d2 Ffactorial(5) = 5 * 24 = 120- r. e7 }: R- y/ l$ j1 X( t
    " I8 ?- H  P( n0 g4 r
    Advantages of Recursion: w7 y% f5 C0 j$ o
    6 P+ m4 Q3 G5 z) C- s6 ~
        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).
    7 T. b# D5 [( A+ m, c. J! P" X+ E! u. r& s+ k, m& Y: j
        Readability: Recursive code can be more readable and concise compared to iterative solutions.  x5 b6 ?1 h" ~6 _# F, j/ m# R6 z

    9 X* N8 N* C; \/ i- [& oDisadvantages of Recursion
    8 i4 j0 L' F5 S1 o
    0 C5 ~2 \# h4 ?8 h0 r* C; S% @+ D    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.7 S" D& a1 u$ v0 V/ C8 E! a; ~
    2 U$ A% e2 q5 J  E0 N
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    + Z3 `. P+ C& r. \- F) K. [4 O2 W; f7 l7 X2 b
    When to Use Recursion& q5 {1 ?/ R6 Z) E. _3 \% w

    / V6 _* E$ o9 b& c" V    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    # i5 \3 |+ r. n6 S4 R; a" o9 w: h) ]0 `3 ~4 L( V8 L5 k6 O8 ~2 X; g
        Problems with a clear base case and recursive case.( V- i& L7 A! l( ?9 `6 u

    - Q) X2 z1 ?! X0 d4 W" Y2 yExample: Fibonacci Sequence7 y5 N2 e& @' u# N8 H  E5 E
    ) V8 z/ p3 G9 p8 _$ c( W
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:$ K- ?' ]5 ]/ S$ I+ G5 c& F

    ) d" B% G0 q* p* M1 W    Base case: fib(0) = 0, fib(1) = 1
    ' b3 g) t* J# l
    9 O9 _* @1 q7 ^) D    Recursive case: fib(n) = fib(n-1) + fib(n-2)% w9 ]- z7 V" K- n

    $ i" M1 i6 F& j' K9 l1 epython6 z2 Z% w- U' R

    3 |+ i0 N+ H9 q/ U! |5 A8 Z- k5 i. \/ ^6 A2 n2 ]/ u  G
    def fibonacci(n):
      Z' f: X. X6 A5 Y! M8 `: W    # Base cases3 L  I& F7 Y& T+ V! T
        if n == 0:
    5 g9 H$ n) F% [3 B" e9 N/ ]5 }        return 06 `2 @+ r0 k, R% B
        elif n == 1:& @0 L& Z1 n0 {; w, ?5 I  [
            return 1
    9 M1 ^' a( m6 J0 Y* r! K3 p  u    # Recursive case2 N+ F3 a3 g% ^6 L3 P* p
        else:2 o3 S+ }% H, F# x6 j* L
            return fibonacci(n - 1) + fibonacci(n - 2)
    ( T# ?8 O/ ]+ j5 O, V% O$ U8 ~
    * A6 O% R2 P; R, D9 ^# Example usage
    9 [. p# a% ^0 M5 g2 ]  K$ oprint(fibonacci(6))  # Output: 8
    ) @. s6 G$ L8 v6 n; H% {$ E8 y: k. M- k5 j0 ^6 B! E* O/ z, h
    Tail Recursion* R1 ^- t5 {, c# U8 y

    / P7 |' X5 n  U5 V! m& JTail 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).# ^5 @  S) t" j

    + {. ^7 z7 }- R6 kIn 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-23 11:15 , Processed in 0.061289 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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