设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 $ F) _7 ~- ]. Q9 s1 N2 w3 u0 b
    ' f# K: I- b3 p. s0 D7 q, ~4 E
    解释的不错7 \* \0 v& h. J3 G, J0 K
    " T1 M+ `4 w  {; _
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。6 q" z- I3 z) E
    % G# U% v7 ~7 G1 C/ X9 V
    关键要素
    3 M9 e; `1 e: ?5 z+ Y5 p1. **基线条件(Base Case)**
    % y! d3 r4 f1 [   - 递归终止的条件,防止无限循环
    ( T& \% J* M6 Q- R1 \9 s   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    7 a' V- [$ I% U, C: E) V. {# c/ l! q* ], j2 P; |
    2. **递归条件(Recursive Case)*** U. I  o3 d. t: i" a6 |* U/ S
       - 将原问题分解为更小的子问题
    * W# I" E& c  M% ?- A2 n" P9 {4 N   - 例如:n! = n × (n-1)!
    - }: a! C: L* m/ d) F* Y5 G4 v5 i0 W+ I- ^: G
    经典示例:计算阶乘' z/ A2 v' _0 u, Z2 u0 J
    python
    ) A. K6 v! ]1 Y7 {5 Fdef factorial(n):1 S5 r6 m$ g8 i1 I  B/ r
        if n == 0:        # 基线条件, M* M- [/ B% I8 I' r
            return 1
    $ `$ h; S" X7 Q# t4 X    else:             # 递归条件
    + v5 s. F, H2 B# e        return n * factorial(n-1)
    6 t2 l7 y4 O3 O" ^/ C执行过程(以计算 3! 为例):" e- z: Q; H5 ^& K! T4 u
    factorial(3)) o! U7 f/ z6 `$ K& ?' e5 |
    3 * factorial(2)4 @5 S3 A1 M) ?& F) \1 C4 M
    3 * (2 * factorial(1))2 q- C0 e9 _% w( m2 ?+ E
    3 * (2 * (1 * factorial(0)))
    * c( n, a$ J" \4 N5 v* `( v' r2 b! m3 * (2 * (1 * 1)) = 6
    & M. b" V; x7 F  ?" s. \
    8 r- ]% z0 P- W  j0 k 递归思维要点( C4 B1 b5 g$ H7 o$ c
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑$ x0 a" ?% g+ C/ D% c9 T% {
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)4 q/ e( H2 Q) p/ o( S
    3. **递推过程**:不断向下分解问题(递)
    $ u3 R& q4 ]6 O" |6 ?4. **回溯过程**:组合子问题结果返回(归)# p0 K$ u4 @3 r: J3 s
    , b8 l, \5 T! Y2 H' }
    注意事项
    # K7 E9 c; T! s+ P必须要有终止条件
    8 H3 j/ z' k( M+ N% O( ^' o' }递归深度过大可能导致栈溢出(Python默认递归深度约1000层)) ?8 j6 W1 H2 K5 u! Y! W; ?
    某些问题用递归更直观(如树遍历),但效率可能不如迭代4 g$ q7 P# F/ g# L
    尾递归优化可以提升效率(但Python不支持)
    8 Q; g4 [  i, N& ?, s5 g" ?
    8 {7 B9 U/ K/ v# E, Y5 Q' @ 递归 vs 迭代
    5 Y' \9 Z/ u& U% N  ~|          | 递归                          | 迭代               |( W; ^8 l6 G: C& X2 M: K7 s) p
    |----------|-----------------------------|------------------|
    * T4 J7 w, X* Z- G2 A; R| 实现方式    | 函数自调用                        | 循环结构            |5 }; ^% V0 S4 u* B  J0 d
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |- ]+ S- Y6 R* o8 W; }
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    " U0 _" W  P) @4 l& t: l| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |# l! ~! H$ y6 l: C8 F! t/ U4 E

    " T' u. U" b" e# p  G5 [( _) I" A 经典递归应用场景
    ! ]. t5 ~4 y; N1. 文件系统遍历(目录树结构)
    5 f  t7 r6 Z/ o! a  P6 O: f+ {2. 快速排序/归并排序算法) t; q0 s, ~3 s  V# @
    3. 汉诺塔问题
    6 q( J# ?: a  A3 J% y1 s4. 二叉树遍历(前序/中序/后序)
    0 f0 Y/ _% |3 t3 c- e4 G5. 生成所有可能的组合(回溯算法)7 @$ n0 y! {" }+ Z7 R# E
    0 d  K! Z& C! N; |5 N5 v
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    7 小时前
  • 签到天数: 3162 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,% Z: J* E) j6 w5 ]7 @; h* a
    我推理机的核心算法应该是二叉树遍历的变种。  _2 E2 M9 i4 O( [! ^1 D) 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:  G$ I2 a5 u. L. z, {& i) V
    Key Idea of Recursion% `0 P5 D% a5 E0 S4 r# y3 V
    $ S: d6 u% f- q; t) R- G
    A recursive function solves a problem by:
    . j( q. v2 x( r( o5 D( c
    $ A) T9 N( }1 A' e9 I7 p8 I    Breaking the problem into smaller instances of the same problem." ~% z9 h& F' W- _5 z3 N4 Q
    ' Y* O* I# y" N
        Solving the smallest instance directly (base case).
    0 g+ [" n+ t' Y4 e+ W
    2 {! F0 Y: A2 l8 c( X: W) e2 P    Combining the results of smaller instances to solve the larger problem.9 y0 Y) [* H2 r3 o
    : B( K- L: v! ]' Y0 N
    Components of a Recursive Function
    4 Y8 @5 i% s& f4 g! j- n( `4 u: ^+ f. @, E4 [/ f2 ]' U
        Base Case:5 h( |3 }  `4 O' m( }$ T

    ( S9 ?: J- u! z: Z        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    2 R2 T* M* Q5 `  ]( B) g& ]3 e, j
            It acts as the stopping condition to prevent infinite recursion.' X* p7 t: O7 u' Z) y# ?' o0 A
    7 E+ d: E$ D. D: X. M& D" u
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1., e0 N. {) y. b9 s- D' |
    ) H; c- E) N7 l& O  F
        Recursive Case:6 R( q  r. L! @& n

    5 W5 M2 U8 A1 S' }$ J6 _        This is where the function calls itself with a smaller or simpler version of the problem.! p, U* ?; j) m3 Q5 j

    % q/ M8 u( p7 i2 y* x        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).+ R" I$ C- b  N; m+ F) A

    1 X$ h% I- t1 b- r  L+ d! WExample: Factorial Calculation
    ) h/ ~. p; N# t7 }3 Q, p% o- i2 ?' {* p# l3 [0 W  Q  {$ e
    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:
    . x& H% k* n) C, z: H' R; `6 r, b' e; ~8 i* T, L
        Base case: 0! = 1, D+ d2 {/ ]/ L" v$ w1 c

    , ]5 G4 @7 e7 W    Recursive case: n! = n * (n-1)!" q8 s3 b* G; H) A7 x. l

    + o& \. d) k" a6 fHere’s how it looks in code (Python):# ^+ u* a3 V: w1 n) s4 r. E6 r/ b
    python
    9 i7 M1 d3 g& M, `+ Y; d) ~8 S* [  x7 @! _+ e0 H- W( Z: B, v' D
    # X7 y9 r( D8 c+ B8 r
    def factorial(n):4 Q6 a3 c5 V$ `  P4 s. x
        # Base case
    : {' e+ [7 I$ ^, y    if n == 0:
    9 C/ |: ^7 e2 ^  z. y; ^" J        return 1
    $ u" M+ p& i% r1 N9 W3 ?    # Recursive case
    / d2 V8 k- a4 c6 n/ J    else:( Z1 W: S6 b" i' Q4 F% f; G9 u6 O- S
            return n * factorial(n - 1)8 F! a" o" \- ~9 }5 t
    ( E: a$ r/ K3 e% Y
    # Example usage
    6 R: H6 f/ u2 i1 Y! Uprint(factorial(5))  # Output: 120
    3 l2 c3 c8 o* ?: x9 s6 r. B  n* Q" t
    How Recursion Works
    - I- a+ x( G2 w! b$ Y) p; `1 W) Z3 G( o* M* z
        The function keeps calling itself with smaller inputs until it reaches the base case.
    1 }& a' O/ I" p' e1 M- d" Q  h' Y4 {, B" h. P8 ?
        Once the base case is reached, the function starts returning values back up the call stack.# h& I" [" O3 D! u% Q, M& H
    ; Q  ^: E7 I. N
        These returned values are combined to produce the final result.6 O) |( j5 L6 _

    ; u! T4 W# o1 m' [) O/ o  ZFor factorial(5):
    0 e3 z. F% C& @, Q2 p8 K
    9 c7 j8 i7 L/ K2 N  N0 X0 U1 G% C
      ~6 E2 v! y, K. q. h- c7 ~4 rfactorial(5) = 5 * factorial(4)& O. d: x. @0 d" }* @7 B
    factorial(4) = 4 * factorial(3)4 H  [3 n! F8 Y' X9 X; s! ]8 T
    factorial(3) = 3 * factorial(2)
    ) A# g$ ]1 B* X* ~' |factorial(2) = 2 * factorial(1)
    ! }& ^9 B5 }- \/ X9 i9 \factorial(1) = 1 * factorial(0)1 S* q( [1 ^- V# e& F& ?
    factorial(0) = 1  # Base case
    ) A  y8 Y2 j4 d! }
    + ?- u) q4 T: n9 J3 hThen, the results are combined:: }+ W- {; A8 O8 s# H4 t( R
    + a4 }# s- ?* A# ]4 ~
    9 L: d, z3 i2 T
    factorial(1) = 1 * 1 = 1
    ' Y$ h* v( D! t' Zfactorial(2) = 2 * 1 = 2
    5 |1 {6 E9 P( T; hfactorial(3) = 3 * 2 = 6
    5 t6 G* f8 H" X: c% Tfactorial(4) = 4 * 6 = 24
    ) z8 I" k7 n! [3 D% sfactorial(5) = 5 * 24 = 120# M( _# @5 W$ C' a* q3 R% U) C
    ) O2 u8 ?1 d% G' \- u
    Advantages of Recursion9 p/ A7 u4 A# o, t* E% t" a
    3 X( ?  n- ~7 |. 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)./ \/ Z0 ]+ b+ h5 o

    - V1 k, P% X2 f& z    Readability: Recursive code can be more readable and concise compared to iterative solutions.2 l. f% n( B" n2 ~
    3 r5 K- c5 L; w9 V4 M& O( J
    Disadvantages of Recursion
    * q9 ?' H% z* k1 Q# E8 u6 v, u* u2 f4 R0 U; m/ f# L8 @" r
        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/ W. _- f, q" R; g/ U; z" g8 X& ]" R
    7 H/ n1 N- W( K( j) l% o) B
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).1 q4 B3 @; ]! [7 I
    7 z  Y' D$ J4 a' n2 `7 c
    When to Use Recursion
    0 ~0 N/ ~' l: o2 Q" @) X
    8 A8 F1 H/ P4 a9 l    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).3 [8 K9 _+ i+ S6 T' @* u
    1 O! C) l, z! r( R1 X  u2 s8 _5 Z
        Problems with a clear base case and recursive case.
    % B+ p: t  n) t# A9 r$ g: l1 S  {/ y* n4 M% @4 {# @: P1 o
    Example: Fibonacci Sequence
    7 H! }/ ?* i) h; L+ Z' Q) X7 ~' [) ~5 r$ @" c# c
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    * @  E' e; _" W. S! b$ O2 ]& X7 {# P
        Base case: fib(0) = 0, fib(1) = 1
    ; k/ k9 r% i% R/ \
    9 J' J4 l" p% S- _; I$ a9 b- m  _' Z    Recursive case: fib(n) = fib(n-1) + fib(n-2)4 V# {9 L( f. n; {8 H+ R

    ! ]! _0 @" q" wpython
    / t2 ?. S- z' P3 G* T9 v- T6 K- I9 z' O
    : e% O1 d) {, ~& i
    def fibonacci(n):
    - ]  f; I: x: G# H6 n0 g    # Base cases
    5 B! X6 T/ ?$ o& k. Q$ v    if n == 0:
    2 T3 x/ s) k' m  n7 Z9 m        return 0
    - W, `% A) W7 s, K. T; [    elif n == 1:
    8 s. M( G2 e' H3 z        return 19 I/ w5 T' O* t+ `- D* {
        # Recursive case
    & X: M4 a- G0 a5 h2 i, T    else:5 G" `' S5 f  Q% b0 r0 I
            return fibonacci(n - 1) + fibonacci(n - 2): o9 e2 H8 g7 |- D
    9 I$ m* R/ N( F
    # Example usage; _: H7 e1 I0 r/ b( o1 B' d
    print(fibonacci(6))  # Output: 8. e- M5 x! n; A* _5 _

    ( a7 {7 B$ w+ A( s) BTail Recursion
    9 @2 z% o% u/ P2 R, J$ L% f$ G7 n  p) L  A
    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).! Z3 E8 R- w9 C+ H

    - v6 U* F) ]8 V) v% ^' d7 aIn 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-2-3 17:48 , Processed in 0.055520 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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