设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    - X6 r. G/ ]) _+ R! `6 ~% p  A$ R8 ~
    解释的不错$ b% D% f5 D( q+ i' H' \
    ; Z% |- _( U& c, _" Q' q& t
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。9 N- M7 O  W2 @* j
    7 `# u. L. N8 J- R& O6 L$ b: T0 @6 K
    关键要素
    - V# b" K" D. r) i3 W& g% c8 w1. **基线条件(Base Case)**: D. V9 I4 K, ^3 `( Q3 }' l
       - 递归终止的条件,防止无限循环
    6 w  C% h- Q' l5 p) v   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1! Y* Z9 t0 c/ E
    8 i3 G, P4 B& K' [5 ]/ J
    2. **递归条件(Recursive Case)**
    8 `# n, f6 p* I1 {0 H& }   - 将原问题分解为更小的子问题
    ) N+ F6 _1 A( x# d( D* C% A   - 例如:n! = n × (n-1)!  h' M1 G5 t9 Y! G# j

    5 v3 _' x- v. m) u8 b2 y 经典示例:计算阶乘
    5 C4 i' C  [- E$ U+ N. x: spython# |9 G$ U. V" R) O0 x
    def factorial(n):9 s: K* h( j: X# M1 @5 I! Z
        if n == 0:        # 基线条件6 |! ~) L# h9 r/ p  k3 |  Q
            return 1: \. A1 N1 p: A0 N7 U
        else:             # 递归条件9 w  h4 i+ U( @* T' P
            return n * factorial(n-1)0 X  S' Q6 [  S% O5 E- [( }5 C" F
    执行过程(以计算 3! 为例):* _  F4 l- ^3 E7 D$ V
    factorial(3): y+ x7 @2 b5 s
    3 * factorial(2)
    " P- ]: p  B2 s% Y3 * (2 * factorial(1))
    & ~$ ^3 B/ g: G1 s! x3 * (2 * (1 * factorial(0)))
    ) R$ c( d# |9 ?9 Y3 * (2 * (1 * 1)) = 6# |% G+ m$ _( o0 p$ @2 m5 C5 @
    3 @5 g3 ], q( p9 }
    递归思维要点2 \0 U" Y* @* A1 T& Y5 I; b
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    / k0 e3 |+ m* e9 V: ~" y6 H, L) v2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    + Y8 [+ p: D. r  w" v! J3. **递推过程**:不断向下分解问题(递)! o* q. d& O# [; X6 {
    4. **回溯过程**:组合子问题结果返回(归)! W0 B# P# V- v+ I

    2 V$ u1 }6 v# l* t* J 注意事项
    . ]' _& \" _& _: ]必须要有终止条件
    7 V% |- u$ k1 \2 z2 [- I递归深度过大可能导致栈溢出(Python默认递归深度约1000层)0 t6 S7 r: F" \% J" F- ?/ A
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    4 W, {  ?" M# ?) u尾递归优化可以提升效率(但Python不支持)
    3 n; `8 w6 A9 F5 X/ h; G
    9 T4 e/ M# Q( R; e 递归 vs 迭代
    + W" Q4 I, P9 |% K" Z|          | 递归                          | 迭代               |1 p! N' w. I4 B
    |----------|-----------------------------|------------------|; y! u$ Z# U  ?6 r' Y0 s  d1 L
    | 实现方式    | 函数自调用                        | 循环结构            |1 M0 I3 N" {# |
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |0 w* n8 x7 U( k, S# `+ y, p1 z9 W
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    * J- J2 d5 f9 K: z$ B| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |7 p- R" \/ f% _3 x! }1 z: [

    7 S, W! @0 D, I3 Q. t" H. c 经典递归应用场景
    9 }" |* R& a8 |8 w7 ^1 T1 F1. 文件系统遍历(目录树结构); T. k: A7 C3 `3 x2 t- [8 Y! O
    2. 快速排序/归并排序算法
    / e% i! ~+ @( A3 h3. 汉诺塔问题
    9 U7 D0 B1 i- ?9 M3 r2 X1 }4. 二叉树遍历(前序/中序/后序)! W7 S! y9 Z7 m; o, K" V) j
    5. 生成所有可能的组合(回溯算法)
    / i$ g& y! g0 c- s4 v
    $ ^+ }; u  A' ^6 F' L7 B' x试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    6 小时前
  • 签到天数: 3108 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    9 {7 s# @% I0 c$ y6 ^; w0 X我推理机的核心算法应该是二叉树遍历的变种。2 K, ?- W7 {  y# T( B
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    . _  y/ o0 h" GKey Idea of Recursion8 k# j# X7 l& o; F
    7 O& S  C9 Y% z) I8 `6 p: t
    A recursive function solves a problem by:0 |- N$ [! v' y6 `; T3 J# s6 P, Z
    : T% Y* n# ]) e4 b' F" k! D2 g( p
        Breaking the problem into smaller instances of the same problem.
      p: r$ [' s, [2 Y8 |8 C, f
    $ j4 X5 f8 k* y( }    Solving the smallest instance directly (base case).
    & E+ _8 B5 Y5 F3 p: }: v
    0 G: }2 S& q1 q# ^" k    Combining the results of smaller instances to solve the larger problem./ ^! g; c' G- Z. c% b

    + q' U% F! L% `Components of a Recursive Function
      L" A, Q" }) j; \( i3 Q7 o) r, h3 \+ d' k3 V4 ?# t
        Base Case:
    3 c9 U- m1 ~; n3 e2 S# _; C- e3 W# N3 }" D/ ^9 c
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.4 U; o3 M; U- L' u# \* |

    ' E4 b) T5 g0 L% T# ?+ ~( J        It acts as the stopping condition to prevent infinite recursion.( U  Z( O1 _$ ^2 q

    / k6 \! b! y2 A+ r+ \* q        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ! I* T# p, A  U2 V" l
    . F0 h5 B/ R' k) Z    Recursive Case:
    8 D( [( |+ U  V* q' i, Q' l# l3 k; Z: G# D0 w7 H1 M" J& T
            This is where the function calls itself with a smaller or simpler version of the problem.5 T& j% r4 s! s3 P) P

    - o4 x9 K0 I: L( [8 t        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).% h, X1 N5 p- Q: i' h& J: P

    ( @5 c$ {' o& y# q5 uExample: Factorial Calculation* c- I  ~* P# I% d# j

    * z5 U7 e" G: ~/ b5 ^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:. @0 |/ S+ Q3 H# }0 G( V

    $ k6 p: G+ g: R, I$ r5 K& z8 U9 b" K    Base case: 0! = 1
    1 L, c" u2 `8 I5 S6 C
    * z1 s4 r, O) I: P& {* u, ~* I    Recursive case: n! = n * (n-1)!  R' M2 ^' [  Z; A

    ; _6 T0 t$ _  U4 PHere’s how it looks in code (Python):
    & k9 F- m' n2 e4 Gpython; F3 e1 c# F9 Z* H. {

    $ J; |9 Z8 L/ w* {3 p: X7 u5 y
    6 S2 n2 I* j$ Y  X8 F1 Ldef factorial(n):
    ! P! a9 w+ R! ~# K; c! r, w3 R    # Base case* u* e1 x" w5 y: f9 r9 @+ z" d, e) b
        if n == 0:& `5 S  c; d5 n, O4 K9 s
            return 17 s9 ?3 W- F* X/ m
        # Recursive case( o$ K* v9 O7 g! P( e* t
        else:0 K2 l- ^: Q) }9 W7 ^1 L' s
            return n * factorial(n - 1)
    , [/ z1 [2 D# P/ p' P" C/ |: z. s
    5 n9 r0 Z, n/ s/ F% M5 G5 y# Example usage" c0 H- T; ]/ B* Z
    print(factorial(5))  # Output: 120
    $ A3 R% f" ^) B9 @
    3 d2 N+ D! }2 a; R6 ?How Recursion Works1 w  d& y8 p8 f$ O
    1 \/ Q" j4 A7 j/ \7 L  T. R1 b9 N- L/ Y
        The function keeps calling itself with smaller inputs until it reaches the base case.
    0 {: f3 C" Y/ b* P+ {! g5 N: _7 T; N9 m9 [$ v
        Once the base case is reached, the function starts returning values back up the call stack./ J) S8 r( {; W' N; G
    $ Y* i9 O8 A9 M  D8 d
        These returned values are combined to produce the final result.
    3 V* W+ u% M- d" [& d6 {8 T2 M: Q5 Z
    For factorial(5):/ X- ?" g- U+ x, [& q% V$ H
    8 E& e# @* u! |& h& z+ D# O- ?

    6 y' Q, L" L, u7 g% p) _& Nfactorial(5) = 5 * factorial(4)2 u7 L1 |+ t3 Q2 V
    factorial(4) = 4 * factorial(3)% S- }7 A% \2 u$ s. I0 Y
    factorial(3) = 3 * factorial(2)& o. S% k: |/ T: t
    factorial(2) = 2 * factorial(1). B3 `/ X* L6 x
    factorial(1) = 1 * factorial(0)
    " J' ^% K4 W( ?factorial(0) = 1  # Base case5 s" g6 m" {" X; |# ], o/ W. ?
    ; {$ Q% n) V9 a( `2 B
    Then, the results are combined:+ }$ `4 s& E3 U. @8 ~5 O$ P
    ; |9 T% Z) P5 H: Y0 [

    * P+ u" R% S- v! K& W# yfactorial(1) = 1 * 1 = 1" d- n8 v+ N9 q; {
    factorial(2) = 2 * 1 = 2/ U* M  J5 A! p2 {' x8 j( S" t
    factorial(3) = 3 * 2 = 6
    : D7 Y7 E5 g8 X' F' H1 `) f  Kfactorial(4) = 4 * 6 = 24& L9 [! }; S1 U- r( `4 _& s% ~$ B
    factorial(5) = 5 * 24 = 120
    0 {" q# T5 [( B$ B- F: H  n1 i* c' \! z0 v
    Advantages of Recursion( C2 h# s4 Z6 t
    1 u: d, u/ @( v, M& W
        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).
    ! l" o6 Y9 O) \! \# n
    ' C" t# a/ ]' H0 l2 [* ~    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    # M- m% ~1 X8 J: w$ q/ l- h- o/ \" T- r& ^7 f
    Disadvantages of Recursion
    , `) G8 \0 N: c8 ?+ V' i
    - J# V5 k9 B) w9 x6 r3 O! |    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.
    ; o' o6 N9 g% y; ^& `5 z+ L1 D: k3 J; M* O# B, m0 b# Z: E) x, }
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization)." @- |* k* L, F2 L0 ~! d

    # r% r8 b7 {4 t' P( t! m" R9 H. H0 _When to Use Recursion
    0 B7 F, I1 D* r; p4 U( }. _
    ! F$ d; j1 p  B  Q0 u& \; a& i    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    / E5 }) E! q7 D  I+ V
    & u' W4 y& m! f    Problems with a clear base case and recursive case.- q; D- |  \. E9 v& [) V

    7 r! L* {0 _! q  a7 w+ MExample: Fibonacci Sequence$ n8 I5 g0 S( d
    % z& i) f: r' b
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:6 I: q# A& k! r# b- g
    + B0 Q* n8 C$ s/ t9 f1 i+ _
        Base case: fib(0) = 0, fib(1) = 1. T# ~# O  r$ H+ n. l
    5 M- b' s* ?- ^, s: N1 q
        Recursive case: fib(n) = fib(n-1) + fib(n-2)8 K9 b4 O2 k) |! J  K3 L

    : f' @' o1 K; G& _+ d# I" Dpython3 ?3 }8 L( b9 C! _# }8 e6 ?; g; i% x

    / r) ]0 l/ I# s9 O, x* x% I8 y$ J  s+ @( R" J, \8 i
    def fibonacci(n):: r9 \! i: y" o' T3 Q3 Y
        # Base cases( J, {5 ~  E' y
        if n == 0:. t1 c/ q/ D, ^
            return 0: l: `' H& v3 X6 S: M) U
        elif n == 1:
    % c% W) E9 L( N: e3 X        return 1+ `9 I/ A& f: X4 L  B* ]4 b
        # Recursive case6 w5 k  f  v( Q: f" w9 _$ T
        else:
    8 F6 r6 ]: e, w4 h        return fibonacci(n - 1) + fibonacci(n - 2)
    8 u  K- }- w9 x' D
    9 k3 p8 \9 u0 k# Example usage
    # g# t5 [. t$ Aprint(fibonacci(6))  # Output: 8
    7 ^5 A8 a. X. S
    9 s+ w9 T  X$ Y4 j" ]6 WTail Recursion
    , O- l6 x  h0 s8 T; H, O
    8 [0 Y  x, O" bTail 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).: ^& l" K0 u% _) f4 ^0 ^
    ! `& f; R! n/ s2 Q) c5 t
    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, 2025-12-4 17:50 , Processed in 0.030351 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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