设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 9 t, q# M! V8 d% j" l0 }

    : S# \8 [/ A' ?2 M; _1 w解释的不错
    $ T, E2 z' Q, g2 f+ M" f4 D- s6 y& Z8 r' E7 l, Z6 i
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。1 w' J1 _; J, W% n+ F
    ) ^; e* |0 g: v3 K& u% k0 E4 x
    关键要素4 X" H# L2 S  r. f+ J
    1. **基线条件(Base Case)**
    8 k" {: w" ]4 G) i   - 递归终止的条件,防止无限循环, h# {  @0 |2 G: @
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    & s. S5 @' F; C" }  z& d: |0 [
    2. **递归条件(Recursive Case)**0 z0 P& k& u9 {0 x. o
       - 将原问题分解为更小的子问题$ K) ?$ W0 z. k9 F8 Z! j
       - 例如:n! = n × (n-1)!  t+ [3 B! P. }! x/ T( T& p

    ; k( q! H3 X& t7 j 经典示例:计算阶乘9 `8 P* M5 u) o: g- N/ _1 `/ @
    python$ N/ x2 p. e8 f
    def factorial(n):
    % N$ N5 T' }9 H* \    if n == 0:        # 基线条件
    & [8 d4 N* b- l9 h6 p, F        return 1& J, y* m1 h2 H' s, G
        else:             # 递归条件3 T- C1 c/ y5 @$ G' m$ @% t
            return n * factorial(n-1); ]- I* {: K" `) d- W  p- R" H
    执行过程(以计算 3! 为例):, d+ n1 e* `' W& r- u: t: ]7 W
    factorial(3), ~" h, A( x, M7 ?0 L( q# \4 V' P
    3 * factorial(2)4 e% l3 p/ K: U/ y
    3 * (2 * factorial(1))) f# V' C2 W1 x: p1 Q! S: ~* z
    3 * (2 * (1 * factorial(0)))
    , ?1 {& B. D! b6 {9 a% u4 ?. s; R0 U) H3 * (2 * (1 * 1)) = 6& Q: E2 ?4 ^  M8 ]

    # m5 {8 _* `' S( H 递归思维要点
    , C) W9 k, ^. R5 W6 S8 O0 _, ?1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    " Y$ r; K8 Z, S1 k; ?: _7 `2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    : g# O+ c3 R3 B# T' M3. **递推过程**:不断向下分解问题(递)- b& @& h0 d3 K
    4. **回溯过程**:组合子问题结果返回(归)
    8 `5 m2 D( `$ w# u0 U! s
    5 O# T& W; a! [2 a0 @ 注意事项
    5 J7 n1 ]. q8 }! ]3 t2 _必须要有终止条件
    9 n9 d( l8 R" @递归深度过大可能导致栈溢出(Python默认递归深度约1000层)) X3 b2 c# \8 r2 x5 f8 g
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    & o! B) v  [: l6 j4 F尾递归优化可以提升效率(但Python不支持)
    : {, A+ ?8 Z; C4 T- m/ n  l2 |. K$ a
    递归 vs 迭代
    , ~2 L- ~" ^8 F4 ^|          | 递归                          | 迭代               |
    : x2 F' I7 j& n& W|----------|-----------------------------|------------------|
    / n' b7 ?5 v* d0 b9 Y' O| 实现方式    | 函数自调用                        | 循环结构            |1 P- K3 }4 z. i7 q) s6 |; V* K. _  x7 n
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |- ^5 y, \. p' s0 I5 s
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |1 W$ e0 Q/ o7 q  I3 V2 n; o
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |+ f. E  V- x4 Z! a, u" [% r% K

    . ~8 T( B5 G9 j 经典递归应用场景: r& b. K# r  V% K+ z  {
    1. 文件系统遍历(目录树结构)
    $ g1 E0 G% z4 e' c6 C* ]7 ?% j4 x! V& z2. 快速排序/归并排序算法9 z. g% i* M: r6 L( R: R3 a& j
    3. 汉诺塔问题
      m3 M, x- c  N. Y9 F8 q4. 二叉树遍历(前序/中序/后序)
    # `, Z  f  t2 {* B+ K) n; p$ g5. 生成所有可能的组合(回溯算法)- \( d. l+ _4 }5 E; V$ ?6 m6 Q- x

    8 n: u1 I8 T3 v. J% V1 ?5 O试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,2 X) @( n7 H) L
    我推理机的核心算法应该是二叉树遍历的变种。
    & K( n+ H5 q2 V& 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:
    1 _$ e- o, t# S! S  S% zKey Idea of Recursion
    7 V+ g0 }  r- Y1 F6 b' ^& Q
    * U% N3 `3 ?3 Y! K# @A recursive function solves a problem by:) J" [4 U! `% D# h
    : m3 E* E: s7 C2 g
        Breaking the problem into smaller instances of the same problem.
    2 `: \  E: d' I' T% x* |2 i! a+ E
    4 Q/ ~. a% Z: o. y    Solving the smallest instance directly (base case).5 v. o8 a) G8 s. i( H+ T

    ) x9 S/ M0 ?& U2 v1 o" W    Combining the results of smaller instances to solve the larger problem.
    7 B$ \" j- w& d$ v4 d1 q
    2 \) d% ?  f* nComponents of a Recursive Function6 W( e* l+ I: d/ f

    " o, g3 o5 M) X/ K" `& v* I! m    Base Case:! J5 c7 ^% u# Q; x8 k
    2 O. s' d. ~7 Z
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    6 |  p& Q" g: R8 g8 v6 T: Y6 O$ L) b* B6 E6 q
            It acts as the stopping condition to prevent infinite recursion.- f) Q+ q4 w; a' v
    $ p, [8 ]/ F6 R( D  o
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
      \6 f) H6 [2 [/ }4 t" k$ Y9 r- t
        Recursive Case:( j; Z& l- d9 E9 s2 M1 Y
    . w# z' D7 t( m5 ?: S# ]9 ]: x
            This is where the function calls itself with a smaller or simpler version of the problem.
    5 y+ b3 i! b' d( H9 Z3 w& Q9 l. Q
    ( }$ X% [& K! d0 g/ V        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    , j' y  h/ l4 _( j& e
    " Y/ D8 q  ^7 z; x! W+ f  `+ _Example: Factorial Calculation  M' ]5 [+ N* q, V& r) |
    1 ]' l6 y5 f4 Z7 K. s1 f* @
    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:
    : C3 F1 t$ Y5 R4 ]
    & m) S  _9 X) I7 A+ b' g6 C    Base case: 0! = 11 @* ~% ^2 O9 q* X7 C- n

    # q) r% N! k6 F# U7 c  c' A    Recursive case: n! = n * (n-1)!9 H/ o( k8 D  K3 Q5 e4 V4 u

    ; T+ ^) b, Q$ I. g1 @; \Here’s how it looks in code (Python):
    " W+ g- \4 m4 Ppython
    3 @# z" I( ]2 `) p! U, H' L4 E  _$ R. w- q2 o+ W# Z+ Z: t0 F: P5 A( N8 `

    . ~7 _1 A* P# }* [; gdef factorial(n):
    . u4 u4 D1 F) Z7 ]- r% q# h6 a    # Base case
    , {; {& j$ h1 ^    if n == 0:: W: f3 B9 o# @" P# a2 A, i8 B8 O
            return 1* N2 l& F! v3 M" _
        # Recursive case/ l) Z' k5 l2 m+ B: F
        else:
    ! @- H6 i( }! o# b. d  m        return n * factorial(n - 1)
    . p- c" n* ^$ A! g; m! U' P4 Y+ t- ?; S) h8 J9 R
    # Example usage
    & e7 K; T$ U- M3 W; l6 Aprint(factorial(5))  # Output: 120
    6 U2 n9 n' o  s: u8 X3 Z. c; s! E: m  u0 Z2 j" u* V. x- J/ A" a- v
    How Recursion Works
    9 U- ~$ e) j7 W8 v% {
    / K8 I, `3 `) `; d    The function keeps calling itself with smaller inputs until it reaches the base case.
    8 N; {# Q: h3 m) Q: h9 h) Z+ H& v
        Once the base case is reached, the function starts returning values back up the call stack.
    # [+ t2 |' _+ z; T8 F0 d% g; [5 d4 Q/ K) q
        These returned values are combined to produce the final result.% j- r! M, V* G: Y% W

    : G7 g& B2 _! c9 bFor factorial(5):
    * `6 y1 P5 l* g- b3 n' P# ~
    2 r, B% }7 Y1 a& V; ^
    + c: E" k- P5 K  Z9 Jfactorial(5) = 5 * factorial(4)$ v* s- H3 M" F& m, V- A4 M
    factorial(4) = 4 * factorial(3)( @6 t6 P* A- Y& g; T
    factorial(3) = 3 * factorial(2)
    8 Y2 P# o" B; ?+ d0 n* Tfactorial(2) = 2 * factorial(1)4 Y1 t! H& |& q2 p
    factorial(1) = 1 * factorial(0)
    / Y, D% [3 O- W. }, Ffactorial(0) = 1  # Base case  v4 [2 ~; l4 K1 B5 y$ ?3 n
    ' G+ f$ q7 Q( h0 a
    Then, the results are combined:" C& @8 \4 N2 o0 X/ p
    / X: T6 F3 ~0 b5 ~  s

    % S9 c3 s$ `# e- a4 t. l$ Mfactorial(1) = 1 * 1 = 1& ]9 ], ^% M! i! V5 Z" R, y
    factorial(2) = 2 * 1 = 2
    ; C5 L  i2 V9 Q# d$ c$ P+ lfactorial(3) = 3 * 2 = 6
    & F! E/ ]: B$ ifactorial(4) = 4 * 6 = 242 B& M7 _4 v1 B+ W2 o+ m* J
    factorial(5) = 5 * 24 = 120
    : e' U" D6 x) x9 W
      l5 f0 f5 D9 A: c  T/ {Advantages of Recursion
    * r' Q: |6 u' _* L6 R. f0 ~# w+ l! [' w( S
        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).
    / V/ k; U3 }2 Y; A3 Y. o! {' u4 q
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    % M; s. A& p, ?% _  b% b: r6 ?& U7 f
    Disadvantages of Recursion+ b+ C$ C6 f4 z3 Z1 ?. z
    : R% I; I8 H3 u$ E
        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.( g, b. a% P1 q( c; O
    ; h' p5 d  A, j+ r+ T1 U. ~
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).3 S# C# l8 D# Z% I1 k6 Q" i6 B6 ~! B

    $ Z) z3 _, Q9 y  {$ ?7 RWhen to Use Recursion
    % Q# e4 z% g1 J# }' Y" @! t9 f
    $ l9 u5 ]4 Q! {  @    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    5 I; y4 E7 X! }' ?& V9 D, m: m6 ]) M% f- ?6 w) Z7 K6 ]" b
        Problems with a clear base case and recursive case.; i& N' Y6 d4 B% y0 z- c; A4 U
    ' p6 O5 x. l. v: z5 o" T: U7 |3 d
    Example: Fibonacci Sequence& a& ~* B4 R% ^7 R& C

    0 Q  }: K. c1 }: ~! iThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ; E, J1 D3 _4 k' C0 _
    ) U1 l  c, X# e    Base case: fib(0) = 0, fib(1) = 1
    ) g* X6 n  [1 L( K' a
    3 j8 E3 p; F; z* }& E, R+ C. X- S    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    / ^. @8 y9 E; a) B+ y7 f, D4 j7 d4 g( q$ t+ N# W8 s3 C
    python( D" S8 s8 A8 v. B( z$ s* G
    + C% e; F: V3 L

      W- z. N$ o" B/ Y1 Wdef fibonacci(n):$ U3 o1 T( V! a, q, o
        # Base cases
    ! d' q0 s& v1 q2 b) @- {    if n == 0:0 x! p4 Y' u8 b( N8 M
            return 0) `( ^4 M2 d. h8 B$ d4 p: x
        elif n == 1:
    7 ^* ~1 e/ _7 ^- o' k0 Y1 @- a$ B+ m5 i        return 1
    ) m0 U; e! I; y6 n    # Recursive case" n* a: R+ U! `) _! ^
        else:9 D" M) V" b, n1 E
            return fibonacci(n - 1) + fibonacci(n - 2)
    & O' u7 ]. |5 e6 N9 f, k+ j, ?4 \$ k" c/ T. r: x
    # Example usage
    / R+ h' K5 {, ^print(fibonacci(6))  # Output: 82 D/ _: u- ]& j9 P
    4 U# U5 Y& ?; C
    Tail Recursion. a# v% o0 y' v1 p

    % M$ \$ p& V' j4 p6 hTail 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).0 w. D6 c& K9 N0 P! q, C" M

    * a9 O4 V5 V; N; jIn 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 20:25 , Processed in 0.059808 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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