设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    & L5 W, N  k+ e8 E+ F3 X
    ; y$ h0 o* n% ~+ @/ S7 D解释的不错
    0 q# p( O0 j1 [6 d, m, @1 q
    9 B9 l& q  }9 U2 b递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    - E! v' _! `. c+ u* b+ ^+ e0 q/ [7 {! y9 e$ }: K# ]0 f
    关键要素* m% U: t+ }$ n% B9 @$ o5 S! A8 W
    1. **基线条件(Base Case)**. n# y& L) V0 o( q8 E2 {8 r) p1 F
       - 递归终止的条件,防止无限循环
    " G3 r, V1 ~) ~0 L/ c) _( `   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    3 z$ L/ u2 n% @0 v
    6 _8 _/ K: }+ l( G& f, a2. **递归条件(Recursive Case)**
    . [* z  Q. c( L5 L, b! q8 F& r   - 将原问题分解为更小的子问题
    ; z4 W: c- F2 T: W+ U! l   - 例如:n! = n × (n-1)!) q: i  g  V! u; d% o

    / a5 G4 M6 u5 D; ` 经典示例:计算阶乘* m0 {, F, v1 G3 p8 l1 O  P- \
    python
    3 \' m7 w3 U3 j5 j/ _def factorial(n):
    1 [7 t7 N$ p9 c    if n == 0:        # 基线条件# W8 W, s8 |& Z3 B
            return 16 K% U; R% w5 a. R/ @4 `
        else:             # 递归条件: ^+ |7 _! T+ Y# D
            return n * factorial(n-1)3 I3 W% E( ]( N9 m0 B  W
    执行过程(以计算 3! 为例):0 }* V* ]6 F* ]* I5 @. u3 k
    factorial(3)
    5 D" y3 ^6 z' ?" p0 F9 y3 * factorial(2)
    ; b( ~8 I* ~* H0 q3 * (2 * factorial(1))/ c, }! J+ b+ z1 t3 s
    3 * (2 * (1 * factorial(0)))5 _( ]. C0 s* h
    3 * (2 * (1 * 1)) = 6
    0 L, Y, e8 P" B2 w5 T3 J' ?3 J* q+ }( y
    递归思维要点" s0 F9 G6 X* v' f* E0 I
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ) P2 P  R3 P1 ?' x2. **栈结构**:每次调用都会创建新的栈帧(内存空间)5 d" v- J6 ^. h8 I4 R6 l6 W# B& `
    3. **递推过程**:不断向下分解问题(递)) @8 _/ ]$ O8 l$ Z: k. \! K
    4. **回溯过程**:组合子问题结果返回(归)  y: c- h% V( F0 l3 |" J9 k
    + i( f* \' g. U) U) [
    注意事项" m+ G2 B; D* f
    必须要有终止条件
    + a3 @* r3 r; e1 G8 K递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    # @/ z) f7 ~$ t& I某些问题用递归更直观(如树遍历),但效率可能不如迭代
    1 K( p$ z: r/ ]- ?; T- ?6 n尾递归优化可以提升效率(但Python不支持)
    & q9 w* n3 I3 y% W' \
      X& s) ~0 H" j7 I6 c 递归 vs 迭代
    % Z' j! z% Q7 V9 c, \|          | 递归                          | 迭代               |
    6 |3 D! G, O; E( B1 _4 c6 n! e7 x|----------|-----------------------------|------------------|
      R) ?8 }& I+ w, H. S! }| 实现方式    | 函数自调用                        | 循环结构            |
    ! C/ M% x* D2 h* [, t| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |6 x7 o; V; Q1 K: {
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |1 i* Q4 f6 r/ l$ \* `9 g1 b- B* H# }
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    2 X# M! `2 Z) e* I7 N, M
    5 {  k$ x0 h8 J 经典递归应用场景3 h. o" }  A; B3 p0 y
    1. 文件系统遍历(目录树结构)
    " R3 a/ `& k9 {# M# U2. 快速排序/归并排序算法  {& r% ~, f9 N# P
    3. 汉诺塔问题& |: [. o) s' @1 ]
    4. 二叉树遍历(前序/中序/后序)
    ; X6 a+ d% [6 \7 P8 ^/ S. l; J+ a5. 生成所有可能的组合(回溯算法)6 B1 N( C5 ^: j4 G
    % Y+ G" d. f0 k$ |+ P; h# J4 S
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    13 小时前
  • 签到天数: 3189 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ( b3 X+ W2 z" f5 k7 d我推理机的核心算法应该是二叉树遍历的变种。  h3 L4 }: U; m4 t' h  r- V  Z) P, ~
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    4 F# n! M( o- H! n" ~# oKey Idea of Recursion! M" N+ I* y+ ]5 |9 J8 U1 F
    " l8 t: V. p9 p6 R6 i2 Y
    A recursive function solves a problem by:& i/ O. L* s6 N) ?
    % ~# W* l8 I+ L; s! L
        Breaking the problem into smaller instances of the same problem.; J' X- P9 u9 @( c. N
    & ^. L  y4 N5 s) Q3 O
        Solving the smallest instance directly (base case).
    / L; ~; s' T8 c1 |. X, {1 T* b2 D2 Q1 L$ Q
        Combining the results of smaller instances to solve the larger problem., ?* i9 p7 A6 ^

    . a$ H8 A: T! P" m* B2 L5 SComponents of a Recursive Function
    # M* q4 a, T$ F5 b( C6 b* U) R
    % J  M0 u; O8 \! i3 O- D! _" j& k0 C    Base Case:) l' ~" N- Y: \" G
    7 t, |- p8 g6 ]# b  Q
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.; W9 V$ E( u; w& J( }7 \  T
    $ P) {1 L# {) _) H$ X2 o
            It acts as the stopping condition to prevent infinite recursion.  g+ X0 w1 r  P9 {0 X

    - l6 Y4 ]: b7 S  ?# `' S        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    7 ?5 v  [6 o& f# Q* Z/ Z
      r; E& }! ~8 Y    Recursive Case:
    4 B9 v% T  t& k2 F. [5 ^- d0 d
    0 K4 F; S9 I+ n- a$ ^        This is where the function calls itself with a smaller or simpler version of the problem.1 Z1 @3 R/ Z; O4 w$ ~( F

    - ~. c: i" _5 w( e4 A% [! N        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    + Z& y( [( s# s2 q
    * I( `# z8 j9 w4 r  QExample: Factorial Calculation
    9 f3 \' X" @; D" l* p, {
    0 I0 X: i) i' [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 i" x, T- M1 b, [- X! Z- b
    4 ?% K6 t, r  R" R+ o
        Base case: 0! = 1" Q/ k: q0 S$ `# J

    ( I* @" a7 O( C/ p  @9 z; G6 |- Z+ |    Recursive case: n! = n * (n-1)!- F' W" z- ~9 o

    ) N7 {% d6 Q6 ~6 G0 T: h' Y6 b! ZHere’s how it looks in code (Python):
    5 S/ }6 b2 h( x; f! z. Y* Y  v/ Mpython5 a' k+ d: ?1 ~' ?8 m. I

    1 Y, Q8 v0 u  Z' }5 V5 Y" j
    ' u: Y1 j% F; Sdef factorial(n):
    0 b! O% K" P% `8 X& M4 U    # Base case
    & s% \$ O/ A5 S7 `! N    if n == 0:
    * t) f# U8 A" m9 @! m% |        return 1
    ( j# S: g* A$ q. v    # Recursive case; z0 |' e/ a3 e
        else:8 \6 U" ^9 J/ v5 M) L
            return n * factorial(n - 1)) _6 w; M, C1 j( S

    , D+ t4 r# i) r$ B* c0 G) ?7 o, [# Example usage, q1 M+ I( s& P* q6 Z- Y& v. `
    print(factorial(5))  # Output: 120
    . K: `1 B2 m$ H  ]" x6 b( G. O. h( j8 y. I1 Z
    How Recursion Works
    ' m' i1 U* \& i. }( f1 x
    6 F0 _4 g) F- @+ m    The function keeps calling itself with smaller inputs until it reaches the base case.
    8 y- i( w) b  p  a' G5 ?  X* N7 B( A! w' T& X
        Once the base case is reached, the function starts returning values back up the call stack.0 X% [2 L, m) D8 S

    4 z) L7 v" V# b, d    These returned values are combined to produce the final result.
    5 k9 j+ S/ [! T, Y0 ?' S; m+ K. {. P+ E
    For factorial(5):
    $ L( `( z6 T* [( x7 a7 Q8 G$ l. L( R; Z4 u0 |4 j: c

    ! P& j: K. L: ?' T2 _9 mfactorial(5) = 5 * factorial(4)
    . @8 o3 \* c' ^; X5 j* \  Hfactorial(4) = 4 * factorial(3)' y" N$ M" x# E' I1 p, E) i/ y
    factorial(3) = 3 * factorial(2)
    3 c" M* I1 U, jfactorial(2) = 2 * factorial(1)9 F, s9 s& l- W% ?+ L& D; V% |) k
    factorial(1) = 1 * factorial(0)1 x' n3 a* m! z& ^) {0 o
    factorial(0) = 1  # Base case
    " v! t1 R7 B1 l# T4 b' I* J' ^0 v; X* J; Z
    Then, the results are combined:
    8 Z# f* S4 n, U2 z0 U9 V: |7 N2 F) H) S2 k2 B9 d$ M! ]
    3 \  g6 ^9 J/ _, I( Q, c
    factorial(1) = 1 * 1 = 1' p: L. l6 M/ Y5 V& L
    factorial(2) = 2 * 1 = 2% q3 G- t2 i$ ], x6 b& Q
    factorial(3) = 3 * 2 = 6
    # k/ Y4 Q* J( R+ U& jfactorial(4) = 4 * 6 = 24
      U; Q$ h5 {& b4 Efactorial(5) = 5 * 24 = 120- a1 w3 k" L) Y( X9 u$ l

    8 T% {1 H! `8 K; fAdvantages of Recursion7 Y/ D  {4 J% V3 s0 x9 h' g( i2 ?
      g! ]/ x) r4 _5 y9 }6 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).
    # i, s7 B3 g( {9 f* T5 U# b
    % U$ x) G$ k% L9 I    Readability: Recursive code can be more readable and concise compared to iterative solutions.6 j2 n( m1 B" s* |

    & J0 ?0 j6 `1 K9 H4 _1 GDisadvantages of Recursion6 x6 L; `6 d: _9 C& U: B
    & ?7 l5 h/ {6 Z: s- v( M- P
        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.+ ~' Z5 l; }3 h6 F+ Y+ J

    7 j4 O, f9 A* @" F) [    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).% j" I8 ^* H0 A1 f- D
    # R4 a0 \) j1 G( H8 `
    When to Use Recursion
    4 E9 X" b- y* s* @
    " E8 x; ~: j+ F    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).7 j2 z, m1 F/ m/ s+ f! r
    . X- F, t, L4 f( b% ~6 V  t9 I
        Problems with a clear base case and recursive case.& P0 Z" Y% p0 H
    " {6 [( ]; p8 u1 o
    Example: Fibonacci Sequence! F" a9 z6 ~' C! C- F4 s. U

    , R8 S4 L) s5 h8 VThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:1 J8 ?9 U! }* `3 Y4 h! p8 \, v4 l1 C
    & B; k( k) X, b( {# v$ _
        Base case: fib(0) = 0, fib(1) = 1
    - |" {; y6 o0 D9 o+ b" H6 A
    " T! j5 @. g: f8 _, A4 Z. L    Recursive case: fib(n) = fib(n-1) + fib(n-2)4 q1 q! P3 \$ }( {, d

    5 G# D6 z( c, Y% b9 tpython! P: u$ i5 c6 m/ c# h0 Z
    , _' c( Q; y% R" w! l$ ~

    . _- @6 S( d  a$ b/ ~- Sdef fibonacci(n):' Q2 V+ ^, J0 }/ A% h# b0 X
        # Base cases
    " Q$ |* k' W" e( D; Z! c    if n == 0:
    - N2 ]- b1 g2 W8 x( k1 o( e4 Q, ?/ i        return 0+ H4 @6 g  a3 J1 G/ i" o* S
        elif n == 1:  F  P! \- G5 H) y2 g9 h5 W
            return 1& h; Z* D+ E, @% p2 h
        # Recursive case2 |: ~" A6 E) [" K% ?$ c
        else:/ b' Q# Q7 v. W0 H& d0 w4 g3 A! t
            return fibonacci(n - 1) + fibonacci(n - 2)
    . x/ d$ i: Y; ?) q8 E
    $ S5 ~# g  v9 n# y. E8 f# Example usage
    . G/ Z& o) p3 O$ wprint(fibonacci(6))  # Output: 8
      E2 l; q8 j8 [: j6 \; s- u" [# m4 ]9 i2 @
    Tail Recursion' b+ X6 z4 d3 {8 [+ m! e1 G  x; F

    ! T6 c2 Z5 }. pTail 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).- y* p: [3 J: h& x

    9 D& q" U; E+ u$ [' \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, 2026-3-2 21:06 , Processed in 0.058396 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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