设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 2 q/ {% T) l# D% h

    3 Z" l5 Y7 c: P/ c8 o3 K" P解释的不错
    5 I/ ?$ Y6 O" ^; p6 T( W
    & x0 E$ l$ B: A! `* e) A2 s; `递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ( _. k$ U% F' N" A% f. V! i4 q1 Y# m; ?: `$ i
    关键要素
    2 e& H. O- u$ e/ Q2 \1. **基线条件(Base Case)**+ P: h9 M7 s6 ]
       - 递归终止的条件,防止无限循环5 ~" Z  a' X) U
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
      [5 W9 M: A* b/ t+ H: D1 i- ^" y$ Q! r3 s& ]
    2. **递归条件(Recursive Case)**
    * }9 @: T& c( V' h1 w" [   - 将原问题分解为更小的子问题% ]1 E+ H: A2 J# r8 F* h! j) ?
       - 例如:n! = n × (n-1)!
    , f  l9 W/ x( S. b4 k* K' ~0 N" X5 a4 `% K* d6 g7 B
    经典示例:计算阶乘+ L4 D3 c3 f+ S0 L4 B
    python
    : H0 `" m( r1 [% mdef factorial(n):
    3 R2 b+ P/ m* b' P: E% g    if n == 0:        # 基线条件
    0 A+ h1 b" N, ~6 t  Q( ?" Y        return 1
    # ?, F( u( T0 t! ^    else:             # 递归条件5 [% c7 i! P- F+ \
            return n * factorial(n-1). B! d2 `- ^0 `) {
    执行过程(以计算 3! 为例):; O6 h% ]6 ~8 _0 ?/ W- V! V
    factorial(3)+ i: o( m; x+ o3 X" H& k! \9 j. R3 l
    3 * factorial(2)* T4 ?4 Q0 Q' H1 r# p  i
    3 * (2 * factorial(1))& l  G) S% K$ x3 V7 a$ U, U
    3 * (2 * (1 * factorial(0))); n) `& o' q# k- t# R: i% j
    3 * (2 * (1 * 1)) = 6* z7 W0 g3 }- P# z" \. V* m
    3 h' W! e* N( D2 y3 |) c
    递归思维要点
    8 d+ Y; h( D& S1. **信任递归**:假设子问题已经解决,专注当前层逻辑5 i2 X% v; K" s8 c0 {+ t
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)' n# V) P( B7 y3 W# J  |* ^
    3. **递推过程**:不断向下分解问题(递)( U* h6 E4 d: Z# ~, `
    4. **回溯过程**:组合子问题结果返回(归), p( n- f4 g0 s0 z" @
    " W, Q. G% I0 z9 z/ I$ ]9 D
    注意事项
    - E4 o  h1 B; m9 ^- X7 k9 O必须要有终止条件
    9 c0 T" [1 e; j  x9 f- z递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ) E% O* \; e5 |某些问题用递归更直观(如树遍历),但效率可能不如迭代
    # M3 z$ a6 h; m+ M7 g* ]0 r尾递归优化可以提升效率(但Python不支持)
    + B1 r, U" [6 L1 q: q: p3 ~) m9 }- v% W
    递归 vs 迭代
    , I$ v/ v3 v! \6 d|          | 递归                          | 迭代               |4 S$ b- ^  O5 B$ `% Q3 {8 h7 w! a5 h2 A
    |----------|-----------------------------|------------------|
    1 J; ]7 r) F  d5 U: V) B| 实现方式    | 函数自调用                        | 循环结构            |6 P" D2 J3 ?! [5 ^
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |" S- O2 M: @$ D# j  A2 d( l8 {
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    : C. c) W5 a" n# t9 E" f" M| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |. |# ^, d8 x, \1 q$ t! |

    ) L& V/ A+ }. I9 ? 经典递归应用场景& \! G% H) l& I& e3 H0 D
    1. 文件系统遍历(目录树结构)  j/ n; ~- z% W8 p& u
    2. 快速排序/归并排序算法
    7 M9 E  u: `4 t" w" p" m8 x3. 汉诺塔问题5 x% |$ x9 h) D6 X1 D
    4. 二叉树遍历(前序/中序/后序)
    ; d- ?/ J6 P) _( ?$ ?5. 生成所有可能的组合(回溯算法). @- r! |0 D- t: a# t! J* }, D

    ( ~- V  {, c- q) o6 ?6 C+ ~8 M试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    2 l: h3 k! l7 K; ~我推理机的核心算法应该是二叉树遍历的变种。; N6 I6 _5 h, f$ z& _& a" g4 G2 X
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:, Z. _) @5 t' f6 R* n
    Key Idea of Recursion
    ( \+ b( ]* k6 D8 ]2 W! I3 `' ?& a. J- _2 \
    A recursive function solves a problem by:; w! S; Q# Z- g* \/ S& D6 y5 j& U

    / q; d  \! p# u) l  b# M, i    Breaking the problem into smaller instances of the same problem.
    ! x9 E% Q1 v' a1 D/ {; y; a8 f: i! L) t6 |& y& f6 I: A) r1 ^' o" p8 ?
        Solving the smallest instance directly (base case).
    8 Q9 F. b# a# H3 v% O" h2 w; }
    / Z+ S# }; M, D6 ]) c2 B    Combining the results of smaller instances to solve the larger problem.* @6 v+ x- f# o  y+ U. P9 }, \) U0 P

    ; y3 Y- o4 Y3 S7 ]Components of a Recursive Function3 Y7 L. z0 S; M: u
    % q7 _; D, d+ ^( Y8 u
        Base Case:
    * c0 T6 W" {: O
    + r  ~* o3 p, a! u        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    8 t( Y: \* ?. G, R* o; ]) D% H$ N2 D% R* i2 f
            It acts as the stopping condition to prevent infinite recursion.
    ' T( O7 j$ I" P: y0 \% |# L
    . T( y* s& W" Y        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ) d2 k% q* I; [0 m* y) x7 h9 S" g0 V# i3 r' w9 ]' h
        Recursive Case:
    ! p7 V$ f  A2 m# |! [1 k9 `! M
    5 \- a& \! d+ R9 x! y        This is where the function calls itself with a smaller or simpler version of the problem.! j/ S; f3 o5 A# e
    4 r- c/ X. S7 D
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ; q- b7 H" T" w: ]' g7 v
    * H* t6 R! S; T3 n# XExample: Factorial Calculation9 e# l1 B9 d9 y

    - ]2 W) x- B* 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:
    ! \; Q% y1 R. Y( E  X- c$ S! z' \6 j
        Base case: 0! = 1; }! R) o7 n* O' o
    ; {  B# r! K: `$ H& z
        Recursive case: n! = n * (n-1)!
    $ v, \5 T- o) J6 Z2 F# d! ^# J( _6 h9 G3 |& d% ~
    Here’s how it looks in code (Python):1 j' w2 u" u. {+ L+ |: Q* w
    python
    1 C- j7 a5 {. y1 ^
    . a! R6 p0 e. _4 ?9 W/ r! |! ^# P" E% p  w7 q7 l
    def factorial(n):0 s3 W6 k7 w! l! u( z- E+ ~4 ?+ |
        # Base case
    8 F7 R" s4 h2 u    if n == 0:
    + ~: A# m- S; |& n8 Y7 W! E+ h        return 1
    3 w. j: Z: f; N% S7 o6 F    # Recursive case
      X) m6 |6 W0 F( ^    else:0 M4 p1 o2 \+ P& n' I
            return n * factorial(n - 1)1 i2 {5 U) T+ S  t3 f

    / w( k5 ~3 C' Z# Example usage2 D# l' q+ J- M' P  ]$ ?( `3 N
    print(factorial(5))  # Output: 120$ C" B5 d& W0 y: N6 m0 i7 C% t

    5 v" R# q. i  eHow Recursion Works8 [0 E7 E& ]+ y: X4 |
    , D' Z1 Q1 |2 o+ z( X: t
        The function keeps calling itself with smaller inputs until it reaches the base case.
    3 e1 |- |2 g) H" ^% L# \+ e0 G4 s+ V! k  b+ V2 ]
        Once the base case is reached, the function starts returning values back up the call stack.
    ; \% {2 d2 |; ]; ]
    3 ^0 k, C/ m7 H8 z8 ]! ^& E0 @    These returned values are combined to produce the final result.1 W) p& j8 J1 q4 k+ N
    ' e( |: o& r0 R8 z  i2 K$ B
    For factorial(5):# o8 @, Q) i' C' I. \
    3 O9 V5 Q$ L; A* W, P- a9 W
    1 d. n* w" B/ T* |) x7 z: H
    factorial(5) = 5 * factorial(4)
    0 l+ o) E: L7 X3 c! jfactorial(4) = 4 * factorial(3)2 i% w6 y$ }1 y0 m: e) S, r6 [
    factorial(3) = 3 * factorial(2)
    9 K: j" H+ Y- B8 `factorial(2) = 2 * factorial(1); I0 Y% r9 p  ]4 }' f
    factorial(1) = 1 * factorial(0)
    7 N; z" S$ p; H1 R5 {factorial(0) = 1  # Base case7 i4 Q3 G2 A7 `/ r$ ]/ j  ~
    % q" Y- p$ }- i8 M: w
    Then, the results are combined:
    # W  e( e4 v. v/ A2 A# L" g1 v- q0 v/ V& f2 T1 ]. M

    : D4 t) D. k! G) K& Ffactorial(1) = 1 * 1 = 1
    1 K& B# y% L! ?1 _" C( Z* n$ j9 @factorial(2) = 2 * 1 = 2
      c% y  d# Q# i' x: v% K% wfactorial(3) = 3 * 2 = 6
    * Q! Q2 n$ Z$ p7 ~; Dfactorial(4) = 4 * 6 = 24
    4 B- S/ ~, q  C/ l  d: }8 D- afactorial(5) = 5 * 24 = 120+ O) _7 Z: ?4 C: B. O

    2 }, d# t& ]( d- o/ JAdvantages of Recursion
    : [# v. o% V9 q& x, z
      {9 `" {1 E* d$ J* w+ Q  m    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).9 W# l1 U  a/ y" A+ I4 S
    2 K- \& C6 m: V  S3 `. s' r6 K0 Y
        Readability: Recursive code can be more readable and concise compared to iterative solutions.& K0 S8 W  s/ _+ @: i" i

    ( j! J1 F; `4 U; p  Z4 oDisadvantages of Recursion4 L" p6 {9 s7 N7 s  }0 f
      @- W2 [' a; ?! B
        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.
    - k, `6 Q5 Z( E/ q, a9 r) d8 ]) D6 M" \5 N
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    8 U! t( b6 o3 E5 ~, S+ V- l3 y0 U1 W9 {+ q
    When to Use Recursion! R" R3 e# a/ M1 i2 [6 l& B
    2 \. V/ A" i: }& X# ?
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).. W, B) R' y) m: |4 y4 [! t8 h
    6 I0 Z  D$ h8 u8 P) _' b* h
        Problems with a clear base case and recursive case., z% u& H" [4 L% j, o' N

    % N4 c7 w  L7 q. @% wExample: Fibonacci Sequence5 h# M9 e& i8 [$ N& Y
    , h+ K( Q# a$ G* e; q. Q; t5 f
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    # \) `9 A, s' M  O
    - R" `. j" A7 l# F1 l$ i; E4 Y$ x    Base case: fib(0) = 0, fib(1) = 1* u2 c+ v9 ^9 L- h  n7 G3 o
    " D+ @- d# F4 A3 q$ _$ C% t' Y' c
        Recursive case: fib(n) = fib(n-1) + fib(n-2)2 P. d& d2 I$ ]* }
    5 l9 }2 m7 c( ~
    python
    1 ~% B2 x  h% E+ ^: l# n& [* F1 [% z

    # J1 `/ b8 w7 Tdef fibonacci(n):0 `9 w* _4 b  I
        # Base cases% d. h* {0 ^* t& j/ @3 S3 i
        if n == 0:
    1 q  o7 ~7 N" X        return 0
    4 O0 j# n1 b+ Q3 d& V  P    elif n == 1:
    ) F  O% k" c3 I& ^7 I8 s        return 1' b4 q: I* X0 v4 e2 v
        # Recursive case+ O/ V/ c6 [" r) S: G' a# f
        else:
    / S& s/ f. _0 A* M/ W' ~6 o& ]  ?$ S        return fibonacci(n - 1) + fibonacci(n - 2)1 e% D: i' T; A4 p: a8 Q

    7 _- d* C6 N6 D4 k# Example usage& B: g& Q4 ]; I/ \9 e
    print(fibonacci(6))  # Output: 8
    ! L% r. i& O9 C0 t7 \' s% @" Q( Z" T9 @, }5 S8 f; Q
    Tail Recursion: v/ {; n- p0 J4 _' i  L) Q6 i
    0 q- A. K: t6 O. L3 T
    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).7 E5 m* E1 |: \

    / k" {0 t0 ]& i3 YIn 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-12 06:02 , Processed in 0.039357 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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