设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    1 f& ~$ H5 o9 [
    + H# i2 z4 P+ z; \* H8 k解释的不错( }. a/ B" p3 ~! i
    ; N& h( X  D" b5 b- C. I, l
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    7 I9 H1 U( G; g) \/ t
    . ?, ?: \( R4 b0 ^! m+ y 关键要素
    & d6 u; P3 k8 k4 o! `2 X( C1. **基线条件(Base Case)**% C( _$ L$ \1 C6 p1 z
       - 递归终止的条件,防止无限循环
    4 ^8 v  ?+ B; y/ U/ d   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 14 U8 X8 V% J0 t4 H* U
    ( {7 \$ B) y6 H2 o0 A) A
    2. **递归条件(Recursive Case)**
    ' T7 `+ |! U8 H8 w3 K" l- j2 Q   - 将原问题分解为更小的子问题
    + z. P6 S7 w! i$ z8 s- V   - 例如:n! = n × (n-1)!! W) m  D" `; s- U5 b
    0 b: c3 o4 t& z8 l
    经典示例:计算阶乘
    . e1 ~5 @9 x9 u7 @9 }. K- i! s/ npython0 L  G) F- ^7 ]( X5 F0 U
    def factorial(n):
    , k) Q$ D. P% k" V, ]: B    if n == 0:        # 基线条件
      r- i3 x3 s0 o% e! H        return 1
    4 u$ G4 F) U4 L2 V; j    else:             # 递归条件
    6 q4 N9 ?+ ]( l! A        return n * factorial(n-1)
    0 @& l* Y) p3 b5 E执行过程(以计算 3! 为例):
    / b. `# g) ?; x& gfactorial(3)
    4 ?. t# Q+ d6 ?  z' ]2 _3 * factorial(2)
      E3 q8 y/ i& b) ~% ^! S2 z2 {3 * (2 * factorial(1))
    $ n) g; _' U$ T) R9 ~; g- X* W3 * (2 * (1 * factorial(0)))
    / p3 L- k, l) j/ Q! V" q3 * (2 * (1 * 1)) = 6  K3 w* u' L5 r4 O9 |5 r' h2 V

    1 i1 C7 Q) }% a+ v3 d# w9 o: V 递归思维要点' D* g9 J2 p, j9 ?7 g7 q
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑' Y7 L' {" g9 A
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)" M1 `' a& W- j% b: D; b
    3. **递推过程**:不断向下分解问题(递)
    ' O" V6 I# B1 I4 {  z& ?4. **回溯过程**:组合子问题结果返回(归)3 g: A7 F! V3 n% I' T5 T1 @

    * Z) C, \) ?; G. h; p8 \$ ?$ Q 注意事项3 ]* R2 z. H: D0 g& e
    必须要有终止条件1 J. n7 i$ g- C% z8 M7 f
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    9 M) e# ?' w6 O4 F8 O5 _某些问题用递归更直观(如树遍历),但效率可能不如迭代
    + |) F: C" a8 M5 c3 f- h$ h2 Y尾递归优化可以提升效率(但Python不支持)
    - e- D: R) c! J' T% D  }7 P
    7 ~/ {# c  G! t* P2 Z" S 递归 vs 迭代
    ; g. |, H6 b0 S  {9 M' u|          | 递归                          | 迭代               |5 g- l6 T( t5 v
    |----------|-----------------------------|------------------|
    7 }+ l) h2 \  P6 d7 R| 实现方式    | 函数自调用                        | 循环结构            |7 V2 ~. n- q( R' Q2 x
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |1 g+ d/ B# f3 f+ c& S2 j8 k& D
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |2 l1 h  a) S5 f. `7 B
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    + D6 z" D2 ]* i" A  y' S+ b5 z( S8 N' B$ ^0 x
    经典递归应用场景
    ; P* {  Q9 L  }7 o1. 文件系统遍历(目录树结构)
    & o* ~8 z. d' i. Q# j8 I  f2. 快速排序/归并排序算法
    - F& }: ^' N+ v( A$ O; s) C3. 汉诺塔问题
    3 s6 e: \" K9 N6 U* Y4. 二叉树遍历(前序/中序/后序)/ H8 E& S( r) R
    5. 生成所有可能的组合(回溯算法)
    1 m: s4 R- C" x& o  ^9 A, I) \0 {5 o3 [% _, ^9 ^
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    15 小时前
  • 签到天数: 3085 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,/ x! |0 w, F8 B) |9 V
    我推理机的核心算法应该是二叉树遍历的变种。8 h7 R5 X3 p( T& }" \2 z9 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:( N  J1 \: i* i$ }: [" Z4 q* C
    Key Idea of Recursion
    - B1 _1 b6 B+ h) N6 k6 B0 w: g, F8 J, Z, p
    A recursive function solves a problem by:
    + x* F% o  u; F4 z: `  R: Z" r5 N/ Z7 t! q0 w' M8 ?
        Breaking the problem into smaller instances of the same problem.
    . j. D3 R& _5 Y! x6 ]
    6 ?) r' b8 j# L6 Q# V    Solving the smallest instance directly (base case).
    * C- }" d2 N/ F+ O
    - h' T+ z: J8 c8 \# z+ j    Combining the results of smaller instances to solve the larger problem.
    9 r# g4 G8 A) i9 a- o& U. I! D) `( }: m4 V  K8 _- `8 a5 C
    Components of a Recursive Function
    - ~( m$ t* Q  K5 e  [: H. O
    . b% r, O' u( X8 }" U. T    Base Case:9 I" w+ F! O5 z

    ; H, N+ S. q( n9 P        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    8 A9 l4 f2 g4 t. |1 M. j- {0 U6 A0 w5 g) _1 F* K0 H1 [
            It acts as the stopping condition to prevent infinite recursion.% \5 {5 G- [" x: }& h" A8 _
    # A/ l2 o( Z! p  @
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    9 R0 u$ |- o! l7 K
    / R, y2 h9 m. Q9 S) x: \. o. ?, Y    Recursive Case:8 l: e* Q+ u* r! E# ~
    # F' N/ P, I2 B( z
            This is where the function calls itself with a smaller or simpler version of the problem.
    . ]4 ]- k0 e1 Z# h* d  \
    0 x% ]# Q) n" s3 D. x# ~( C        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ' |/ `  ]0 ?3 S; x3 X* l0 f: f
    ; K# ]+ C3 y- z+ }) gExample: Factorial Calculation
    , z- g& S$ D4 y, i. ^; ?1 B- i4 `* G: v& L, C$ h9 i
    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:+ t  q8 Z' V! t9 q- j

    4 q8 f1 [9 z0 s' C% F7 ?    Base case: 0! = 11 F5 B, @5 y0 u/ [

    0 g, i4 B, C+ C' a6 G    Recursive case: n! = n * (n-1)!
    5 s+ i( w0 F* U. t. g. c* ~1 W- N5 H) {2 p
    Here’s how it looks in code (Python):
    0 b( _, n8 o8 n! fpython
    ) e1 Z6 h2 P% |6 a9 h* G" G/ c5 ^* V; U4 `, C1 p4 B5 K: f( H" `
    ) K) T) b7 U3 i
    def factorial(n):& {! ?9 `: }1 P8 J2 @! W, y3 Z; }
        # Base case" i! ?* ?+ @( z+ J& }, U
        if n == 0:
    3 a% z7 p0 _2 G9 R- [        return 1. e( h; d: g7 O" `7 O( o0 l/ S
        # Recursive case
    8 v6 }/ b  p; q0 Q2 `& i" _    else:! y0 I0 _1 L* k6 k6 l( V  y
            return n * factorial(n - 1): b2 x- I' {4 [& c1 E# v4 v. Q6 A

    ! V. F6 `. ]* i5 |, e, u5 i# Example usage+ Q# }+ K. n, W( ^
    print(factorial(5))  # Output: 120
    . ^9 p4 {5 Y3 R$ l% c! e0 U2 s. O
    4 t# t, ]/ ~- _, iHow Recursion Works2 [5 k0 W  a+ z& u, F- B

    7 b. l! m8 y2 H2 m8 Z5 a/ x    The function keeps calling itself with smaller inputs until it reaches the base case.# ^9 H  y6 S7 P" m$ _5 z

    : \  Y5 t3 I* f5 Z    Once the base case is reached, the function starts returning values back up the call stack.
    ' B1 c+ ?' E% m% w9 a+ V+ v& u" I# r" k9 q
        These returned values are combined to produce the final result.+ s3 ?% [. _. u2 U

    % ~$ s. G* \; }+ D, a; nFor factorial(5):
      @3 T0 j, O+ u" m: b
    1 X* J& T7 A! h4 Q! W, [- }  S9 K- ]$ X6 V
    factorial(5) = 5 * factorial(4)
    ; r$ |% k9 @; N2 q. F* c, v" I1 efactorial(4) = 4 * factorial(3)& z5 _; _( V3 \3 p6 \+ ]
    factorial(3) = 3 * factorial(2)( C' A8 q0 M6 a9 K5 r
    factorial(2) = 2 * factorial(1)
    9 @2 V- d; c# w$ [# J' Hfactorial(1) = 1 * factorial(0)
    4 g/ ]% P7 ~: Z: Yfactorial(0) = 1  # Base case+ k, @6 E" P+ O7 c# N

    6 v  c+ |7 M0 C7 j: EThen, the results are combined:2 ]0 Y( F. L. f8 ]; R
    ; K1 a( \' [  o, V: L+ P

    . v$ r. C7 Z' C+ _, n. yfactorial(1) = 1 * 1 = 1
    8 C$ \+ u  _2 n1 ?' qfactorial(2) = 2 * 1 = 2
    6 a3 h1 l+ O' x! W/ Kfactorial(3) = 3 * 2 = 6
    ) ~2 `. K& E1 `* Lfactorial(4) = 4 * 6 = 24
    ! Y9 q0 ^3 Y1 Y! C: M& V' k) \factorial(5) = 5 * 24 = 1203 Z5 b6 f8 c- o" Z5 n
    $ y, I% U# S9 `! [" `! Q4 E
    Advantages of Recursion
    / H; [' L) Q+ G/ P% ]7 u
    2 T6 U7 H6 \3 ]( I- B    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).. p7 X% o7 {, s4 R5 R8 `
    % \# N( n5 [) z& I6 z" C
        Readability: Recursive code can be more readable and concise compared to iterative solutions.& ]$ M* Z2 M- u. R

    ; y! R6 m0 i- h) L( |Disadvantages of Recursion/ j: ]/ ^& C5 k/ T$ c: R
    , t3 e! m! n+ a6 v/ Q- s
        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.
    ; ~1 l! o, Z, O& J! D
    7 B$ a- m7 G& `; p5 S5 \  e    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).8 \0 E7 }' e& k3 I) ~8 V& m

    ( h3 p: z& d$ T6 M, c1 MWhen to Use Recursion6 g: K) R1 C4 b

    3 V0 k3 Q5 m% B, R$ `    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
      n1 Z8 h, X+ M4 Y% h1 F, R7 B* W% }) X3 Y' t" w2 X9 d
        Problems with a clear base case and recursive case." `' x. f  s3 X

    ) Y* V: Q: X* Q7 w* `4 i- }$ V$ tExample: Fibonacci Sequence
    ) `' ?8 _% O7 }/ ?+ a& s( K; X) [3 y1 |8 Z1 Z0 J* g. R
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    9 b4 k/ X7 |& f0 _5 N4 z; z) e& F' b
    " n4 ~' r+ y4 r    Base case: fib(0) = 0, fib(1) = 1
    * r+ C" d1 d4 P; W' J  N) ?5 C2 w; X( C
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
      j  {4 o' U" C2 b: H/ t# @% w8 _) F2 r" n2 ^2 q
    python( B  j+ k. C4 f/ R7 i) X
    , U9 k3 Y6 [8 ?

    + E  t) g4 I' ]def fibonacci(n):
    + c! y/ s, F+ L    # Base cases
      a& ?& E' i# ?5 @3 ?# x+ J    if n == 0:) _, P+ J3 ]$ S1 c
            return 0# t8 O( d4 `0 G
        elif n == 1:" l3 l- O1 r3 B% b) y
            return 1
    ! `& M+ o  ?: O6 R7 D  Q- r    # Recursive case3 w2 P. `. }$ b6 z% S' Q1 ]
        else:9 A3 T- q* f1 l3 V* J9 s
            return fibonacci(n - 1) + fibonacci(n - 2)- a* Y) s4 Y+ k

    + e7 Y+ H; P& ~) r# Example usage
    - \, ]# o( V9 i" _' w) f+ S- X6 F7 ]print(fibonacci(6))  # Output: 8' D9 d3 l* s5 I& r- O+ q

    , a- P2 N+ m# ]; d. T2 VTail Recursion" ?) S, y3 A) k
    4 }# J' i. i4 A4 e8 q
    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).
    . v0 e9 U2 E) v/ D6 r8 F! e& d3 P2 K- K
    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-11-5 23:29 , Processed in 0.032708 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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