设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    1 d. d/ Z6 Y3 S0 O0 F' U: P- n7 Q0 g: ^3 w
    解释的不错
    ( r# k& m- Q# l3 Z' [# T1 ^! Z" h& y+ E) ~, q
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。& p" k* {. \0 A; P# D1 C% a8 g

    4 t7 F0 B# I- U 关键要素# A% ^7 ^- C+ M& t2 v; f$ N) O7 Y
    1. **基线条件(Base Case)**
    " y0 f9 A0 e5 N* s8 e& }   - 递归终止的条件,防止无限循环
    3 L- K7 ?2 G8 {5 e. |! b   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    . B, O$ b) Q7 P6 s0 W; W( ?2 K' O& @" n( [) u' ?
    2. **递归条件(Recursive Case)**
    , g7 I5 s# {$ q& m- P   - 将原问题分解为更小的子问题% p" w+ g. m% b: @5 e: f8 i
       - 例如:n! = n × (n-1)!5 y5 d$ b0 x/ I: M6 G' S

    ' `  W# j7 H3 ?5 H8 {/ J4 l/ D 经典示例:计算阶乘
    6 D- H: a' G$ t2 Tpython4 W; b6 k7 a3 \: r( I0 N! H" I$ t
    def factorial(n):& @( v: y  W* K3 {9 y& r
        if n == 0:        # 基线条件
    + u1 B$ e. I  l# I' C, c  b! D        return 1) U9 z! S& h6 e( W
        else:             # 递归条件% Y" n) U# T% k1 s" L
            return n * factorial(n-1)
    8 |2 k7 U3 o! }7 L9 A. P' x. Z执行过程(以计算 3! 为例):4 Q3 h; A$ y! \; Z. M
    factorial(3)2 H) S# a/ n4 ]) M# Z
    3 * factorial(2)# g0 B" Z1 g  w' F4 x$ I# L0 F+ v
    3 * (2 * factorial(1))- D" k; O) Y1 F/ S
    3 * (2 * (1 * factorial(0)))# L. `. y" @5 o3 L/ t6 a/ H
    3 * (2 * (1 * 1)) = 6
    1 m' ?4 U" X( J; a( w/ C6 M! K1 o3 t. @% e
    递归思维要点
    7 a7 a# B) `( Y9 X1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ) I2 L; @" F: V2 \2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ! o2 _! ^- Y) P7 l0 A# G3. **递推过程**:不断向下分解问题(递)
      M7 O4 K9 v8 }* ?9 w+ p1 @4. **回溯过程**:组合子问题结果返回(归)
    1 E! H' Y+ Z& k  p; E2 [/ p+ l" a1 R. i4 L- U
    注意事项
    ' b. h+ I5 E! U& `% w必须要有终止条件
    # d0 S0 B* q" D0 o  L6 I, H递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    9 d$ @  ~6 Q& z3 ]4 D某些问题用递归更直观(如树遍历),但效率可能不如迭代
    1 R9 X9 q- n! I4 K! D尾递归优化可以提升效率(但Python不支持)
    & [& g/ @6 ]6 @/ B* v4 T
    ' N0 f- e% N% u* C4 ~- ?# G 递归 vs 迭代
    - i3 J0 q7 ^+ z- y) z* n8 ?|          | 递归                          | 迭代               |: P. D( V2 ]( }$ J
    |----------|-----------------------------|------------------|+ ~- R$ _: J4 u0 m- D2 W, B$ G
    | 实现方式    | 函数自调用                        | 循环结构            |1 @2 x+ C1 x, l* o% b. M! Z
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    2 J0 U! a% _. b2 Y8 k. b| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    , D- |' O6 K/ Q| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |$ Z3 H$ p0 I- T

    / \% j. T4 ~: o( {6 a) m* e& E3 Z 经典递归应用场景
    - t6 F  u2 u- L- \! q+ ^& n1. 文件系统遍历(目录树结构), X  }+ j/ ?  C. T0 z
    2. 快速排序/归并排序算法% ]; D+ {# C" |9 t& x8 O
    3. 汉诺塔问题: o0 _! l" r: \8 N7 ^
    4. 二叉树遍历(前序/中序/后序)
    & m$ r+ d3 I  T0 B( K5. 生成所有可能的组合(回溯算法)
    5 G8 s) {" W) g. T: |6 k
    . K8 g' w9 U* s0 E. V) G' W试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    昨天 22:05
  • 签到天数: 3125 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,1 t) n& y1 t* s
    我推理机的核心算法应该是二叉树遍历的变种。+ t& D3 A3 Q8 M2 _& q* N( ]
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:+ L; }$ L2 H8 z7 t" K1 O" C& ?+ p
    Key Idea of Recursion
    7 ]; ]2 J7 C9 f" g' g
    4 W: X6 [# }; V* S  b' |A recursive function solves a problem by:8 z$ B- {- w* j# w. `

    9 A, ?0 m7 L3 [, U$ n4 E0 X    Breaking the problem into smaller instances of the same problem.1 W( [+ k* q, Z( [+ d

    ' K4 w. `1 h& H    Solving the smallest instance directly (base case).
    . i! b+ i/ M- Q& g0 j. S. R8 Z1 ?+ u# _% T, _" I* @# ~
        Combining the results of smaller instances to solve the larger problem.
    ; E' b8 G, r+ e- b6 D0 f  J
    3 D, l. M* o1 H8 h! k% B5 b3 vComponents of a Recursive Function
    9 n" n9 ]  H% n0 w7 @3 y$ j+ x
    ) x, H- g, F9 o' |" S+ t6 `  z    Base Case:1 d: p0 y( X3 a' O  v
    $ W0 j" U% U+ z1 v# b4 r/ V
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.  l# c' P: j7 [+ ?9 h3 }& @
    ) T9 A- W& t" B7 b( B8 x6 P
            It acts as the stopping condition to prevent infinite recursion.0 k2 E0 s$ x! c3 O4 \9 j
    5 S8 f# Q: q  m" Z; D6 C
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.8 w- z% v4 R4 Y& J4 ^! V! J9 i! C4 b

    ! O, n$ T  _6 W7 ~; r; p8 `- a' J    Recursive Case:+ P4 s9 X$ M+ W8 h2 K
    2 y9 z4 t6 T( Y  |( u/ E7 A+ e
            This is where the function calls itself with a smaller or simpler version of the problem.$ g8 W, _. ^% Z# Z, l$ G3 S
    6 A% L8 E4 K6 k
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    & n9 U* l+ y9 |# ?! |
    " I4 `; s9 r1 M# S# V& NExample: Factorial Calculation
    ! j& ]) G6 a5 C
    $ f1 w+ Z6 Q( w" c2 h$ F2 j. JThe 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:$ x5 `; I# ^) O" L

    , \: K8 X( t. q    Base case: 0! = 1
    1 B) w( e+ E9 I2 F* S# T* p' C6 D0 k) [9 ~$ r: T$ y" i
        Recursive case: n! = n * (n-1)!$ t% P4 ]* ]/ Y3 r+ I

    6 u  x2 ]0 r! D/ mHere’s how it looks in code (Python):$ E% ^* k/ A  @6 [4 ]- ?
    python
    / m  n: p; Q% W- X2 [: ?0 E7 G: B* E' Z; }. m1 X
    , `. y2 P2 M, H, {& g$ r# ^* f
    def factorial(n):; y* n% H* ~( u( m: D
        # Base case
    9 Z. ~+ Y; v9 a    if n == 0:
    4 E# `% k# _- h& u! ^" j        return 14 {1 V6 t5 V1 M" V) m
        # Recursive case! x  l6 b! a8 f4 |% \9 @) `9 {( j
        else:9 w: v! r% ?* F7 L
            return n * factorial(n - 1)
    3 x7 `$ r. v* h6 d8 N8 i) Y+ T
    ( }) J/ a% e+ x" S- t0 J& ~3 `# Example usage
    ! c" j* o+ I4 pprint(factorial(5))  # Output: 120
    ) A7 A) e2 W) B/ ?$ l! N' a! |/ b9 d! r; ?! ]6 H
    How Recursion Works2 Y3 r) m- \0 u1 N' t: P9 G0 w

    0 D: n+ S& q) ?* @2 P( X% V9 ?    The function keeps calling itself with smaller inputs until it reaches the base case.
    $ Y& S" T3 t+ q( D3 A# r: v5 I
        Once the base case is reached, the function starts returning values back up the call stack.1 `6 Y- r& ]5 l. G- }4 Y
    & Q1 l7 X+ }9 m3 P* v, |
        These returned values are combined to produce the final result.
    9 P/ ]1 a4 n2 y( }& ?
    6 U% N* `& D0 ?6 l  j, AFor factorial(5):
    ) [) x; Z7 v8 W/ Z0 @/ n) T) b+ I5 i, Q+ f0 _

    ; J  J9 f5 @6 ?( |5 Q- ufactorial(5) = 5 * factorial(4)( y% x, m) v3 |% s: G
    factorial(4) = 4 * factorial(3)
    " a3 T2 [' f7 `; z1 ]factorial(3) = 3 * factorial(2)
    . H  k' R3 x: Z/ sfactorial(2) = 2 * factorial(1)( e9 \, n! R) u# E; I# c
    factorial(1) = 1 * factorial(0)
    1 k) g& I4 |1 dfactorial(0) = 1  # Base case
    ) H3 g, @3 Z, }
    4 t+ ^4 C# R7 g6 iThen, the results are combined:
    ' W$ K1 K- K$ u& O& T$ O  j7 f+ J) ]) g

    6 I, g& k# a' zfactorial(1) = 1 * 1 = 1+ ~- d  l7 O& c/ s0 t
    factorial(2) = 2 * 1 = 20 t' B; x9 x4 r) I5 P
    factorial(3) = 3 * 2 = 6
    8 J. u5 @: Y( g, lfactorial(4) = 4 * 6 = 24+ i1 S9 N+ p# f, u6 E
    factorial(5) = 5 * 24 = 120$ N! c* q4 Y& z+ L5 w6 Z
    ; J4 ^" H' ~9 P" z" W
    Advantages of Recursion
    / Y! o# M0 I8 V1 n. F
    # J! q) v) z; A4 @# \    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).7 I% a9 C$ J; A3 D

    8 f5 a" \  z/ [5 S3 x" B. [    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    * `* z$ ]; I# d: g) Z0 {; H8 q
    Disadvantages of Recursion" t& D4 S! z+ {, V  k6 b: t' r! T
    ; C+ @, O- j  n% S3 A
        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; e+ }  K3 o% e; O7 ^  O' N
    $ u( \& _  K& X6 T5 y; A; `    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    & M& k+ y" C' e* H8 M
    $ z9 ?4 i6 |; y+ |3 fWhen to Use Recursion! S1 t- _4 }0 e& r& E. n: ^

    5 [7 o0 q$ b7 F5 }( g% ?! @/ X    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    & l$ v/ y; \; v8 c6 M6 D0 l& T; Q1 g  e
        Problems with a clear base case and recursive case.
    4 K7 c9 U3 o% w. A2 F
    6 y/ [8 f4 l/ S) d* }* L+ |Example: Fibonacci Sequence0 b4 p5 B# @% i4 Q7 {
    & n2 J, q9 y* d" A
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:/ s1 ]0 h( I$ U/ n, y: Q6 R3 d
    " b3 k: P% D5 l: ?/ U
        Base case: fib(0) = 0, fib(1) = 1- P# o* F& V& w- C) V+ g* _

    , I$ \4 Y* J, s/ I8 O2 Y0 |% p    Recursive case: fib(n) = fib(n-1) + fib(n-2)9 n1 g& f2 b4 a: b: c0 U
    & @# B: P5 p( [2 m9 |" L
    python' M; _  l+ @( R6 T7 A4 u

    $ O! H- @# A' I+ e
    3 q0 m3 c5 g# ^8 }0 ldef fibonacci(n):
    . Z$ B0 R0 y( y8 |4 b    # Base cases
    - j2 U) I& D% J! u# l* N    if n == 0:' i! j, V/ v* r# ^' X
            return 0! u& p5 `' ]( l/ {9 M! x6 n  a
        elif n == 1:; d$ g6 Z: X& C9 e
            return 1
      U0 A2 Q  V5 |    # Recursive case
    ; c0 U$ _0 k0 J3 ^3 n. [    else:6 x  ^( P2 C2 ?) j  o
            return fibonacci(n - 1) + fibonacci(n - 2)
    / J; n3 H. \3 e  y5 G5 x# c- s5 m! i3 Z! G# s
    # Example usage
    2 ?% R4 S3 g0 t5 V# sprint(fibonacci(6))  # Output: 8! {% j# U$ ~, @/ j
    % {. ~0 J( O, u$ h! Q5 b
    Tail Recursion
    ' r' B5 s$ ~! u" j
    9 W. ^  s2 t0 V2 v+ CTail 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).
    ' C% q; S% f) {% ~9 ]7 f1 G! Q1 z) }2 H2 \
    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-24 04:36 , Processed in 0.031629 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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