设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ! j# c7 }! a  X" j; [8 x
    * o9 w% q: L+ i6 P0 {5 Q0 Q解释的不错
    6 m1 E* I2 y% X9 h
    ' Z: p3 H/ n0 Q& o, p递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    $ o! A* w  m+ o& V8 g% p8 l% }6 g) [/ N# w& w3 \0 F) ^& q# Y
    关键要素
    0 T# T2 T; \3 W1. **基线条件(Base Case)**0 @0 _- T; ^5 Q
       - 递归终止的条件,防止无限循环6 p' w. z; d8 ]; b: \+ t
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    8 n8 H- `& B- U) ~: q  j* B4 E& a: ]$ i" z& u# f% g' t0 o
    2. **递归条件(Recursive Case)**3 a6 {' f5 v; a% y7 x7 X
       - 将原问题分解为更小的子问题
    . t5 }) ?- {- _& q3 l( W4 Y) k5 ~   - 例如:n! = n × (n-1)!
    & p7 b0 k# N2 H
    + S& O; A+ d) i. s 经典示例:计算阶乘
    & B/ G8 s' }" wpython* O- E7 G! g- M2 c
    def factorial(n):
    / D, L5 e) E  O6 @& A$ s0 x4 K: Y" H    if n == 0:        # 基线条件
    ) y2 {, ~& J6 T* |$ Q5 a        return 1" z, V& C! \8 E# W0 r% h
        else:             # 递归条件
    : W! a% s6 M. s7 \( `        return n * factorial(n-1)
    2 g( h1 ?- T) r1 H0 x# Q执行过程(以计算 3! 为例):
    " W! `$ ?+ {* }" F' wfactorial(3)
    ) v9 x% ?! M9 ?) A; j3 * factorial(2)
    0 D1 b- g+ D( f9 C# D3 * (2 * factorial(1))
    6 s3 o+ u$ a0 c1 I3 * (2 * (1 * factorial(0)))! z: E* m/ K. M; o1 F9 h
    3 * (2 * (1 * 1)) = 64 p! _& c# U) A2 O$ x. o

    * _, @5 ?: y, B/ F" w5 T 递归思维要点
    8 q0 h9 ]/ Z; j. p8 w: b1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    , L# N7 e2 {( J4 y+ x/ A& ^1 k2. **栈结构**:每次调用都会创建新的栈帧(内存空间)" }8 s& r* {4 P& I, R: O. q
    3. **递推过程**:不断向下分解问题(递)
    ! n2 G) A( g' Y. y! [& z4. **回溯过程**:组合子问题结果返回(归)
    & I0 c" Q! _1 ?# r- |  A8 e
    7 M1 T$ j3 K; m- T  y, f0 \9 H 注意事项
    8 s& o" c! W  P) m0 [. C必须要有终止条件
    5 K9 y' H7 }9 ~* ]1 L递归深度过大可能导致栈溢出(Python默认递归深度约1000层)) e0 B$ m4 B& ~/ d# z5 F
    某些问题用递归更直观(如树遍历),但效率可能不如迭代! e4 Y1 ]; R" }8 o% I7 n
    尾递归优化可以提升效率(但Python不支持): M8 S) Z6 a+ a# s) @: M8 k  R2 J" o

    - X& {' K  q! R4 k  J4 o, o 递归 vs 迭代4 @. B& z3 i) Q1 U- ^
    |          | 递归                          | 迭代               |
    & j0 Y/ I+ P& ]/ H" d3 J' ?|----------|-----------------------------|------------------|/ \# c+ X4 c% c
    | 实现方式    | 函数自调用                        | 循环结构            |
    1 {3 ^; m( b: V4 B7 i. a0 _| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    4 s: T  L5 V2 o| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    7 m# X3 o4 ]$ I- F, _9 ~2 }| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    5 R; `9 T: I$ x2 C$ o1 `' z
    8 Z8 t8 _7 w5 ?7 k" l 经典递归应用场景
    - Z* ~3 w" `3 @) Q& V, R1. 文件系统遍历(目录树结构)
    5 k6 f4 j$ [- o2. 快速排序/归并排序算法( v$ s. C! D; q, ~1 P
    3. 汉诺塔问题
    8 N! l; I3 D/ \7 X4. 二叉树遍历(前序/中序/后序)( T' N4 Q1 I% V/ g) o
    5. 生成所有可能的组合(回溯算法)
    * S& K, v2 I. m$ r+ N( d: [- J) _1 L( z# B. f. P
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    17 小时前
  • 签到天数: 3111 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    9 s5 x4 I/ H, o, X我推理机的核心算法应该是二叉树遍历的变种。( Q$ r: ~; F( B9 e$ d, b/ Y
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:3 A3 f+ W$ N( m) b9 @+ I* q' E
    Key Idea of Recursion
      }# q& a: Z4 R2 O
    % a# m8 I. Q& {A recursive function solves a problem by:
    3 ^/ ^" N, }# V$ p/ B0 ]& W- T$ k5 v2 A
    : @1 ?8 L; q6 ^4 i% c    Breaking the problem into smaller instances of the same problem.
    / q! v. Z' i( ^
    ' K3 s/ i' x, L9 W; _) N5 I    Solving the smallest instance directly (base case).
    . w  Z' n1 w$ A' j) A- g0 d, a8 Y+ o
        Combining the results of smaller instances to solve the larger problem.
    7 r  Z; z# I+ _& M4 W% e' O- _, v! Q. ^; W- v5 ^; F
    Components of a Recursive Function/ M) m% O% {; L; K# ?3 j) `9 l

    ! }. ^: c. A8 b# U) j    Base Case:
    3 N/ ~- Y6 h6 Q  x, E$ U3 A+ l' k5 t& e" z6 D0 A
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    / D. {# |1 ?9 v% p5 r. r4 H; p$ I6 q; P7 @  j" b
            It acts as the stopping condition to prevent infinite recursion.
    2 [4 G9 J& u, a3 D2 L6 R2 z# Z" O: m. {+ k/ S+ `& z
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    . W( ^5 l3 h! V. \) [: f
    + g4 j! c$ i1 A/ Z    Recursive Case:
    - [: C9 x/ f6 I* A: F( S1 w% Z4 P6 w/ J+ V0 Q. C
            This is where the function calls itself with a smaller or simpler version of the problem.6 q; H+ l1 U' T7 h$ x

    - u- n' S, o7 v" a/ j        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).- F: L, B& Y! s; M

    & l5 l: E* Q; P8 R( u& X2 OExample: Factorial Calculation( ?! f* t  `% ?+ B- R, O0 ?

    , u) `6 n# V1 rThe 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:
    3 Y# w: g. C  g& S; l, k( b! l. u8 J( i' b" P2 b4 z
        Base case: 0! = 17 W5 u3 q: o7 R2 N" ~' X

    7 Q7 P1 p) p/ T# j$ X    Recursive case: n! = n * (n-1)!! |4 ?/ X* P; b+ ~

    1 X1 B  z/ n; D4 g) S$ r; C, N& MHere’s how it looks in code (Python):* W. A1 |1 u& W  ?+ O
    python
    0 j5 l9 x: g1 B; ?# z6 o7 W2 W' o' h8 c; N9 o+ K5 A

    , |* u4 m. O) O- t# P: ~def factorial(n):
    ; k' ^: W" i+ K1 ?    # Base case
    : i( y1 [, a" K2 B- J5 L    if n == 0:
    6 L0 Y( r6 O! R        return 1, z8 {) A" H! o1 F  K5 a/ ?
        # Recursive case
    5 M+ m$ x% J+ X' ]7 o& B    else:
    6 B3 _) I. g) ?2 s( J; I        return n * factorial(n - 1)
    % K  `& O+ H5 J: g3 r. G3 P( c4 _5 P  Q2 K& ?! p9 S
    # Example usage2 A- b, A6 @& y4 R2 ]" X
    print(factorial(5))  # Output: 1202 J! v) N1 C3 _6 o. v

    2 ?1 S( [: _9 ]& V) FHow Recursion Works( _* a& Y: E; a: a1 G- @7 z

    ( G. P3 i( R3 ]  V    The function keeps calling itself with smaller inputs until it reaches the base case.) M! x8 e9 ], [6 n4 [9 X# k
    6 T7 R( F: Q0 E) P! c- s: l5 k
        Once the base case is reached, the function starts returning values back up the call stack.# Z9 F; _  c' n, B1 x& p
    8 m2 f. \7 x5 T
        These returned values are combined to produce the final result.2 p( F) K- h3 o" L5 \% {' t
    : m/ u' W$ R; W. p1 P4 o: L! j
    For factorial(5):
    " v9 ^; C+ }7 t! W, c) c, b0 `# A- t8 O: r: T/ L
    ; S" R3 U& A0 i) B# m* p
    factorial(5) = 5 * factorial(4)
      I* Y2 r  v5 Jfactorial(4) = 4 * factorial(3)
    + M( _# ~- ]7 v# Hfactorial(3) = 3 * factorial(2)8 Z, U# Y$ |% i5 b7 f8 A  M1 O
    factorial(2) = 2 * factorial(1)1 z3 \0 M4 B# H" F) `5 \
    factorial(1) = 1 * factorial(0)
    / u7 t4 w2 w3 F! jfactorial(0) = 1  # Base case
    ) Q( h. j) z$ k9 P
    6 B; J5 S; x" B  _1 gThen, the results are combined:. {* U( I% \. S7 r! ?% H# `
    - k$ t% B" |2 K2 K8 u
    4 V# N8 ~( H+ [0 x) J4 {1 U
    factorial(1) = 1 * 1 = 1
    5 C  ?% _2 |& M. X) n- i4 efactorial(2) = 2 * 1 = 2
    2 D, j; ~0 I& z8 G0 \6 E2 kfactorial(3) = 3 * 2 = 6
    , |% w8 o9 D: s0 Jfactorial(4) = 4 * 6 = 24
    * y4 Z1 F: p$ ?; Qfactorial(5) = 5 * 24 = 120) J  u% R2 R6 x5 o
    3 Z( l! N. w4 W4 T9 m8 V
    Advantages of Recursion
    / q5 ^/ y" q$ l' _( U" r
    & o! ^! `7 ]% {  ?$ ^1 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).) Y- ?# r% x" ?+ R& W
    6 s9 y* D1 H2 y! Z! N
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    7 O$ L3 e, @6 y6 b: c' B4 H
    0 S4 y7 S* p* Z2 HDisadvantages of Recursion
    ' D. a' T1 M8 @. {5 v. S- i- k" f9 H" q% u6 i% u
        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.
    + I6 K& s. O+ O, Y# g% g
    . d% P2 g. i& J- U4 V* u& m9 o    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    9 D+ C  x5 S9 U! k) e3 b' D  }, _
    When to Use Recursion+ i8 [1 k/ H5 A# |8 F9 ?
    5 i9 W2 H& A; j2 w
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).0 q9 U" C9 @/ l, t7 O6 k3 w
    5 u' s1 h0 d- \3 |7 ~
        Problems with a clear base case and recursive case.
    ) p# |, N: l+ S* q" K' e. H/ f' E, r* I; w6 M9 f: w" ]
    Example: Fibonacci Sequence2 ?$ W/ @+ U  i. p) [

    2 T# a% s5 v' G: b0 D( x4 DThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    3 ?, J2 Q& d% D  y
    % ?" o* C# [3 w% z$ J7 t+ v# _& c    Base case: fib(0) = 0, fib(1) = 1) p, y* y2 w& T4 z
    8 P+ Y2 A; ?* F, K
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ( W) u! W4 [0 l1 p) c# n
    # p5 ]  F6 q+ g) ?* c* f. ~4 q1 S$ upython
    : l* Y7 z0 o" C& X
    0 S3 h! Z. L+ h7 l, C. L& L
    3 w. N7 U+ r* U+ e) _7 H% ^  h0 Pdef fibonacci(n):6 {7 O* r) u+ D6 o0 V! A$ H1 |
        # Base cases* X$ ^8 T! T  w
        if n == 0:+ C' L' ]' q5 D0 O
            return 0
    0 G/ p, L( i; \4 z8 d    elif n == 1:
    + U0 z( _* W) N' o! ?        return 1
    . p- L% }1 T0 g* g- C3 f" f    # Recursive case
    7 D& z! P! X; {    else:" B! k! l* p8 V8 a% d$ O; q5 p
            return fibonacci(n - 1) + fibonacci(n - 2)
    9 p% S0 @: k, S
    - o8 J- K4 M9 ?# Example usage
      G" B; i; Q3 g, i2 a3 i! e  _  y/ Xprint(fibonacci(6))  # Output: 8/ u" O6 ]: A. Z/ |# [, P1 Q
    - i/ C) N* w: |) A# V+ d
    Tail Recursion
    * G! N/ s+ g; f" V
    & b" n% l6 Z; F9 U- H; o# sTail 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).
    : S3 Q6 W6 f* M( X; `# B5 H) Y8 l+ [  O7 {* M6 @
    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-12-7 23:54 , Processed in 0.033030 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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