设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 7 n$ R" v! M* }, Q6 ^
    + A& L3 w9 M( \! ]8 s
    解释的不错6 A( C. |1 `( @# E; O

    - O. Y7 V4 k, \! t递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    1 ?6 P+ f, h7 K- p$ n8 \. F. V. p/ y
    关键要素5 ~2 |- l6 a3 [7 M# v8 h
    1. **基线条件(Base Case)**& h. t" D7 B- V6 r+ D8 C
       - 递归终止的条件,防止无限循环" M7 j& p) R  r+ m0 h- G3 z
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    5 p3 A2 X  k$ [0 E2 o) |# a2 ?/ X
    % ]1 k$ B- Q" O* Z6 @4 y, C, g2. **递归条件(Recursive Case)**% s6 f) T. h# T9 w
       - 将原问题分解为更小的子问题
      e' [8 a# j# K/ G   - 例如:n! = n × (n-1)!; {& i1 `  l( D: t. P

    9 X. f8 M4 c, R! @1 }4 r- U 经典示例:计算阶乘
    : ^8 Y/ }1 r! v( o& C% l) Q0 @+ M5 ypython
    6 M* \% w" `2 d# {8 Q: udef factorial(n):9 m, E2 {4 j# ^6 T3 H* C& j
        if n == 0:        # 基线条件
    / g: p! Y4 E! b        return 1
    5 I( b2 f0 Z$ j# r) b$ b) J    else:             # 递归条件
    , z& B! w4 S6 B2 w( r; I        return n * factorial(n-1)4 Q$ I% P! T# F& o4 r, R  m
    执行过程(以计算 3! 为例):
    , Z! N3 v& }- `6 y% S" _  u' gfactorial(3)
    % m! w8 v$ l! {$ o9 q) Q- z- N3 * factorial(2)
    $ v8 p/ G+ |4 e  u/ x6 ^8 d3 * (2 * factorial(1))2 P9 F9 b/ s$ G. N3 w  b
    3 * (2 * (1 * factorial(0)))# x0 a/ k3 Z1 }4 w; e
    3 * (2 * (1 * 1)) = 6
    1 m' d! R- K* C0 i! i* X. N$ z: o6 K3 I4 E+ b1 y
    递归思维要点8 u3 R+ m2 ~5 Z
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑- r; @4 [) D1 C( ^5 i
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    " j- O: z6 ^: N2 J7 ?4 e* v5 i1 A9 ^3. **递推过程**:不断向下分解问题(递)
    / W+ n% a/ w" c5 J* ~4. **回溯过程**:组合子问题结果返回(归)
    5 p$ `' Z3 m2 Y- r
    ( ]4 |% S# a, q; r 注意事项
    , z* p9 }2 x+ C- W1 w% E0 J+ M必须要有终止条件+ ?7 j1 G: {& ]( r) v
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)7 ]* }! w$ j8 H. F9 L' z- g7 F
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    $ _( W) Q) Y& V2 R尾递归优化可以提升效率(但Python不支持)3 J  o/ B# i5 z) {+ H

    $ J# f& N4 L  o8 n1 ?# s 递归 vs 迭代' r. e% A. N1 ~5 [' b
    |          | 递归                          | 迭代               |
    " g" e8 w4 N5 w6 @. J: e* G|----------|-----------------------------|------------------|
    , I$ N' U" X! @9 A1 g| 实现方式    | 函数自调用                        | 循环结构            |6 C  m. @7 {9 z, M  I9 Q
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |& q/ R( ^/ h  [& H/ @. n
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    5 _% h, I" X1 ]3 J| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |' |9 c1 S# p+ J# f$ c! V& ~
    3 F; T# g: u, a: G- C, u
    经典递归应用场景# R$ I, p# v. `0 B" o6 F
    1. 文件系统遍历(目录树结构)
    ) }- j* x4 a  i! b2. 快速排序/归并排序算法: F9 P, B. q3 F, d. |
    3. 汉诺塔问题
    ) c6 b# F' h3 e1 ?4. 二叉树遍历(前序/中序/后序)
    * v& Z/ _9 L, u  i5 q5. 生成所有可能的组合(回溯算法)
    4 ^- I9 z6 ~$ g3 B4 d% }) P3 `7 q; e5 x
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,+ i3 d9 A, m4 E0 P6 p
    我推理机的核心算法应该是二叉树遍历的变种。
    " I5 e) k# ^. x: M8 ?9 i  y: i+ f另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    + E$ t! m: q* H. vKey Idea of Recursion; [) D* u9 ?3 }
    # E) ]' b! l8 y/ y% F' l) N& W
    A recursive function solves a problem by:) U9 v1 r! a: {

    . S+ S3 H5 P6 l7 N' [+ e    Breaking the problem into smaller instances of the same problem.
    % ~: X' c+ {% T
    0 l2 y. `$ {+ R7 `# d( R    Solving the smallest instance directly (base case).
    8 y- {& @$ y0 w% q# m' l6 \; q
    ; c+ l5 X# x% w" f    Combining the results of smaller instances to solve the larger problem.  U8 `2 m, ]  }6 ~

    0 T, u% w& O8 }- UComponents of a Recursive Function, r( c" r! X. w$ z7 `
      G- S4 v) |: ?1 \% V) w
        Base Case:
    1 _! x8 R$ J3 j, l4 E
    4 B3 y& V$ r8 n1 |        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    0 w6 r- }1 d, G5 \& L) A7 `0 P+ m7 J+ P9 P; q- \
            It acts as the stopping condition to prevent infinite recursion.5 d! }& j6 E: C7 y

    / l7 B! V0 Z8 v4 n        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    2 H  T9 @5 O  z
    : d) X, D; {; x0 I8 A) T% R3 J    Recursive Case:3 s. R/ E$ I. [, l4 j! E1 \
    6 ~# Z* q" o4 y% ?% R, V
            This is where the function calls itself with a smaller or simpler version of the problem.
    1 q. z6 }, ]  M( ^$ o# o4 @
    9 j( M) j1 T7 I        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ; d1 O1 I. Z# ]2 _7 p; R
    3 E4 i0 C) c% s; z- k: U- B4 v6 hExample: Factorial Calculation
    " I( d5 k3 m9 y
    . V" `( h0 ^! y( K5 C( T: W$ GThe 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:$ Q4 X2 [/ g) n# b% V
    6 ?! B+ Z' ]$ x- S- d4 Z
        Base case: 0! = 1
    * e7 Q) G" n9 ?% f5 @0 ]" X: Y7 Y+ W& n3 z$ G6 b1 k8 \# Z
        Recursive case: n! = n * (n-1)!7 T2 `! \- @+ `1 V5 d
    * R& s5 r0 t- I
    Here’s how it looks in code (Python):
    3 b7 E6 s/ W! Zpython
    ' A8 d$ q4 \& t% b
    3 R! m* j% Z% u6 A4 ?
    . }" Z& V" h: t; [, q" pdef factorial(n):% m, G/ |; p2 s% J2 w1 p
        # Base case
    7 D6 g: a" a& v/ y' _3 I8 h/ R9 x    if n == 0:# x8 e  N; h: V* O
            return 1# Z! X4 q% t% s" O
        # Recursive case
    : ~$ d, P; O5 A  O) `7 n7 [% e    else:0 Z: i. S* p. }
            return n * factorial(n - 1)
    6 ~: B8 ]5 l" P4 q& i$ e% S
    / U/ J6 C/ ?' X, z8 A" l( y8 l" U1 P# Example usage
    7 v! g1 B3 c$ |" N! l9 kprint(factorial(5))  # Output: 1209 ?- ^7 E3 P) h7 |1 ~- A4 D
    , e) X0 x- {* D7 z' G. u1 j
    How Recursion Works8 q  n" C7 m% k1 ]5 C
    ; F+ I0 z: A  p+ ]
        The function keeps calling itself with smaller inputs until it reaches the base case.
    7 b7 E, i% z. K+ E- i: R1 a
    * H4 i2 ^8 X; E& t( T    Once the base case is reached, the function starts returning values back up the call stack.
    2 y3 Q% g; X$ N! q# c" E4 b8 T6 u3 E3 X" ]3 \
        These returned values are combined to produce the final result.3 N/ s- l6 s* Z

    + b) c' J/ }- j  W( c, M& TFor factorial(5):
    1 X/ T2 M0 o) o" Z, g
    ! S6 C. H! c1 `: g
    2 @  W& C" J1 sfactorial(5) = 5 * factorial(4)# o4 w" D, t! K6 r
    factorial(4) = 4 * factorial(3)
    7 ~" X  F: n  Q3 \4 Mfactorial(3) = 3 * factorial(2)1 S1 ]$ E# }/ d0 m6 o
    factorial(2) = 2 * factorial(1)
    1 g0 a' M( e, }( c8 g" X: r# Gfactorial(1) = 1 * factorial(0)- j" l7 L) `1 J7 W
    factorial(0) = 1  # Base case  B  Z* Z3 f+ l
    4 b) K' {1 F8 n8 `1 l
    Then, the results are combined:# H# W  V8 n6 r1 C/ p2 \  K
    ( i+ [7 p6 R1 V5 F8 f) M9 f! |2 B; B

    4 Q+ \/ t6 C( [* x0 |factorial(1) = 1 * 1 = 1- w4 R3 b2 t9 u5 ?$ ~& {
    factorial(2) = 2 * 1 = 2
    , `! i4 d* T7 L# D  wfactorial(3) = 3 * 2 = 6- _9 L! K2 q  z: Q- ]
    factorial(4) = 4 * 6 = 24& l! w* b1 i8 a, f% t& P7 _) K
    factorial(5) = 5 * 24 = 120
      O6 T% S3 j! h  p& _6 g0 ]; b9 C- F
    + [$ O: E2 K+ P" D- SAdvantages of Recursion
    ' n+ u, K3 Q; l% ?+ @1 j. [3 m: P1 J' @( 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).
    . {4 C) |4 W7 M6 Z2 d2 m! K1 m$ H' |) ~
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    / D8 k7 D5 F9 x# j$ s, ~. r/ N) h0 m( k% s
    Disadvantages of Recursion$ v3 g. Q' {# J, R4 \: i0 H
    - h; Z4 S* {& 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.& R8 k7 D$ a" c

    ' G5 T6 E# Q. T9 F    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).7 A0 L3 t! U9 E" f( S  z5 O

    & [- H* ^, W# e( i& b, FWhen to Use Recursion7 o' P5 M! u1 Y
    ! [' O) g2 S+ @! z. [4 P
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).' G' f$ d1 Y: [* G3 d$ a
    ' {; Y4 `5 K* B1 N2 ^* U5 z7 L
        Problems with a clear base case and recursive case.2 W+ d0 c0 Y8 p4 B1 H

    - ]9 M! Y% J8 x' f' wExample: Fibonacci Sequence
    ( n/ {! X/ k- u; _% ]  ]$ ?& s  @' L# i& \# ?. F
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:+ U' X$ C2 ^+ U6 }

    5 J2 k4 L1 f% _/ Y/ |/ G' H8 i6 Q    Base case: fib(0) = 0, fib(1) = 1
    6 Q2 ~2 G) [  b4 \
    9 z$ _) F: M/ ?" Y1 |% f    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    " j/ n/ p4 m. {" Q" A2 B7 ~0 Y/ x) k$ I4 O1 y' B- K  \) _
    python2 q6 j' ?0 X9 }
    ' b/ B; u+ [' U& n6 H
    7 ?  \. k3 w: G, [5 c& J- g( }
    def fibonacci(n):
    ) R& s- Q$ ^/ w    # Base cases( Z( {/ l( J  \, T9 ]+ m
        if n == 0:
    + t! K: A4 O$ y9 r4 }( P        return 09 b6 I1 F1 M3 J% M* ]
        elif n == 1:1 w6 x( [) t, q/ J. @; s
            return 1' a6 v$ R- ?9 u7 A
        # Recursive case1 k& C* Q& m! d  z( P" w
        else:5 J/ G! {% ^, [  v. d- A7 |3 E
            return fibonacci(n - 1) + fibonacci(n - 2)3 l0 R, d* k) d0 C6 @

    / |2 g; {8 N6 I- r1 K3 G( U# Example usage
    & }+ x' o; M* r$ M  Q7 b' U/ dprint(fibonacci(6))  # Output: 8' e  B7 a; p# @9 I

    * k% G2 h* \, u; V7 d6 M, dTail Recursion
    / v* w+ C/ M" L0 J
    6 [4 n* D; w+ w7 C* n5 ~* {  v, pTail 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).
    1 U, F' ~5 F" o- j+ x; X2 k) t: V% T+ h4 m. x
    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-7-17 07:28 , Processed in 0.054967 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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