设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ) m% D3 t: i( c$ m( U3 Z
    ( @* h% `* z) [# a, T( n& e7 U解释的不错
    5 p6 A# i' Z: U
    / T4 g8 |9 P( X) I8 R递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。5 s5 W% k( m. ^- H0 y
    " f7 T) I, p* Q
    关键要素
    8 t% i2 j; M: G; {2 g" o9 x) G1. **基线条件(Base Case)**
    1 t7 D' H6 w5 \! n   - 递归终止的条件,防止无限循环5 f' y- ], o3 m) V% o# D
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 19 g' |  E+ F+ L, M2 k, }- ?
    4 Y/ m% _* j& f& r
    2. **递归条件(Recursive Case)**; R  W9 Z; q1 ?! {' C3 W
       - 将原问题分解为更小的子问题
    ) h' i; J) N  L, {3 s) a, e   - 例如:n! = n × (n-1)!
    0 g4 y: {! h3 V) ]- T; Z1 C+ k5 F% W' F
    经典示例:计算阶乘
    & S! e7 d) I$ ypython
    4 g- ~/ k, Y! R& @0 i$ hdef factorial(n):+ L. N, X3 {9 U0 V/ V
        if n == 0:        # 基线条件
    7 X9 [( ]3 Z1 Z& g$ [4 m        return 1! J+ b" L, _( A# v: G
        else:             # 递归条件
    1 G0 b" e$ H6 W! k* X) J3 A5 b        return n * factorial(n-1)
    6 C7 a$ R! }7 l( d' n. R$ w执行过程(以计算 3! 为例):3 g% S8 ~8 W/ ^
    factorial(3)
    ! K7 G3 c/ Y/ H3 * factorial(2)! G4 m( d! Y8 I: \  x
    3 * (2 * factorial(1))
    : X6 c4 ?' |6 C0 z( O% l& m! Q3 * (2 * (1 * factorial(0)))  O( Z# r' I, j  w! b" g8 V0 @
    3 * (2 * (1 * 1)) = 6+ X+ N; q9 U5 v% u$ z

    3 D+ A& ~: u6 Y/ \9 a4 @ 递归思维要点
    ; f+ Z' [8 \* o* l: [+ H1. **信任递归**:假设子问题已经解决,专注当前层逻辑& F1 m2 Q$ z0 l8 U) i
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    0 p- Q' h: m3 g; Q- D* ]3. **递推过程**:不断向下分解问题(递)/ ~0 k& U: V% \
    4. **回溯过程**:组合子问题结果返回(归)! ]$ @  ~) |- c, Q- T0 _! ]

    , _7 n0 c! L4 D9 ?( _- } 注意事项
    ( \$ X$ {/ Q# p, j) `必须要有终止条件
    . t4 X& X& u3 R' Z6 [, R递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    5 m, M7 b+ K8 Y  @某些问题用递归更直观(如树遍历),但效率可能不如迭代
    + b, U- m1 T' H, o" i尾递归优化可以提升效率(但Python不支持)
    & y. l% h4 Z6 I8 }
    , {8 @, t" X0 q7 @ 递归 vs 迭代
    ( O4 Z+ n" o; C/ K|          | 递归                          | 迭代               |
    3 ]& x2 Y) i2 `2 G7 ^|----------|-----------------------------|------------------|  V$ I, [: @! B* F9 p7 S/ K- u
    | 实现方式    | 函数自调用                        | 循环结构            |6 p; d" @$ f: c; w) }
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |# z" u9 z' ^3 w8 R: b$ T" C
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    7 j0 s+ m; L& ~| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |8 m1 W- a* O% U# W

    # u( D% U* \* W& U- A 经典递归应用场景4 L! f0 v- a3 A5 d, w3 @
    1. 文件系统遍历(目录树结构)' k' X7 G! Z6 w4 p7 M: N# i/ J
    2. 快速排序/归并排序算法
    2 q6 Q) A! w9 p: j# P5 |3. 汉诺塔问题# }- R0 }0 b, Y$ p
    4. 二叉树遍历(前序/中序/后序)
    1 q2 o) L& n, n/ Z- O# f" k5. 生成所有可能的组合(回溯算法): E8 d: |+ \# J% g) ^; X6 n

    4 X+ @' q. D" h! ]" X, w& R4 Z! s试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    昨天 07:20
  • 签到天数: 3144 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,& ^+ c, n4 v3 Q2 o
    我推理机的核心算法应该是二叉树遍历的变种。* {% `3 s9 d% a% b8 f
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:+ W+ P* {) }+ r1 h
    Key Idea of Recursion
    - }" a+ R# d: {6 g3 y3 y$ P
    : P  x6 F; J- h" b) R( ]( I1 UA recursive function solves a problem by:8 g2 G; q! Q) u5 K7 U

    6 ]$ p- ~8 ?- F8 C0 X; k$ ]    Breaking the problem into smaller instances of the same problem.3 A; H+ V( G( p5 G
    4 Q. \- Z: c1 \( Y, O7 F
        Solving the smallest instance directly (base case).! D1 c/ {- K, l5 t5 u/ x
    $ M) g& A& d1 V- ^  I, K2 Z
        Combining the results of smaller instances to solve the larger problem.0 u% o0 _1 c1 K

    ; {+ C4 W; N" F) F4 [; ~2 iComponents of a Recursive Function
    2 Y& ?1 [  i& E7 ^) V2 y: ]) v" Z7 P3 f# K3 D
        Base Case:( G. W" O, _) M+ y$ I

    ( i& q3 @+ I& V' O        This is the simplest, smallest instance of the problem that can be solved directly without further recursion./ i4 a* Q: w1 l) i. ^

    " i- p4 ^0 ]& b. [  Q4 O' R/ F        It acts as the stopping condition to prevent infinite recursion.
    ! g- F" y" R% u* t9 ~& n! |! O$ H# f" x% {8 ]/ z
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    4 Z% k# w1 E& G7 B
    ) ]: y# R9 @' n, m    Recursive Case:
    8 N2 v$ W" [$ \6 j5 R' K& F% R; V# S% [
    # I- m/ P1 Z7 p. R        This is where the function calls itself with a smaller or simpler version of the problem.8 s$ R  q/ ?& e% L, b! |3 A! m
    + q3 y% ?! W, f4 S2 H7 Y
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    3 D8 a6 Q) Q, `0 C+ G. S8 h$ b. i5 N! X8 m1 F' C7 I. V. Q7 `
    Example: Factorial Calculation
    / i, r; k& w+ C0 y. k& K6 h; l/ }/ S& l; |
    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:& L$ @; C/ B6 l' i& S+ z# t

    ! L" X7 w. B8 w6 O1 S9 e0 B% E$ B    Base case: 0! = 1
    6 d2 ?! B. d5 s6 q/ H  J# f. Z; [( k1 y* G  o
        Recursive case: n! = n * (n-1)!# y& ?% c+ T7 b! ?0 }( Z' Z
      b/ C3 G3 w& g7 U1 ?! r
    Here’s how it looks in code (Python):/ @9 p3 q  S9 C! S" B- M
    python3 _% k, T6 _$ h5 B7 y, ^4 x

    % q: H! [/ Z' W4 x5 v+ h% o
    % J. Y% M. j$ ?def factorial(n):; ]  B* i: ?- j9 x$ X! F
        # Base case
    2 }4 F' [% O) g; ~    if n == 0:
    * I, }+ H. ?/ [6 U2 J        return 1% f$ A% y+ S2 X* a+ V
        # Recursive case. `; A; Z$ }+ ]* e" b, Q
        else:
    2 L/ S! O, O6 D# V' ^        return n * factorial(n - 1)% d* d3 V4 P6 g2 e' G; j+ ~  J

    8 I) k( @: [7 N0 v; j# Example usage7 s; Z/ e+ V. }! q
    print(factorial(5))  # Output: 1200 u/ t$ d1 F: [+ U; ^- I

    ' s1 Z( x2 Z& i4 ~0 ?# UHow Recursion Works7 _. i2 C- e: B0 N$ L0 s
    , y% g- J1 t% ^8 W- N4 {( Z& F
        The function keeps calling itself with smaller inputs until it reaches the base case.$ h2 q; U$ b: o4 o/ u5 R8 q' [
    2 O" U( J, c6 J: H
        Once the base case is reached, the function starts returning values back up the call stack.
    4 |* e) Y" I) c7 L; f" X  l! c7 H( B" G
        These returned values are combined to produce the final result.
    & i$ R# N* A" H( n
    ! F: [. g7 Y3 l6 V0 IFor factorial(5):$ C2 Y7 t7 F% H, C
    . w. b$ [5 M  e8 n" x
    : C1 q/ T6 W" ]% j8 o0 q, b6 s
    factorial(5) = 5 * factorial(4)9 d8 E) f7 x' ?1 Z/ w4 c8 ~
    factorial(4) = 4 * factorial(3)% K! c6 K& g/ ~
    factorial(3) = 3 * factorial(2)
    ( N/ }6 p. u6 V1 Qfactorial(2) = 2 * factorial(1)
    " X5 a7 m' L+ D& f5 E- Mfactorial(1) = 1 * factorial(0)
    * W) a( f4 N4 g+ d% @4 B, Vfactorial(0) = 1  # Base case
    ; [6 |) R7 j/ J, L; X8 `/ f3 h2 E* {8 }: \4 ^# E
    Then, the results are combined:
    ' z+ g& p0 O0 K9 w( m3 U, ]# i, r7 d8 K% U
    : ~( ?1 }1 _: A" Y
    factorial(1) = 1 * 1 = 1
    , s( w: E" s; v5 J" Ifactorial(2) = 2 * 1 = 2
    0 v) q, K8 _+ I% K, q% k1 n$ ?factorial(3) = 3 * 2 = 68 T! L1 v: I' E) J. p; v
    factorial(4) = 4 * 6 = 24
    4 I" n5 ]; y1 j. g. cfactorial(5) = 5 * 24 = 120
    , _/ T& V  ^2 k) {/ U4 ]9 p' _
    & M/ x: d1 J9 H# gAdvantages of Recursion$ \6 |0 U' N8 t# [9 n! _  a1 t

    + |3 l  {0 E' X$ p) M+ B! n    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).' j5 W' `; k3 \

    6 ?' }4 @; Y7 k  M+ q4 u8 F    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    $ w: j4 U3 c; F9 X' U+ g; C
    " g. e, u" I2 \; k. }Disadvantages of Recursion, @8 E( R; @; c) o( c

    / ?/ c6 l" E  f6 |5 a; a    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.$ d. S4 o' c9 ?! W* {  y0 A  ]; O

    . N7 f- j% Q$ a7 n8 l, ]6 r" ?    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    - \# I6 D$ `3 V$ n9 i7 _9 t- i3 q% _# I" T3 _/ E
    When to Use Recursion
    , E7 S! @) s( ?1 V& i5 A* o  T4 e" M' G3 k/ q$ x& @
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    . j+ N: V4 y  }9 U' x, V& y/ d: W2 }; f0 w2 n
        Problems with a clear base case and recursive case.2 h# v6 t" p' q3 ^6 T$ W- P

    * R  K2 S9 @0 }( a8 ]( PExample: Fibonacci Sequence
    8 P: l$ L  ]5 l! d7 X/ K/ m' n8 b9 g& j2 B9 u
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    % g: q! u9 n  V5 f% f9 p+ F0 W8 B& ^, |% O2 G* u
        Base case: fib(0) = 0, fib(1) = 1$ F# _6 i- h* b( p. `! l) |3 L
    , n  t/ f  g* e
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    " S* O1 ~8 Q% w* Q) N0 B7 ]' V; D* V- K# ~
    python% J% q% A) [" z/ U0 H- l# T, |
    1 Z9 `( w( P9 }& d
    $ d* n2 {$ Z9 C! i) l" `! [
    def fibonacci(n):
    , ?# s. C- O& A2 U    # Base cases
    7 @" @* |8 _% _( A* @- z" @    if n == 0:
    / Q3 ?& o. e* C: C+ A4 t1 e        return 09 D7 b- P4 Y2 d* W( q5 f6 O
        elif n == 1:
    9 x) q4 R/ J2 L) u9 H        return 13 |) L( Q. m& c! B. H) c
        # Recursive case
    7 A6 i) F  n' F. x0 P    else:
    8 a, g' K0 A, a; H' X        return fibonacci(n - 1) + fibonacci(n - 2)
    % t& }: \) y4 [% x5 m, [9 k+ Y* A0 u3 `# |: i% D2 N/ C$ R
    # Example usage
    5 ^9 [; }: z2 S: W0 uprint(fibonacci(6))  # Output: 8" N8 l- m0 R5 i4 O9 H7 F$ e
    3 k: O1 |$ k, b/ L- Z* ~  @7 j
    Tail Recursion& m7 e# _5 Z( m. f8 L9 Q+ @# N
    6 C0 k" v; `7 A  ^. y$ ]& o
    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).* l: a9 [5 E7 W! ^, K9 h
    4 o1 F, X/ D$ D0 e
    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-1-13 00:52 , Processed in 0.030819 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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