设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    8 {0 C6 X8 R' d
    , [( C% v7 Z* N8 y" ?. t解释的不错
    ( e: a2 o4 Q$ j# Q( c( o6 Y
    " H. l' `$ l0 k" B& V' H  f递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    7 p7 \. J5 V( l$ d; Q; \" G) u$ [7 C' d$ R
    关键要素- A  ?& C% a' s" O/ E
    1. **基线条件(Base Case)**
    0 d! T; {$ p' J   - 递归终止的条件,防止无限循环7 J$ O5 `3 }; E2 a
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 13 Y3 D5 J; v8 y" D0 ^
    # G3 @& a* b  \1 O7 t
    2. **递归条件(Recursive Case)**7 W% Z$ Y# C1 ?2 d. c+ J
       - 将原问题分解为更小的子问题; a' \( g& X" K
       - 例如:n! = n × (n-1)!
    6 _1 @; J% p) S4 I# J4 u) z) K. Q: g4 b% t3 @6 A7 _
    经典示例:计算阶乘% K3 X2 T- s+ k5 h( k
    python, j3 M) m; z3 }
    def factorial(n):
    ; J: r" H- b0 I    if n == 0:        # 基线条件: v2 @% ?! G% W% B' u1 w3 ~: X
            return 1$ q0 X- p9 a" k$ Z
        else:             # 递归条件4 e( I. D+ C/ }2 D
            return n * factorial(n-1)3 {$ K" J8 L$ U% t/ \
    执行过程(以计算 3! 为例):
    2 }. O) n6 p. `factorial(3)
    ! e7 o* t) l; v2 H$ B' @* x3 * factorial(2)
    . G, P& ^) J! Y. `3 * (2 * factorial(1))
    3 w# `( i* g2 E! @3 * (2 * (1 * factorial(0)))
    5 i7 m+ m( k3 U& c. I% S: h5 S; p5 L6 }3 * (2 * (1 * 1)) = 62 i' a* E; Q5 W; M; u' C
    * B* s, @) R4 q% G! a- I
    递归思维要点
    ) T1 S" @, O; B. t4 ^1. **信任递归**:假设子问题已经解决,专注当前层逻辑6 V2 c  g8 u' E; {
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)# ~! Q+ x. q% s& E& h8 F' w
    3. **递推过程**:不断向下分解问题(递)
    - C- G0 [6 `& g, N4. **回溯过程**:组合子问题结果返回(归)
    0 A+ [  P( J  W+ E# q8 I. N8 K; Y: F
    : W8 l6 a# {6 \* }. f& n 注意事项8 _1 E) a# b1 t, }/ C( z
    必须要有终止条件
    5 x: B* O. v' S* q0 n: x递归深度过大可能导致栈溢出(Python默认递归深度约1000层)/ N$ C6 H  Z9 E9 C% l4 h
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    - {$ W( {  z% Y尾递归优化可以提升效率(但Python不支持)
    . L- o. K& ?4 K3 T) G  _
    ; w5 }' f2 W# i- F3 q 递归 vs 迭代8 U" N0 N% q, G) K1 q7 v
    |          | 递归                          | 迭代               |2 U/ \% n1 c$ Z1 A0 d: g4 D3 q
    |----------|-----------------------------|------------------|
    - }7 O7 L8 o/ N3 B, g| 实现方式    | 函数自调用                        | 循环结构            |2 y: t! Z& c+ T4 I* ~$ H2 ^1 j
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |1 L  w8 [% g& `% `
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    8 y( x  |: k  d" }% ]1 K| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    9 I) N9 c- ~" J0 _6 U0 Q
    ; w9 l+ e# U/ e/ w! { 经典递归应用场景+ e0 {3 R: s+ u' j; ?
    1. 文件系统遍历(目录树结构)9 M! J( U* E5 w6 v( w2 L/ b1 v* h5 l
    2. 快速排序/归并排序算法
    4 }& X$ f- ?; B* \4 }, y9 @6 G3. 汉诺塔问题
    7 a, k2 ]: V/ s4 l9 Z' a4. 二叉树遍历(前序/中序/后序)
    ! h  G$ u* J. n0 p1 e+ _5 X: ?3 N5. 生成所有可能的组合(回溯算法)! a  {+ I& r3 l, t/ o8 G

    9 d4 X% T+ n# f" b' b2 a5 L  [试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    6 天前
  • 签到天数: 3208 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,0 _! T+ m5 C# M6 n% J: M! `7 Q
    我推理机的核心算法应该是二叉树遍历的变种。, i! }- `6 g$ p% n1 R. U) y6 R. u
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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' g$ R8 [8 p9 D, P% c1 Z) m$ W0 PKey Idea of Recursion
    5 n5 V2 N8 q6 I2 H1 y/ v2 q( K* m/ c; T8 }; }
    A recursive function solves a problem by:" N: M$ z9 T) O% }1 c3 [: }' ~

    $ {- p3 e- h( U    Breaking the problem into smaller instances of the same problem.
    8 a5 c: P8 v: Y/ b
    ; Z& ?/ N* L8 v  q& _    Solving the smallest instance directly (base case).$ R3 {9 X2 y1 h4 j  V: |

    . ]" j+ y( S& a# g0 [3 [: N    Combining the results of smaller instances to solve the larger problem.5 U1 Y) R8 x! Z7 I

    9 n/ S/ t$ V5 y, nComponents of a Recursive Function4 Y( h6 {5 D( j8 m9 o

    ( q$ O' k9 P0 u7 k) m* X, k3 h5 k    Base Case:$ o8 {" B# B# ^1 H- G2 L

    " r- J5 g9 Y  v+ r# {* k        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.: ~! y5 N' N; z$ F$ W0 z9 ^4 d
    ( B0 O) R" P: o+ R2 I3 b
            It acts as the stopping condition to prevent infinite recursion.
    1 g' V! A% z1 l- i* e. Y
    ) y. M1 U2 y  L# ]+ Q1 h% ?        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    4 R" A# ~( G! a: c' G/ B
    ( O" t5 G7 {( F2 ~    Recursive Case:
    ' F! {7 N7 l; `4 o- N* o3 `! N* q8 b7 r
            This is where the function calls itself with a smaller or simpler version of the problem.) C1 [# h% @# @* D) @' K" T

    0 k: i8 i# N! h# e4 A! V6 ]9 X        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    3 @1 a* X1 a8 G+ h0 V4 O0 d
    ; z. Z( U; }9 `* H: O. G; BExample: Factorial Calculation- W6 ]& r3 H# [! D' K
    / }/ }; P  Z1 Q
    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:
    " \" D" j/ |( ^5 [3 ?7 y7 y8 c3 J; x7 a$ n& R5 ]
        Base case: 0! = 1
    # N2 E& O) x' T1 O) ?5 i9 i& F& x9 u
        Recursive case: n! = n * (n-1)!: |' E$ `& J+ H" U( U& @
    3 k$ r" m3 A7 A0 Z, ?7 |
    Here’s how it looks in code (Python):
    6 J9 ~1 D4 x3 r2 t; t. L- i& bpython8 v; ?1 @4 Y  P& D

    # g. `4 q7 Y+ O8 a$ N& ^: m3 P8 \
    def factorial(n):
    # Y# V4 Q( i& o& g2 B    # Base case
    2 v8 I3 ?/ t& ^& p$ v; u0 o    if n == 0:/ |( Y4 e: V+ m/ ?
            return 1
    1 N" U. ~2 w0 @/ \8 o( r8 l  G    # Recursive case( o% p; I9 ^7 O9 S  l6 k
        else:
    : I, s( R- }9 Q& t" d" Q        return n * factorial(n - 1)
    - g& k, z  r* s) ~. y7 c0 @! |' h' c8 ], v; ^. E
    # Example usage
    - J' x8 \) y% X: z% u# \; Eprint(factorial(5))  # Output: 120' Y5 k- T2 D7 Z( g+ i; o5 f

    8 L! K  `( e5 Z8 g- y4 x' ?How Recursion Works
    5 Q  h6 M2 }, t6 g9 }
    - c* u$ J+ H8 x1 S! V* z7 q    The function keeps calling itself with smaller inputs until it reaches the base case.% x! v; b, h) ]6 [

    9 t+ q, v9 P' @, u5 z$ s0 U    Once the base case is reached, the function starts returning values back up the call stack.
    * s5 W. T4 S' r+ Y7 |2 j- l( Q: P" }: f. H% n) g, b$ E
        These returned values are combined to produce the final result.7 B; ]. j# q6 [3 `5 x3 G+ q6 q6 z) @
    - F8 r  m. W  h5 H6 M  Y
    For factorial(5):# K: b% j6 e& c6 X

    $ x: W: h) r! b* g5 s3 ~+ o" ?% Z/ M0 |& \& x* r
    factorial(5) = 5 * factorial(4)
    " |0 b0 \  m) L; L. tfactorial(4) = 4 * factorial(3); y1 O0 D/ |# [; _
    factorial(3) = 3 * factorial(2): H# Z/ i+ Y( @. ^9 s
    factorial(2) = 2 * factorial(1), a; V$ S5 A$ f* a+ z7 Z& q
    factorial(1) = 1 * factorial(0)
    : k) z1 L- n" w% nfactorial(0) = 1  # Base case/ I) Y$ ?5 b+ @' q7 B# d1 @
    " u, K, k5 D- D7 Q# `( B" b6 ~4 w5 }
    Then, the results are combined:
    . M3 ?, x9 [+ A  C2 u4 S6 A/ k7 B2 x% j( H: N/ Z' B2 c

    . H' x1 l0 k0 u7 n5 ]$ ?; Z; _factorial(1) = 1 * 1 = 13 }  U  Q" i+ X4 T, G0 {& b
    factorial(2) = 2 * 1 = 2
    8 |% Q: W( x5 o$ |* ^factorial(3) = 3 * 2 = 62 F5 l4 f5 z, c- l0 M. U( @
    factorial(4) = 4 * 6 = 24
    0 A8 Q) o' W4 mfactorial(5) = 5 * 24 = 1202 B# V4 ?# ^5 g; }0 Y: X$ c

    ; [+ N6 r, G# f; j+ d1 a6 Z% P5 ZAdvantages of Recursion
    0 b0 K/ V! s' f3 j% K1 w; K4 }9 n) f
    ; Z* c% c5 I7 S& p    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).
    $ a$ \& Z& y! X
    0 n# r6 S4 e, G" l9 o" \& S% _    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    , X9 V# d$ ]+ C
    $ m1 F7 _" f/ R' ^Disadvantages of Recursion: k. _1 h& X5 \5 t
    . H. B/ W+ Z; |# _- Y+ c) a7 M, T4 c9 V
        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.
    , d" T/ k/ k  l$ v' G( _8 j+ I5 m# y( M# A# b: @
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    # A' @) d# k" y& v9 s" y) c/ H: s$ Z
    When to Use Recursion$ ^5 k) @+ k  t& s. G$ d4 ^

    . b6 @; N; l/ @8 L5 O    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    . R( f5 h! h8 U. c! x$ m3 L/ t; j  T: _& F! o' T
        Problems with a clear base case and recursive case.
    5 I4 @; I* M" U9 b. A3 X! T
    . [7 K+ D1 y1 U. k) H( XExample: Fibonacci Sequence
    0 w" I4 z$ |8 d, N1 o, {# i( z  Q* I; a  C) }
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:) u5 {3 [6 X; c2 a' ?6 I
    * o. q" a; |( U- [: u
        Base case: fib(0) = 0, fib(1) = 1" S- F4 Z, J# z) x. A3 y- U
    ; A0 T( p+ y8 C9 W$ o& y
        Recursive case: fib(n) = fib(n-1) + fib(n-2)" f% E7 b/ p: _9 o. x

    * @/ ?/ H0 |0 t* Z& j% Npython4 [/ _/ `4 n) t8 d+ k
    5 E5 f* ^) x' w

    , t# P; \3 M" k  c) mdef fibonacci(n):. k( D# `4 \3 ?9 A7 S( `; n; M( m
        # Base cases
    - P' F& c+ n  E1 n5 z2 ?+ z# k2 `    if n == 0:, z* y) o$ M/ M1 X% r, ~. E9 B
            return 0( ^* _  V/ R+ i
        elif n == 1:* _1 Z; _; P! Z3 D) _( h( [& ]
            return 1! }# ]& p* O5 }9 g
        # Recursive case( s& ]- Z& V$ ~
        else:1 Y* D& x5 ]: D( H, d2 O
            return fibonacci(n - 1) + fibonacci(n - 2)
    - g# z7 x2 X2 a& J0 B
    * S. y! Z* Q3 m# Example usage
    6 I% U% t1 \& R3 B! a. `print(fibonacci(6))  # Output: 88 }. I9 |7 `7 N) d1 X
    8 ]( x7 S8 j, S: c* [: D, B' }% e" n
    Tail Recursion- p+ ~$ v7 ?. Q( F, k

      `  H& F! p3 l$ uTail 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).
    2 \3 P/ K1 t: x4 w
    ' B. F/ ]; m! m8 M/ _; }4 I+ l% AIn 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-3-29 07:10 , Processed in 0.056212 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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