设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 7 A, b8 G: c9 }
    ; p" O' ]3 q$ K# d7 I
    解释的不错
    8 m/ Q0 G& a/ d0 ~& B- p) w2 P! f/ E
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。, u8 j% Q2 W. s) j
    9 Z. R* G/ W) s* C% O+ O+ S2 u
    关键要素' v" t0 O- Q* H5 E
    1. **基线条件(Base Case)**
    / {# Z) X7 }$ a. y   - 递归终止的条件,防止无限循环
    3 h% M$ ~3 M! T- R   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 18 ^0 u: C& _, T/ f  ]
    4 X- Z" @9 q7 P9 i5 H
    2. **递归条件(Recursive Case)**0 a, {3 V' E4 `- x9 Z
       - 将原问题分解为更小的子问题' ?  s0 z, G$ \# S
       - 例如:n! = n × (n-1)!# O5 h; V1 ]; }0 q1 S
    9 |8 E2 W7 j+ E- y2 u3 O' T( Q! v
    经典示例:计算阶乘# O; J* a' L7 j' f* V
    python7 f" u1 f5 y7 T) J& f
    def factorial(n):
    / a8 f- @8 j3 l! V    if n == 0:        # 基线条件/ s: y- H4 j) i
            return 1
    3 r! ?9 e. J: @+ F: e5 ?6 F    else:             # 递归条件
    ( C& ?/ ^2 B5 Y& l$ U        return n * factorial(n-1)
    % {# Q8 |$ N5 \% O! x& o; [执行过程(以计算 3! 为例):0 i. r5 G; u  C/ g8 Z) Q. [0 @4 A6 U
    factorial(3)
    7 r" c% Q2 \- o9 \; Z4 J. ]# Q3 * factorial(2)' M- j2 {; W  s6 O
    3 * (2 * factorial(1))6 U: C1 t/ G! u  D) M  X
    3 * (2 * (1 * factorial(0)))
    - {; @3 l' l; h1 s' J3 * (2 * (1 * 1)) = 6
    & [- b# f) v7 @3 m0 ]: O2 e9 n' ?* S# F7 V9 M9 F$ p, p" W
    递归思维要点
    ; @3 L) Z, T- V: h$ {) y; T1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    & v( a( T+ X/ O% d1 d& a, H5 N* o2. **栈结构**:每次调用都会创建新的栈帧(内存空间)- P3 S/ ?3 Z( ~  X  o8 F, w
    3. **递推过程**:不断向下分解问题(递)
    ' q# f- |, Z2 k/ [* ~4. **回溯过程**:组合子问题结果返回(归)
    ' d$ s0 U$ D5 t# O2 H" C2 U  m! n/ U1 V8 n4 C4 c: H3 Y
    注意事项
    ) U0 D3 U  k. v; y6 _必须要有终止条件8 C- y# K. e% E2 s+ s" e: S9 e
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    5 s. [1 X$ @4 B: u! i4 H某些问题用递归更直观(如树遍历),但效率可能不如迭代
    & T* s. _" x, h# |* P# o尾递归优化可以提升效率(但Python不支持)
    * a; I4 T5 G, Q1 m& l2 O: A
    * d# j2 B/ c. D3 E 递归 vs 迭代" n' J% L0 {! s8 N( L
    |          | 递归                          | 迭代               |
    & z+ \. h3 V# K( H|----------|-----------------------------|------------------|
    ; {5 m) P/ Y: W6 r1 [% M6 N; ?| 实现方式    | 函数自调用                        | 循环结构            |  Q% O, @* f3 `2 W# m5 J& {' l& _
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |" C( k! c6 S& y/ O
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    5 M) |8 @6 j0 C: o| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |7 C& K& Z+ w0 P0 n
    & g& }$ L  ?  D' ^
    经典递归应用场景6 A4 N- d2 c* s9 `" C# u5 w. K
    1. 文件系统遍历(目录树结构)
    / V8 f' X4 d; i0 l; S# V# h/ y2. 快速排序/归并排序算法
    ) C) n. W9 \$ Q: \) ^4 o3. 汉诺塔问题1 @. E0 Z) l5 B) f) T) E; L/ t
    4. 二叉树遍历(前序/中序/后序)
    9 r8 x# k6 v; k8 `7 i& i5. 生成所有可能的组合(回溯算法)0 L# _- B1 c# m+ `9 k# z' A! y$ R

    2 D! t, _$ R% X9 C试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    2 小时前
  • 签到天数: 3137 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,; n+ m8 y5 h& d
    我推理机的核心算法应该是二叉树遍历的变种。3 F% o: m# n( A% [  v* c
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:, \! K9 H* O( Z2 J7 @2 x9 W9 C
    Key Idea of Recursion; s3 |# q, S) Q9 D" p9 ]3 T

    2 \8 O1 ?) A9 T. {A recursive function solves a problem by:2 Z! Q  T2 S8 o. |/ ^
    / R, `; i* Z( T1 K) c
        Breaking the problem into smaller instances of the same problem." p/ e2 E+ z: q( C

    " n& z3 r6 \8 O2 H1 N4 |. e* C    Solving the smallest instance directly (base case).
    : W1 c9 J, w$ ?9 E( t+ b$ x" C8 }' I% ?# X& D2 U
        Combining the results of smaller instances to solve the larger problem.
    3 s! k9 x( ^  Q
    0 ]6 Q; l' S( V9 Q% }0 I8 rComponents of a Recursive Function  n( B1 c' G* V  ^' D3 N

    - T5 R9 W7 Q4 I    Base Case:8 }! n  w. S8 {. y- F8 a! m: K  h
    + j/ U3 q# \& `7 ?! V' r' {
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    9 P) q3 b, [3 {3 K7 |( W+ }! W  p9 h3 |6 \
            It acts as the stopping condition to prevent infinite recursion.0 T# R& ~, w4 W( b

    0 v/ `" J) a# a* r2 w        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    1 t7 X5 `% G/ U" s' Q$ N6 Z: h
    8 a2 {2 s4 Z4 G2 }" [& G    Recursive Case:
    ' e6 m- C/ r1 x5 j
    ( W1 d4 q$ n; O* q" w7 v6 U6 N        This is where the function calls itself with a smaller or simpler version of the problem.
    3 `7 _" a( b2 i  {8 `1 f. e7 {& C( E% k. ~' x
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).- z4 A% p- D. F! a! M
    0 e- \6 x' H1 j  k0 e
    Example: Factorial Calculation3 r: x/ E- x  t
    & e$ W( p! I2 \0 ^& O
    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:$ C& ?1 k4 h2 O. @6 M1 j0 c& z( ^/ a
    ; x4 }, `' {& Y9 j
        Base case: 0! = 1& i1 b4 T6 M" Y
    5 V" C+ u6 W7 W, F+ `* G/ W
        Recursive case: n! = n * (n-1)!
    / ?8 u. L1 H; [) @0 h/ q! m% T( Z) }( Z4 m9 p
    Here’s how it looks in code (Python):: g7 ~0 H" m7 w% R1 m2 c; q7 }
    python
    - V0 }' g" c& u- o4 n$ \: W' D$ M. r
    ! p! t& h6 X! j) z; D% H9 u- ?1 l( ^+ I, P; z! i
    def factorial(n):
    " X$ M/ c5 j7 P6 L- K) @+ S2 \7 `/ n7 U    # Base case
    2 p9 @8 d# X1 [5 l/ c) A    if n == 0:
    & d3 b2 Q2 H! }! x7 b3 |* `        return 1
    3 n" K" W) ?* Y# O8 |    # Recursive case
    4 C$ Y# q3 F; }4 }% v: C    else:( P( ~1 m/ ^  G
            return n * factorial(n - 1)) d, S" Z$ M( q3 B8 K

    1 D" v% y& ^* g/ a& W' H# Example usage
    . @: h+ N7 p. J, [print(factorial(5))  # Output: 1202 m. d4 F% `8 d

      T' I8 T3 S& |( N4 C6 FHow Recursion Works% _& y$ M$ J+ \4 b5 `( g; n

    8 x7 M1 `' ]8 f& `- K, o    The function keeps calling itself with smaller inputs until it reaches the base case.- g4 r  J9 F( p

    6 C. J! m5 v" e$ L- u! ~+ ~- a    Once the base case is reached, the function starts returning values back up the call stack./ M: j( O: P% i" ~
    3 k% B) X& t- ^# y4 i" Q
        These returned values are combined to produce the final result.
    " p. u- y! N7 o3 b5 [1 |& Z5 T$ f: u
    ) X5 {; O; M* N8 R) fFor factorial(5):
    . P. @) x; m$ N$ p$ A1 D& ~) ~# {! O0 Y0 R9 b. [

    5 H  E7 C3 ]" N6 @5 b% v5 Zfactorial(5) = 5 * factorial(4)
    ' D7 ]5 L; Z* C3 _) o* M1 Sfactorial(4) = 4 * factorial(3)
    & d3 k1 |; O1 Y9 I, @& nfactorial(3) = 3 * factorial(2)5 t3 d/ N5 r! m0 _1 M: J. x# w
    factorial(2) = 2 * factorial(1)
    3 J7 S% \. y$ v7 f5 O. Ofactorial(1) = 1 * factorial(0)# ^4 q( }& o, H7 r+ C" J
    factorial(0) = 1  # Base case' O- J4 i) f$ \; S
    * Q7 H9 \) @* e8 q) u
    Then, the results are combined:
    . O9 F( X7 Z$ q5 L! b. A# g2 R& y6 x  i8 c# s

    0 O/ H( W+ }+ }$ q7 Gfactorial(1) = 1 * 1 = 1& R5 Z+ @1 Z- V9 ^
    factorial(2) = 2 * 1 = 2
    0 o: L  d, \' w7 \& vfactorial(3) = 3 * 2 = 6
      j! Y6 z" o* rfactorial(4) = 4 * 6 = 24
    7 @9 U9 H( I) S1 Q9 ffactorial(5) = 5 * 24 = 1201 m; C. T6 D6 E) h5 U: v% D9 J
    1 Z7 B: O$ p* `, m
    Advantages of Recursion  C6 S' B5 g$ e- n

    " I9 h! Y1 C9 J, G: \. \    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).
    . c. y% Q: F6 H* R+ d4 _
    1 O; D* ^. v5 y6 Z6 g' C) M    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    , P; Z& X  i4 F2 P6 v1 v! ^  i5 X" T+ m1 d, e
    Disadvantages of Recursion, J$ T+ V) V: @& K3 R

    8 z( L/ W" ]+ ]+ @. 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.2 h+ e" n1 f( ^/ X6 T

    6 {& }: w3 N* t: t7 b& N    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    2 b3 m# l1 R0 Z4 ?3 o1 b
    ; c. e2 b+ I& M. A+ A1 XWhen to Use Recursion
    " N' V2 l! O3 P; t! ]' s& e5 w6 J/ F1 [8 C
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).8 z/ S3 y9 p9 K3 f" w$ J# X  i

    # y" h/ q: c5 ]" K  J/ Y    Problems with a clear base case and recursive case.- y$ q( x2 b, y; q  f+ x' L, f1 U

    : B# X2 ~+ l# f; B6 L  _8 X- jExample: Fibonacci Sequence$ S- O% H9 y; V% L6 ~
    2 |3 r: _+ J  p
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ( J. O; Z. W, `0 o9 Q. F, ^
    6 j* R' \5 ]9 @; V  T% v    Base case: fib(0) = 0, fib(1) = 1
    2 c. `3 b# ?& b9 R" }- |2 m% ^3 }# b: ?# K! Q
        Recursive case: fib(n) = fib(n-1) + fib(n-2)* U) i0 @& m7 j; U# ?( i. N$ x

    1 g9 o% J. I2 s- F; n% `0 {python
    + @; z! U! R  K5 G- d( N
    9 x) K) K# z( Y6 r/ k: ~, U3 T* [4 t% ?3 j
    def fibonacci(n):
      [  Q* ^0 v3 ]    # Base cases: }" E6 h  H% x6 S& h0 R6 F
        if n == 0:6 l7 W  U5 T, H& e2 @8 P
            return 03 a4 A2 e) y3 D( w3 H
        elif n == 1:
    4 j" u/ j# b! H        return 1  ~9 H: K8 D2 _. \% E- {/ ?; Z- c5 a
        # Recursive case" R6 z. E' {* z* X
        else:
    5 Y3 s& j" K7 @* A2 |        return fibonacci(n - 1) + fibonacci(n - 2)+ j1 X+ d( _% W. L, Q- d9 D. h
    1 W3 P' f8 q: A' \  `0 z
    # Example usage
    & J: ?5 x; B2 R$ E% h( xprint(fibonacci(6))  # Output: 8
    $ N( f. P0 K, j9 R6 U4 M& N. O
    8 N3 s/ q5 w7 }' T' KTail Recursion8 Y) T3 c7 z5 p. o! ~
    & J5 O  J( Q. B5 ?/ H( T& B' [
    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).
    8 q5 V1 ~8 \; D/ c4 c- v" O! j6 E5 ?; D( X. ]1 g
    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, 2026-1-5 09:09 , Processed in 0.168218 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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