设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    5 k+ ^' e5 c0 Z$ O; ]
    # s2 r. K/ f1 b3 y, J8 O解释的不错4 O, w2 c; x; v* s# \7 N0 T

    9 e! S, Q$ h0 `, M  W2 {# I递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    $ [9 C. G5 o# O; G' p9 l
    1 i$ }' v- n0 L9 ]+ f& s 关键要素  ~" t1 _2 ~" H0 l
    1. **基线条件(Base Case)**) L1 [5 i! e  i6 O$ O
       - 递归终止的条件,防止无限循环0 `2 b! S% D2 w' M* w( n
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1# q$ P9 b  O/ w( B: P) @$ }

    2 J% H' ]. N* @' U: g5 N1 G* ^# |! a7 k2. **递归条件(Recursive Case)**# T' i/ g2 i6 u. ~5 p4 A: H
       - 将原问题分解为更小的子问题: l( g. U) u  E( W3 `
       - 例如:n! = n × (n-1)!
    * b0 k. R; w$ M$ t: j" f* h' Z) q4 j9 v+ B
    经典示例:计算阶乘
    0 `- M: y; Y* |: Cpython2 V6 A$ X7 y+ m  Q  ^. z
    def factorial(n):
    " f5 l$ n; q$ C; T, E0 O% P4 N+ N2 R    if n == 0:        # 基线条件* p1 l$ W3 z2 ^. ^1 e; B" {
            return 17 H# J! v( D# }! X0 ^  Z5 Z
        else:             # 递归条件
    * u0 D( w5 |% P. w! m( [! p        return n * factorial(n-1)
    6 Y! W, K; ]" _1 s1 E/ _" O0 w执行过程(以计算 3! 为例):- X* K- U9 G, p; h& a/ @
    factorial(3)0 @! i+ s! V, m* q1 v
    3 * factorial(2)/ U. X; x* K8 n- P$ L% ^5 }
    3 * (2 * factorial(1))
    / z" s. J5 O9 X" D- }3 * (2 * (1 * factorial(0)))' U* {8 b" X8 h" ^, |1 S+ h
    3 * (2 * (1 * 1)) = 6& R$ c5 I; W* t) d! V5 v
    & X/ r5 g# @. E( W: `- A
    递归思维要点
    4 a& O7 M; a4 I  U+ S1. **信任递归**:假设子问题已经解决,专注当前层逻辑& G+ h. D% d  N' F9 e& p# m
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    7 M# P; f/ [: c/ P' w5 v# J  G3. **递推过程**:不断向下分解问题(递)
    8 {. q* S3 c' Y8 k; R: ]4. **回溯过程**:组合子问题结果返回(归)
    7 X; k, s, @" f' I3 `( t
    + r( L  W8 ]! g4 }& b$ M3 l0 V' a 注意事项. R' Q+ r) `$ ^0 C% o  I5 V/ u7 q. Q
    必须要有终止条件
    . n/ ?- A; U" x# ?! ?* `+ ]6 N递归深度过大可能导致栈溢出(Python默认递归深度约1000层); ?$ ~$ _$ E/ P0 T4 o& O
    某些问题用递归更直观(如树遍历),但效率可能不如迭代. k: X7 |8 o; s- A2 q' s3 `
    尾递归优化可以提升效率(但Python不支持)" `: S6 h9 i% X+ Q

    : }" w) E- ^6 z& z( U6 r 递归 vs 迭代1 `3 V4 E5 E: K$ G( B$ r2 n
    |          | 递归                          | 迭代               |& m5 K( S4 P' B
    |----------|-----------------------------|------------------|; q5 Y/ X8 T- J9 ?& b+ ]
    | 实现方式    | 函数自调用                        | 循环结构            |. K; f7 F8 m! ^
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |- z$ l7 B% @* ~& H' ~% X7 `
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    $ U; d9 g# v: K) g1 n| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |, ?5 Y% M/ \* t, \% e6 {$ ~
    - v2 V5 Q2 C" k8 G* T: F
    经典递归应用场景, ~5 s% n# X9 c, O  I( I
    1. 文件系统遍历(目录树结构)
    1 m0 h( X; N: h1 g' A6 g8 N! r" }- h2. 快速排序/归并排序算法
    # V7 e6 x) ?$ h& ^6 R3. 汉诺塔问题
    / w# o% M) E! S7 h4. 二叉树遍历(前序/中序/后序)
    4 d: a( G" [2 H# S, S3 @& E7 N- e5. 生成所有可能的组合(回溯算法)" \  B3 n/ m# L. ?

    & L$ W& l) _5 g试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    前天 01:20
  • 签到天数: 3115 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    3 \$ B9 G) O$ D/ l' z  {9 g( G; [我推理机的核心算法应该是二叉树遍历的变种。; _/ N  Q+ ]' }, F0 l
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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; P5 L* K) a* b
    Key Idea of Recursion
    # ~7 q* t! A9 H+ U
    8 f3 o7 z- [3 d' Q+ F9 iA recursive function solves a problem by:' n4 s( l9 g) _# ^# @. Y; O2 Z9 D' c8 x
    3 x& w# ^2 }# z
        Breaking the problem into smaller instances of the same problem.
    6 w# G% P1 L! o2 K6 S/ H
    ' C1 Y6 e3 ~$ _0 Q  U1 j0 L    Solving the smallest instance directly (base case).8 D! x2 M% P* a. i

    3 Y6 U) g0 C, v    Combining the results of smaller instances to solve the larger problem.9 i: x+ s2 l' A7 D' C/ E; Z
    9 ]) ?* Q- d8 m7 I2 q
    Components of a Recursive Function. w0 D$ V: C2 ?) {5 `

    " c/ N# D9 l& A6 H% T    Base Case:$ u8 U9 S0 Q% v1 H) Y

    ! q' u; a& K; s) B' H. `1 \        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.( u8 L# S6 m! W8 g' r: S4 w

    ) ~1 j, \% p2 t7 h; M6 p% A) D6 n        It acts as the stopping condition to prevent infinite recursion.3 g, H& ?/ e7 N1 s9 h! T1 n; s+ e

    : E7 g1 Q& P2 a* M( @        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ; G/ Y3 Q: n5 R1 C9 P! \& d
    - l) H; ?0 j8 O& K$ z1 }4 s    Recursive Case:& V2 s1 _9 I2 o
    / w' M$ h& q! c9 f
            This is where the function calls itself with a smaller or simpler version of the problem.
    . M. w2 e2 Q0 W; B" e! J
    ( a6 I! ^5 T. d: v3 b        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ; ]2 I, z4 ]* T* G1 R. w
    $ c5 v. a. ?" I2 B  J% LExample: Factorial Calculation1 v3 z/ T+ z! r# G  S5 u
    ) R3 I  ]0 T& J  R. e  S5 s
    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:. h" Y( |# |1 U/ Z1 q( `) X
    2 A( _  W7 k  |( z
        Base case: 0! = 19 N1 S4 f& ?. q& _# n
    : L' g1 L, c, t4 [% S$ N
        Recursive case: n! = n * (n-1)!
    & W$ @: u2 J6 H" c7 q) Y- u, D1 C
    8 L3 G, u9 B! r: }* v! IHere’s how it looks in code (Python):' F. z. w2 e5 q6 P" O2 m/ a) B
    python5 t" r, K8 g# `7 q; d

    * c2 }7 u* D0 f- ]! K% L6 [7 N& o2 s9 _- _0 u
    def factorial(n):. K( d6 e- J  v$ h
        # Base case
    9 h7 a8 Z, z) E* E    if n == 0:
    9 v) V  F; p- V9 Z3 Q! T1 W# s$ Q        return 1; _( L: }' ^  \" f
        # Recursive case' o1 x, G! e+ @' B( ~$ {3 y
        else:
    : R" c8 [4 J6 x        return n * factorial(n - 1). m4 w, f1 x/ y  T7 }

    0 O; L+ O, m; E0 X: T# Example usage
    * e$ @4 J7 t% n# {( yprint(factorial(5))  # Output: 120  ]& H' R9 q7 M8 O5 }
    3 k3 r: M% e9 x
    How Recursion Works* g4 z' B% {- R0 Q2 k' l: i( A

    + Y) B# h8 m5 T% o* o% L3 I    The function keeps calling itself with smaller inputs until it reaches the base case.0 G4 m5 R5 G9 h" F6 E; b& P
    5 l$ k6 d  `% e+ x( J3 ?  V* E
        Once the base case is reached, the function starts returning values back up the call stack.
    : a' ~5 g. a8 @6 G" q2 l
    0 c) L9 U+ z) }    These returned values are combined to produce the final result.& u0 N) D, O  ]1 g
    / W  P1 N8 Y( I8 g, v6 s
    For factorial(5):
    ; [9 e- H9 J# Q2 c! l
    ) c! z2 o" `5 u3 Q2 ^! g! L0 o5 F- d9 c3 Y  }" X- n0 }5 P5 @, t2 ~
    factorial(5) = 5 * factorial(4)6 o& P1 e7 q" s- \
    factorial(4) = 4 * factorial(3), ]; E6 d9 i7 y+ j% [/ C
    factorial(3) = 3 * factorial(2), K; H! B) j0 x4 W: F( r
    factorial(2) = 2 * factorial(1)
    / ^" |: t- Y# R+ u/ Q& y' {factorial(1) = 1 * factorial(0)
    ; N4 D/ [) c7 Q3 b: a( Yfactorial(0) = 1  # Base case" P+ ~0 }) x) O8 y2 F1 t

    : ^* m" v/ G" p: ^* _0 dThen, the results are combined:
    ! F' j  c- H+ J1 E" k% w) s- d, K" X) p2 K( G" B! Y
    % z! z4 @9 ]( _( P! m
    factorial(1) = 1 * 1 = 1! B, |1 J7 d! Q% P1 r
    factorial(2) = 2 * 1 = 2) f, g6 M% a( o4 E+ c% v
    factorial(3) = 3 * 2 = 6
    : p( n+ r0 V: A1 B& A* o1 b& v  Wfactorial(4) = 4 * 6 = 24% u$ Q, ^- r7 C+ R- p. A1 e( G
    factorial(5) = 5 * 24 = 120
      E- S/ r7 G+ H  K% P" a+ |
    # o+ @6 d- j0 B+ i. {' S/ cAdvantages of Recursion
    9 d& t  Y/ |9 |( L1 i3 r4 Q2 c
    % H9 E* |: _9 a; X6 M$ R    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).
    & R6 J, I7 i: |, C3 l, C$ A9 y3 p0 `* h/ a8 I1 z( {, C! ?( P+ Y0 b/ I
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    * a0 G6 d# P: {$ u- a/ B1 \- G7 l
    9 G. C: p1 e8 o- w0 VDisadvantages of Recursion+ k: K6 x& c. H0 D
    / |' I9 O$ U" e) K
        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., U' g4 s+ x4 k! E
    5 `2 d$ j" M9 W/ H+ p6 ]
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    " ^6 x6 o2 z# `4 u# {- `7 R  Q. U/ S! s  P" t8 s* L
    When to Use Recursion: I  D3 r6 d5 j9 }* v
      f2 x. r& U( x7 P
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).% _: m2 ]) C& f' Q, ~
    - b( D2 D( q! T( K. X  n
        Problems with a clear base case and recursive case.
    / g- l1 X. q. _, T1 S% _0 j9 }& n; x5 j, r& ]1 E% u
    Example: Fibonacci Sequence
    ) s  h3 u; X9 _0 ^. O9 m# G1 E: O1 a% h! e% k4 e
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    , `9 h* Z% {. U. W& V* u+ f7 s4 ~: y+ Y7 K6 y( H/ m
        Base case: fib(0) = 0, fib(1) = 1
    . M" o# Q  u' V) A9 H+ D% T0 p8 m
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    + m% X6 j" d$ ~  v
    9 f/ V& w  O. bpython. n; r7 N/ Y' K& s
    1 c% N9 s; J4 F; Z. s$ o

    . k& U; k* R6 m, m, }def fibonacci(n):0 B! X: x' u" Q6 E  d" n# J; v
        # Base cases
    5 h4 L2 _* B# V    if n == 0:% n; W' a( f/ {
            return 0" y# P, v4 I8 m7 ^
        elif n == 1:0 Q* g) D3 ~3 F% f+ @  J
            return 1
    % Z. n+ y( A: ]: U* F& e    # Recursive case& D2 r& R, l8 E6 E7 |" ]
        else:
    # T- b& j* {( ]7 L: D! ?$ W        return fibonacci(n - 1) + fibonacci(n - 2)
    , h5 r$ U* E% ?- o; w
    . S/ u9 _9 z" U; T. I0 Q: a0 I& I# Example usage
    # G6 V4 W) h/ ?8 T' zprint(fibonacci(6))  # Output: 8% n: ^! g/ I8 y3 y: m: Z) ^

    . @8 g; B5 q. P# _$ BTail Recursion4 ^0 p2 g) j9 r
    ( d/ k+ O; Y1 M; H
    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).
    % E" w! l2 ^! g9 P( o3 M
    8 C2 M& _( n. C6 RIn 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-13 06:49 , Processed in 0.032321 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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