设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 4 D! L8 t: a* c1 `. o" v

    4 {3 Z0 |0 ?  X5 ]; ?2 v0 C解释的不错, {) R8 A8 ]' T/ M
    , ?4 A. m& g6 H% M; B# G
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ' B* ?; I& N! S  \5 k! }
    9 p/ }- R- G: `$ m6 D' S 关键要素0 ]" [) ?7 I& p( D9 H
    1. **基线条件(Base Case)**& a$ v1 X8 z5 v2 k: v% D  s
       - 递归终止的条件,防止无限循环  F$ X8 T6 n" _* \
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    / V% _( d$ m+ b5 g5 _' H
    1 F1 ~0 l; O) m# {/ {8 i: u. G2. **递归条件(Recursive Case)**
    2 h' S8 B. \; j6 B! l   - 将原问题分解为更小的子问题! f* ]: b/ V, `' {: T* f7 b6 _
       - 例如:n! = n × (n-1)!2 L7 E6 C/ C4 S9 O& G

    ) }+ f) t4 O. V& @9 ^7 I% n 经典示例:计算阶乘
      {5 O7 y7 o+ F% ], Ipython
    1 _6 \4 ~! Q, Q! z/ |def factorial(n):5 ]/ o: {9 m  ~! D) b
        if n == 0:        # 基线条件
    - ~$ X1 g# i  i& z" @+ O1 y6 }7 W        return 1* S6 h9 s% M& B) J" r
        else:             # 递归条件4 m$ ]4 Z. g$ d7 [! Q" }
            return n * factorial(n-1)7 s- C' X* e7 k- ~- A; I
    执行过程(以计算 3! 为例):
    " O" u6 o) O- F# B0 K$ }* _7 Jfactorial(3)9 K) Q& @/ M7 w" J+ y6 U
    3 * factorial(2)
    ! v( q1 |& H) O* J3 * (2 * factorial(1))
    : H1 G8 z- h5 f! S$ V  {  Z9 ^8 ?3 * (2 * (1 * factorial(0)))
    ) N! f% I: y$ f8 B3 * (2 * (1 * 1)) = 6
    & v% D+ p( j9 o4 a
    , K9 \; {" G6 q# c 递归思维要点* Z5 G7 v! a3 Z5 Q/ d% @
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    # D: E2 D# _( u1 j: B! E2. **栈结构**:每次调用都会创建新的栈帧(内存空间)! ^4 Z" K5 P* X4 q5 ^
    3. **递推过程**:不断向下分解问题(递)
    / a& X1 M+ G, f. s. f7 U4. **回溯过程**:组合子问题结果返回(归)
    6 \& k% I8 e4 s- z8 v' ?- f. B) ~2 r. i: x: s+ t3 d5 H  z
    注意事项4 r0 c! o+ C. Q
    必须要有终止条件1 a! Z1 V% u% e; Y3 @
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)- x' A0 n/ A4 X) _
    某些问题用递归更直观(如树遍历),但效率可能不如迭代- E4 h6 _  u5 L  L2 r+ u
    尾递归优化可以提升效率(但Python不支持). l. K3 W; x, A3 D) k& m
    + k7 l$ N9 @: O/ }* o
    递归 vs 迭代* }! i. V; {8 T
    |          | 递归                          | 迭代               |
    8 C% o  S) W' W+ J|----------|-----------------------------|------------------|% [7 j1 m3 }( C. a% }
    | 实现方式    | 函数自调用                        | 循环结构            |
    + _5 I% p* g" C3 k| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |4 Z4 }% l  e* j0 E! Q2 ~% i' }
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |6 W$ ^* C' i4 l- c3 d
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    5 n: v, r* [9 o  d, F! X( q$ w) @
    7 m6 c% S" z2 R* z3 O( k4 M2 h: s 经典递归应用场景
    2 |4 O/ c7 W; k- [9 }8 U3 ]1. 文件系统遍历(目录树结构)! c; m: l, Q6 ~: H( p1 A
    2. 快速排序/归并排序算法
    0 G, V( T8 R. t4 }: I$ M. F3. 汉诺塔问题6 ?. d' s/ B& N& d+ ^
    4. 二叉树遍历(前序/中序/后序)3 ^/ M+ k' y/ U
    5. 生成所有可能的组合(回溯算法)
    # z) T8 \1 q8 y8 s& P7 X; x) h# z3 d8 ^" T
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    8 小时前
  • 签到天数: 3129 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    " S/ m+ R$ k4 }2 J$ R# n9 E! \9 o我推理机的核心算法应该是二叉树遍历的变种。, F$ I( I  h: E- [; J% \; \( Q2 Y' 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:) l( s: U2 L, L: O
    Key Idea of Recursion
    ' C& H  w/ m' X+ Q$ x+ B9 b0 K# }) x" U
    A recursive function solves a problem by:4 {% g: g" R; f( ^( Y, Z

      P: Q0 u+ F" y    Breaking the problem into smaller instances of the same problem.
    , `8 U- w) F* |8 H! r1 H, s4 B( Q
        Solving the smallest instance directly (base case).& C' B6 r9 Q6 Z2 B% |6 O

    ! Z1 E) D$ \' t- \    Combining the results of smaller instances to solve the larger problem.
    : P0 X+ ^& g4 X+ A# X; K  K6 U1 x0 F0 |
    Components of a Recursive Function
    4 J) ~+ v+ e9 }# s  \# X  B5 D8 y& f3 A
        Base Case:# @$ i- |3 Q$ d8 q) Y$ b# g
    , q% p" O( |  G  o/ @# T
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.5 R/ g9 w& S. d6 `

    6 k; y+ |& D  {8 J        It acts as the stopping condition to prevent infinite recursion.
    - ^0 e( W; S- o0 t
    # U9 z( J; j+ t5 I2 n1 {        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    * }: J* u( Y4 |. k( ~( C% h+ M3 s
        Recursive Case:
    - U& ?, h8 j; G* P: q# U- |+ A5 H: E0 S  a
            This is where the function calls itself with a smaller or simpler version of the problem.
    ( a3 ?* o- b+ ^6 f7 y, \) Y1 P+ g/ g! H
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)./ n# w2 w; ^1 d

    5 X9 s- ]! Q) e# |, X3 p9 t* `6 LExample: Factorial Calculation1 |9 L& Q8 x6 V/ F9 T

    1 p5 j- E7 k+ p/ D, {# ZThe 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:. x, A/ a3 W+ J( v
    - n% C4 k2 @7 v3 I& n4 W) P
        Base case: 0! = 1
    3 `/ {! N; {+ h4 H
    # z! r8 A# m) K" z  x    Recursive case: n! = n * (n-1)!6 s% `) u0 _' r# g/ C
    6 E/ i8 H& B6 ~- }& z+ g1 T
    Here’s how it looks in code (Python):2 Y% u$ `& E: o$ l# W% q
    python+ l- O, o2 i9 K: h! A+ v
    ( D- t( c$ k) ~9 j8 j# f; T
    ' z# [& Y% b8 D9 a, }
    def factorial(n):
    2 X/ A: ]7 s+ Z' `* R1 X    # Base case5 `8 q/ b0 q4 B& T1 h6 q5 v  U
        if n == 0:6 c. Y' o3 Y2 o" S6 p7 ^9 p; M
            return 1: C* _' |% h: O1 `; s# d+ d
        # Recursive case& W7 ~4 ]. l2 a$ ~1 s; g
        else:
    3 k6 o3 I. A; w# I        return n * factorial(n - 1)
    ; s) _0 ?# j; [
    8 r( r3 A) N; ]6 S$ ~1 V5 `$ U9 x# Example usage
    $ o7 s) `% d" d8 v0 |9 o3 ?print(factorial(5))  # Output: 120
    1 \4 _/ ?3 L1 h# H3 p+ m% Z3 P( U/ |4 r# M0 \
    How Recursion Works
    * ]  N; x) A0 J1 @7 X! V
    * b& j2 e4 D0 Q* r    The function keeps calling itself with smaller inputs until it reaches the base case.
    : h/ Z( [6 g0 |
    . @4 x" p2 u# g8 R) ?+ }0 d) i    Once the base case is reached, the function starts returning values back up the call stack.
    9 [: R7 w7 d5 F  t) W& |
    8 D. C5 n" o7 b, G    These returned values are combined to produce the final result.! j9 Y1 i8 v9 s$ X
    3 L6 X& W: A; r) R
    For factorial(5):6 w) [& l$ s5 ]8 o2 ]
      e1 ]4 w% k1 L

    & B, j( Y; p3 D" ?# K4 }9 X4 qfactorial(5) = 5 * factorial(4)+ ^5 q" T+ g  [3 h! [3 V# l; @
    factorial(4) = 4 * factorial(3), b! m3 H7 z) {7 i7 k8 r. _2 e
    factorial(3) = 3 * factorial(2)' s( t. }  ~1 r
    factorial(2) = 2 * factorial(1)3 O0 h: k) R4 s4 q
    factorial(1) = 1 * factorial(0)
    # D9 {  j$ Y. A" M# G* t# k5 [factorial(0) = 1  # Base case* p+ L, l+ d$ J1 U
    0 l3 A4 c! Q& q! W9 {0 r
    Then, the results are combined:
    3 f# P* N3 y; Y7 Q) m/ P5 D& i3 B# x) s" e" w4 \% w; S  H

    5 X0 ]" t; G; C" t7 O. M# kfactorial(1) = 1 * 1 = 1
    " j! J( e# M7 T3 yfactorial(2) = 2 * 1 = 2" Y. y  |( }5 S0 x1 @' Y
    factorial(3) = 3 * 2 = 6
    & u( P( ^( M. ?; k1 ^+ G2 Y# }) [factorial(4) = 4 * 6 = 24% d' x: Q+ P4 p4 ^9 R0 S) C' N  n
    factorial(5) = 5 * 24 = 120
      g( U- R+ V8 e1 v) [0 y) }, B3 K8 z  Y( Q- g% G
    Advantages of Recursion" E9 W) Q: [7 Q0 N$ w4 x. |

    5 k  g& h& [6 T! d    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).
    , B. n0 R% R3 U0 e8 y* B; M
    0 v+ @8 b' S- g8 y# U7 a    Readability: Recursive code can be more readable and concise compared to iterative solutions.; Y  [8 y8 }1 e! {  h1 T8 x. K
    . _( f( Y1 {1 y( b: j4 [+ g
    Disadvantages of Recursion3 V: \" T5 ]& S+ U* M

    : F3 x2 a* b. x4 E1 s. V1 u    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.
    . L! p* P8 H+ W9 e. C4 z/ Z7 h; E& |) V7 V" X" a
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).7 y' Z, M4 E- D" h( \; C' @% K

    0 C/ F/ a2 `9 {; nWhen to Use Recursion' Z5 D7 m- N7 ]9 I
    $ N+ U0 Y2 w, }5 I) T( Y
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).5 {8 y4 a: @. i2 K+ M  }# e" S2 B

    ! m6 m# k) ^3 x$ D, o    Problems with a clear base case and recursive case.9 W: F+ b7 Q4 i! g. _3 E6 r
    + ?2 B: V0 _5 }" @
    Example: Fibonacci Sequence
    6 r& Q  z2 a% g; g1 @  q, ?( {. H& x; H. q  ^1 d: n0 c) Y
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:& _& s! X4 D7 P, L0 N* m

    6 |7 @0 y% W7 r- h; [7 U& F    Base case: fib(0) = 0, fib(1) = 1
    3 Y! A# L1 T8 u. d4 h6 C% g3 E* s% C. G0 H8 X) S  m
        Recursive case: fib(n) = fib(n-1) + fib(n-2)# [1 I7 F! S5 i( w# L) }% ]% V
    , c( I5 t" C8 d
    python
    ' e# |$ {: E. S- ~& D% V, ^! u/ ~0 W9 w1 X  z( h

    1 u% b$ r& x; f0 U: Z5 Pdef fibonacci(n):
      G$ {, J" T* U4 s) K    # Base cases' K& }4 O- G2 D% t
        if n == 0:
    , m! l. R& l) J- X7 L5 k        return 01 V) d3 y4 c( g; J! n7 ^
        elif n == 1:
    / L/ q8 ~: j! m4 Y" H3 ]) R  m7 C        return 17 J2 Q9 p. n0 R
        # Recursive case; y- w: d3 r8 u$ `7 I
        else:
    - K6 F2 q; U9 A4 k* E3 \        return fibonacci(n - 1) + fibonacci(n - 2)
    + z& y+ v6 b  A4 G0 x5 N  J9 g+ G% K3 ?) g# d
    # Example usage
    8 t2 K/ Y4 z- s+ o+ t$ Y% tprint(fibonacci(6))  # Output: 8
    9 _! r6 g0 K# p! J& I7 o7 }, q0 B7 w9 o/ L) ?9 W* D+ f
    Tail Recursion
    ' b* \, q4 @! U! J0 Q1 ^$ |/ ^; M) W' S" u2 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).
    ' b% G( q+ L4 G
    ' p/ d% k2 z  X: J. y2 GIn 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-27 15:38 , Processed in 0.029125 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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