设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ( M' ?$ W  Q1 p6 Z9 L( s; o
    9 B$ h4 J; b- m0 S: a# a& x" ~解释的不错$ ^& A; I+ `  U% F  c7 r/ r$ u
    # @" ^+ B( W+ A+ A& f( _
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。  k9 c5 h/ o# [& p3 h# b! g. j

    5 o  v. v1 h2 [8 X+ C* N) P* t 关键要素
    * H9 Y. z  Y8 m1. **基线条件(Base Case)**/ V4 Y2 r  }: W
       - 递归终止的条件,防止无限循环
    3 {5 Q2 ~+ z$ A' v9 _2 g3 r+ M   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    % Y  W0 M$ m$ F/ X1 \% J9 o8 ~7 w5 P! v, ]: g  x0 {
    2. **递归条件(Recursive Case)**
    # j, @5 w: _" ~) P# s, W4 ?7 z   - 将原问题分解为更小的子问题; Y9 X* Y# f- B( t! J( J
       - 例如:n! = n × (n-1)!
    5 _5 n9 s  k/ C" |7 y; b& V
    / F- a) Z& A# G+ _; k% r: R; K  f 经典示例:计算阶乘
    / m7 Y* ]  X3 S9 G1 N8 G5 D/ xpython
    " f( u3 \* K; ?! t1 Q; Gdef factorial(n):; z  Y# `' P: r. P& u) m: W
        if n == 0:        # 基线条件
    3 z& X# \, m& L        return 16 L! {4 O2 F9 t9 d' _; l  i
        else:             # 递归条件
    0 z& `! W0 ~  {2 Y) @& ~/ ]1 @        return n * factorial(n-1)" ]- ~$ X# `" h) J% l8 o( L
    执行过程(以计算 3! 为例):
    ) @8 w, V0 X' X: `7 Q$ Xfactorial(3)  {3 r' @2 C$ i) q% u1 \
    3 * factorial(2)/ M0 \' @$ p* k1 d; x5 }8 X
    3 * (2 * factorial(1))
    0 x! N/ e  F; M% D( f% T3 * (2 * (1 * factorial(0)))
    1 l# h6 ]/ d. I% T# U2 ~' k6 R3 * (2 * (1 * 1)) = 6  b2 O& I0 A  T/ o& j
    & E* c) d- P2 O5 t
    递归思维要点& S& }6 n, R5 j: }2 s
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑7 I; `( M- d5 ], N5 p
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
      z- S, J+ K. `. ]3 g! q& Q3. **递推过程**:不断向下分解问题(递)' |( H* c" _' m* d0 p' J/ V7 E
    4. **回溯过程**:组合子问题结果返回(归)
    ; ?7 G# f; |/ G! [7 C
    , u. D3 N, G" X7 x 注意事项
    0 W7 f1 k5 e2 I/ T5 [: {3 t5 c9 k1 @必须要有终止条件
    ' i3 u# Y. N  E2 k递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    3 @, A4 O, r) D' G  [1 B# v+ L; A! x某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ! I" F  m9 {& ~4 t, E* P尾递归优化可以提升效率(但Python不支持)
    4 m" a# o1 T" |* Y7 s6 e" F* `. u" {+ B7 G" d
    递归 vs 迭代+ f; ^/ s% G) R$ _2 n( p( U
    |          | 递归                          | 迭代               |7 s% k+ }. V% I* D. Q
    |----------|-----------------------------|------------------|
    8 y- @' o% E$ j| 实现方式    | 函数自调用                        | 循环结构            |+ L! b  U" T% C+ z
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    8 k2 w  n) q- p# `7 U| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ; L; W% _" }& p| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    0 e( D# G5 U! d9 ?1 X! {% O( s1 x
    1 ^/ d4 O1 |+ g. n5 L 经典递归应用场景0 |# Q) _, c3 c: n
    1. 文件系统遍历(目录树结构)( ?6 {$ f- {1 A% G6 I
    2. 快速排序/归并排序算法2 @- z! n1 }# U9 u" K% }8 g" X
    3. 汉诺塔问题
    2 ~% O, m, B5 H- F  d4. 二叉树遍历(前序/中序/后序)
    + J9 n6 `& a1 \, r; U% a& @5. 生成所有可能的组合(回溯算法)
    3 V& e$ W2 G" C5 j2 Y% M( ]1 d3 F+ x& f2 ~: ?; h
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    4 小时前
  • 签到天数: 3184 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,: G; S5 f  ]8 u! R+ V4 s: y7 i, D
    我推理机的核心算法应该是二叉树遍历的变种。
    # d9 @% D$ J) P  I3 F; z& B! q另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    / w4 |% O) t) q) _2 p: M9 N: G; eKey Idea of Recursion& e* h5 F3 t3 \; N) X$ t# d7 P5 _4 F
    : P; d" b: k8 t& z* \( D+ ~0 M3 o
    A recursive function solves a problem by:
    6 T2 a% a. ~" ?/ e
    ; ^# u) o  ?3 b8 z2 i. _    Breaking the problem into smaller instances of the same problem.
    ( F: c+ J9 d* Z$ Z
    : C: w/ ?7 W$ m1 W; a, s    Solving the smallest instance directly (base case).
    7 y: X$ q. i5 i
    - o! b2 B: D/ t" z    Combining the results of smaller instances to solve the larger problem.  X& ~% K( }/ Q8 @* u
    4 w% l; s( q6 t: I4 ]
    Components of a Recursive Function
    . G0 V: _$ K* W- o1 q" ?8 x* N8 Y+ X# B" j8 o1 a9 t
        Base Case:7 N" a, @/ M* ]- d& N2 \

    - t+ Z3 T5 q5 k* [' m4 H3 n' v        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    / B$ h7 u- ^- k4 D2 O
    2 c& w  G1 k6 D6 J: I1 H: H5 M/ \        It acts as the stopping condition to prevent infinite recursion.
    " |# g& h& `, d% _: v5 A' O5 R0 p2 m9 P
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.5 _, k$ d, \1 v4 }5 Y, X0 Q4 O; s
    3 D" [& \7 P- @& P  l, n$ P+ f0 v1 r
        Recursive Case:
    3 j% k+ v5 K" {: M& T
    3 p; g' M6 N! ?5 Y: r8 }( I        This is where the function calls itself with a smaller or simpler version of the problem.
    : |/ B! S7 H1 ~5 g0 v
    2 A0 x6 c$ }+ _        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    , F$ m* y/ G+ g. M: [) l; w
    + ]& O. {, U5 \- o* I5 e4 ]; w, dExample: Factorial Calculation/ H+ q+ b5 T. p
    ; r& L! B2 l) q( h, o# D
    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:% Y; s4 S: Z1 |$ [& Q( L9 W6 ^6 c

    ) }) {& O3 g; e5 e3 g( U# B    Base case: 0! = 1
    + O3 ~- E1 Q$ _1 C, l4 X4 z9 B2 G
    7 [; Q- Y& G9 E9 o3 Y& s) u7 e    Recursive case: n! = n * (n-1)!
    8 H: w& B& m6 y7 S
    $ c6 t1 m" C* B% l& gHere’s how it looks in code (Python):
    ' ~9 F5 H+ Q" I, i6 d6 }python/ z- D8 D7 h, H* _* E
    $ y9 p, h* j2 {2 D

      o; x; h% R, t" P$ Y9 P! Jdef factorial(n):) e& T7 _7 n7 O- F/ v. t% Y. U5 \
        # Base case
    ; s0 b! P0 M0 s4 Q. d8 T% b) o8 E: t    if n == 0:/ }$ `- Z9 W8 o7 U5 `/ S
            return 1
    ; \- d+ g) D9 R* R( b" @    # Recursive case
    # k) Y& _5 |& o( t    else:
    6 `# o( e4 P5 M" ?        return n * factorial(n - 1)# v9 {! o. Q' M

    . _: u6 Q$ U  D( _' ^% X3 @; j# Example usage
    + j: W! F+ b9 U( oprint(factorial(5))  # Output: 120
      s6 E5 Y$ K$ {! b! S- D# d
    # ?2 |3 r/ h! W& a0 [# @) L9 ~* l. x4 [How Recursion Works
    5 `+ E9 o  R5 T2 I6 u; g7 R0 Y
    5 V/ _& P# `9 ]" a, g    The function keeps calling itself with smaller inputs until it reaches the base case.
    ; E6 U* _8 l7 }" @1 l3 c5 r& }) X/ F  Q
        Once the base case is reached, the function starts returning values back up the call stack.4 J9 K' j8 _7 u& I5 N4 R
    4 A' ^5 ]5 z1 v$ J6 t1 N: `
        These returned values are combined to produce the final result.
    $ B, [1 g4 z7 g: _4 [: t- Y0 ~: ^6 {# C1 `6 ~; m
    For factorial(5):
    $ Z6 T4 P% `1 Z+ V9 M1 T7 v5 R
    8 t& p6 b( h9 b
    . f0 n' ~0 n4 \factorial(5) = 5 * factorial(4)3 ]0 N5 ^# C0 V" ]$ G; i
    factorial(4) = 4 * factorial(3): r4 o% {2 N/ D+ I+ P; }% s4 ]4 |
    factorial(3) = 3 * factorial(2)
    6 N, m8 i  M! [+ Mfactorial(2) = 2 * factorial(1)
    , Q/ N0 |* A' g7 U5 o' U5 N8 T% zfactorial(1) = 1 * factorial(0)3 r; c: Z. |) ?
    factorial(0) = 1  # Base case$ r6 k+ k+ p- x6 `! n
    5 Q4 p7 [8 C3 {
    Then, the results are combined:1 ?/ F5 n9 r! t0 E9 B. s

    ! @0 {' q* ]8 [4 d1 F( _3 E6 _/ q& v; t/ f3 N
    factorial(1) = 1 * 1 = 13 _$ R( t9 P% _; |/ c' T' e
    factorial(2) = 2 * 1 = 2
    8 H. u3 O3 [" N0 Bfactorial(3) = 3 * 2 = 6, u- f0 Q0 X4 |, W% `4 V
    factorial(4) = 4 * 6 = 247 p8 c6 z  v3 k2 Y
    factorial(5) = 5 * 24 = 1200 _; M) g, G7 n/ y4 s; V5 o

    * t+ o: B, Z: h% s* i& m! e( zAdvantages of Recursion) @9 C, m+ g/ I* F! Q! E

    - w, o2 v9 E) }' L  l7 k1 N    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 T1 \: j0 }! `! R' N
    ' Q6 {# g# b  D) ~
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    3 @% H% b1 _! @' ~( l* l7 _5 S6 Y1 B7 z6 O0 p
    Disadvantages of Recursion0 ]6 p/ s, h& p: r5 O5 l- }" A
    ! I( N$ }; G2 m7 b4 M/ f0 T
        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.5 P0 n8 w3 l$ _$ L3 v! p
    ' D, k1 b' v2 n6 w& C
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ) g! k* ]5 s: e! U% D' ]/ H  j8 [1 D, p4 T7 P* K$ |
    When to Use Recursion
    7 a, W% \! x0 g8 \/ u2 L$ t7 W
    . f2 K( x( V! K# Z9 a6 \    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    4 o$ c" |( r! I! ]
    2 C& S/ R4 }# Z3 g    Problems with a clear base case and recursive case.
    ) w, J1 _( D( Y) z% R( a8 `+ w' E" Z2 A$ B2 z
    Example: Fibonacci Sequence
    : x7 ^* b8 l! m% [2 P) D% F3 A  w2 k; C- e+ g3 Y' ^$ ]# U
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    8 I6 ^' Q- u5 ~* e
    ! R$ c4 ~; f! H. Z# k( L2 k0 B    Base case: fib(0) = 0, fib(1) = 1
    , {, c/ f3 L7 |/ J; m4 r" A
    5 ]' c6 G  w& {  }- @( T6 C    Recursive case: fib(n) = fib(n-1) + fib(n-2)+ H* r) C% U2 _; e2 b3 A% m
    / a& E; m6 d9 a4 M9 A2 t) i  q1 }- ]
    python
    0 `  S2 U+ J. T5 c- ~% R
    7 ]) J! x+ s, N& V5 D
    9 I. A; g9 s. w9 Mdef fibonacci(n):3 G: U1 |3 c2 T
        # Base cases
    / Y( D9 X3 {) K' m    if n == 0:3 n# [3 A$ c0 o; n6 ?: C
            return 0
    7 L; I3 T. J  `* U- |: G8 t0 R. _% r# Q    elif n == 1:. g- R# y7 E: p7 E$ `/ w
            return 1$ P  t' p$ W5 j+ }7 g
        # Recursive case
    - ^. C7 a3 _( k    else:
    , z9 N' v# F+ @4 L  b1 Z% r9 \4 Y. e        return fibonacci(n - 1) + fibonacci(n - 2)
    * N5 }% ?+ z! S; D/ j! g6 e0 E/ R' W5 `! f8 R% \
    # Example usage
    % b* Z" Q/ M# J3 ?5 C# g  ?print(fibonacci(6))  # Output: 8
    5 A$ R+ X, ^( |
    3 ^/ S0 |/ D0 K2 `$ \$ OTail Recursion5 P: f0 Y! |& H8 m! f
    , R3 ^# s2 X! B4 i; ?
    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).
    + k3 ^3 Z; w. z( X2 J& Y+ C$ w+ I
    # k8 ~+ r2 P( B1 {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-2-25 11:58 , Processed in 0.054092 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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