设为首页收藏本站

爱吱声

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

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

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

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    / k$ V, _, E- L$ r. X4 a7 m% f: t% P
    & O8 i! s- O( h( h* A, s解释的不错
    # t0 z+ u- t, ~) v8 g* C  k5 T& k2 k1 b3 y' B
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。! \; c/ x6 S" D6 ]2 f

    ; |3 p7 m/ x: e/ G  L 关键要素
    # [1 {4 h" j7 w% h* `, ]1 k1. **基线条件(Base Case)**
    4 T+ J7 A9 c  ]   - 递归终止的条件,防止无限循环
    * V( h: v0 _6 n' c   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    1 N& {0 T% R# b) h# ~# A" u% b% z  o4 Z- j$ x2 f4 d
    2. **递归条件(Recursive Case)**
    5 p( k% Q9 G% T; p   - 将原问题分解为更小的子问题; Q" X4 q3 A# ~0 X4 |0 L: S6 g
       - 例如:n! = n × (n-1)!
    " {  E& ^+ l' G0 R; a& F( k. c' O/ {+ l$ m% n  B" O- ~1 R
    经典示例:计算阶乘  ~7 R' m9 e8 J
    python
    7 e3 I$ X1 c) r% @9 [5 W0 d. Hdef factorial(n):
    2 s2 o3 ^8 s/ w% C2 z) \- k* u6 T    if n == 0:        # 基线条件
    2 P9 R  F& O' T5 C* d/ S  M$ f        return 17 y6 u8 s5 X9 Q: l" i, l9 D
        else:             # 递归条件
    5 ~$ f. B4 v1 R- q0 H8 h& m        return n * factorial(n-1)
    # @0 S# {- b! \' j, |7 X8 H  m0 S执行过程(以计算 3! 为例):
    8 o0 }. ]. r; o4 M+ Ffactorial(3)
    5 ^- e7 N- D5 F2 @" s3 * factorial(2)
    : Y6 C$ c( @( Q$ t* z. W2 M) h2 s3 * (2 * factorial(1))
    4 t& X# D  `# |1 n, C; Q* {$ }( D3 * (2 * (1 * factorial(0)))! M. H, Z  y% N& O4 F
    3 * (2 * (1 * 1)) = 6% Y9 [+ _# j5 e& g2 n

      O' G* {1 T8 T$ n8 P" y: _- o) y 递归思维要点
    0 B* Y3 x8 i. V4 S  \, I8 [1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ( x) x. l* X9 F) m, G( d2 m6 P2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    " R/ y9 p; ?% F7 \8 m3. **递推过程**:不断向下分解问题(递)
    ( K3 i4 X( E( n6 r4. **回溯过程**:组合子问题结果返回(归)2 R  F2 n; n# t, S; S
    ! {' C( D4 J  V0 x' @; s
    注意事项
    - s4 u3 n: h; K+ F; V必须要有终止条件; V# m: Y; o. ^4 `1 |. y, j
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)) i% w' Y' S# A
    某些问题用递归更直观(如树遍历),但效率可能不如迭代# r  k  @' u, H5 i) ^
    尾递归优化可以提升效率(但Python不支持)) e3 b1 y& j& w
    / H( |* l5 n; p0 H* d" q
    递归 vs 迭代
    ( G4 @6 z' E5 t2 x* ~|          | 递归                          | 迭代               |
    1 O/ ^6 \' Y# M0 A; `5 s|----------|-----------------------------|------------------|
    : E2 y+ d) V9 R9 G8 ~' C| 实现方式    | 函数自调用                        | 循环结构            |# ?6 F1 y5 T% K* d
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    - e) C" x  b$ k| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    * ~0 s$ Y1 e  i; b9 l9 M2 _* w| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |0 \) V+ v" n* ?+ n

    * [' Y; Y  _  n" ]6 l4 G; H 经典递归应用场景4 |; k5 H  C) n, S4 v
    1. 文件系统遍历(目录树结构)
    % R& E4 @6 S5 }# t8 |2. 快速排序/归并排序算法
    ! h  O. X- B  y* C, a3. 汉诺塔问题
    ( T! c, b# Z0 e, Z$ b4. 二叉树遍历(前序/中序/后序)
    2 V8 ~; \2 \/ R' T+ f  n5. 生成所有可能的组合(回溯算法)- N! P8 v: t/ V$ u* @
    * K0 X9 h0 a) h+ ~$ [
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    昨天 06:39
  • 签到天数: 3016 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ' M( ~/ c  e; R9 @9 ~5 p我推理机的核心算法应该是二叉树遍历的变种。
    8 m& ~2 q( u2 a+ u+ n' G, p# |另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:+ ^& {6 o$ e& i& L) T
    Key Idea of Recursion
    0 U' h( B& g4 {, o: ]+ }
    5 ]3 |; J. ^* S: I! \  x9 Q% w+ lA recursive function solves a problem by:
    , z4 z5 C2 w  Q; ]2 F
    , D, s, K5 ^0 S6 l$ j/ U    Breaking the problem into smaller instances of the same problem.
    & y+ Y* k9 z7 ]! q& ]; R4 |/ S, {
    " E4 [- q+ y! i# D1 S  r" l    Solving the smallest instance directly (base case)./ B& X& c) V6 T* b# x

    4 P2 ~6 i( n5 W8 |    Combining the results of smaller instances to solve the larger problem.
    8 [- \6 d! z6 [- C' x4 E* B, q2 b" I$ f! H7 x0 @
    Components of a Recursive Function. W8 M. l( M! I4 Y0 S5 M- d, W! T
    : ^) [- k, y+ X. B! ]
        Base Case:: [, |8 \2 x! A: T# `! l5 R+ ]: ]

    ) P8 P4 E) ^; w0 P/ i: r0 i        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.: f& b) n  k+ O$ b  r4 ]. p7 {
    0 H+ v: Z2 A: v! T; t. B) `% ?
            It acts as the stopping condition to prevent infinite recursion.) t8 I2 K0 [/ a6 x( N5 K6 N
    8 H+ x; ^  m, n: X2 L
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    % L) N# R+ i# p0 W% a3 r, ^  y0 o  |, r6 `& G
        Recursive Case:/ }1 O8 v+ I. i

    ( x- m' t9 ~8 Y- }" r5 c        This is where the function calls itself with a smaller or simpler version of the problem.
    9 b7 w: W% B% ~( f4 M' a( ?4 P& D9 J8 n" u* U3 D
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).: g; n0 p) T1 b; C5 A+ v% [) n% k
    * _/ d5 J3 Y8 d0 @
    Example: Factorial Calculation
    2 Z. Y- m  H9 w3 ]' c& M" c' [! g& L" N9 h4 x+ `
    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:
    + t2 S5 h4 ~8 a. R  W
    * ^2 h7 K$ T& k& W; G    Base case: 0! = 1( N: b5 l% V6 E( [

    ) c8 y1 k. o1 N5 J! V5 ?    Recursive case: n! = n * (n-1)!
    ( L9 P: X$ \: c, z3 z
    ( I( I0 A/ N4 g8 z: b8 b) f4 @2 ?Here’s how it looks in code (Python):
    ; b9 B! s. t. k9 R# y# K7 opython
    5 D5 Q. g6 q$ J$ l3 a3 O/ S0 C5 V4 N8 S6 |) W1 s

    $ l6 B; f; X, ^% f* n  `* [def factorial(n):' |4 `% p0 c7 @/ h/ s
        # Base case# x- R* z/ C& u& }$ r
        if n == 0:- a+ Y( s1 r- y
            return 1$ o0 f" @1 J& q! y
        # Recursive case
    8 [3 |% d3 x" I, x) ]- \4 m    else:
    / q& x, O/ r2 M( ^  R* k        return n * factorial(n - 1)
    6 D  Z2 l( Q% H( p! m& A5 u( G5 }3 _+ F9 |3 D
    # Example usage2 J; n% e5 U( E, U
    print(factorial(5))  # Output: 120
    % X9 g' k  E) y6 H1 {( f( }+ E3 Z- `  O
    How Recursion Works
    2 l4 |  i5 h, Y5 u) `1 e! ^* j
    4 b% K' s3 |+ s' b! O( u    The function keeps calling itself with smaller inputs until it reaches the base case.
    + i; k* |( v3 J! w& e* [4 i( S9 ~" `0 H3 ]8 V3 D
        Once the base case is reached, the function starts returning values back up the call stack.# b* x) Q. x9 F: b4 H  M

    ; x$ V. r( T( _) d0 A& I! l    These returned values are combined to produce the final result.
    : ?0 U- \1 D0 o6 h0 N( N
    1 A" q# [- n- ?# NFor factorial(5):" q& _7 _: c2 R2 s5 I

    ! p+ F# |; ]+ F; H& R. D0 ~% h- E2 e! C% `2 S5 }$ z7 r* S
    factorial(5) = 5 * factorial(4)0 {: }* `; _4 Y* H
    factorial(4) = 4 * factorial(3); F: Y6 Q2 e9 u; ?) g8 `- x6 g
    factorial(3) = 3 * factorial(2)
    : q# _, j8 ^" n( c# j9 p7 dfactorial(2) = 2 * factorial(1)
    3 Q5 {8 d3 V" x( t1 dfactorial(1) = 1 * factorial(0)
    # P& q8 W1 ~+ q8 E' J; M" c" tfactorial(0) = 1  # Base case6 V, y$ o, U5 a2 w) @7 C

    / j$ B( t; J, f  `2 p* m3 T3 I/ d1 ]Then, the results are combined:
    0 A( k5 u8 \5 @$ U  v
    ; q0 i9 s- ~. ]4 Q; U5 Y: q$ |+ G3 g, d2 F, B# e$ X3 L2 w
    factorial(1) = 1 * 1 = 1
    $ a, ]) O( P3 X1 ^. Q- N& Tfactorial(2) = 2 * 1 = 2
    & C( |& L0 R- B( j6 Vfactorial(3) = 3 * 2 = 64 _2 e9 j$ O3 {0 Q" l
    factorial(4) = 4 * 6 = 24
    $ `: g- u) Z) c2 y$ l& |factorial(5) = 5 * 24 = 120* q' \  X, E. I; q; c. h, Z  a! f

    + k5 q. `& h) j2 u- xAdvantages of Recursion
    - b( \$ D- D4 Z( R4 h! d
    5 e) A3 @& x+ H9 s' Y# O( E/ k    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 d- J* _+ o4 w
      c: T, ^, M6 P  q/ r9 S# E
        Readability: Recursive code can be more readable and concise compared to iterative solutions.+ J, c' Q8 A3 X% Q( _
    + O9 I, T) U, x6 W- L9 U! G
    Disadvantages of Recursion- v, U0 _+ o  r; [: E% \

    3 y) ^* _* V' j' c7 w7 K- ^    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.; w3 T; G5 X4 ^# L9 |
    . R+ N4 T& D9 x4 j0 T
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).- x/ B/ `& S5 z; D8 a9 n* R4 t+ U

    - \! P0 X  o* y* P1 W3 G' XWhen to Use Recursion' G( k% R" R4 o; P9 z) z

    + S( N& B) Y5 X, |    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).1 z; k" X, y" `3 w
    # ]% \3 J/ e- d/ T4 Q! }  T
        Problems with a clear base case and recursive case.6 B  q: A3 H# h

    1 {! `: b; Q9 [. k8 A' L" PExample: Fibonacci Sequence  ]) p( G5 L+ n3 h

    4 X% W# B) ~% x1 D. }The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:) J* d% Q" M" j6 x. I# Q

    & H1 J  m* ^+ f+ n    Base case: fib(0) = 0, fib(1) = 16 w( j& P: A+ I5 Y6 |0 D
    & J$ }6 e: ?1 o
        Recursive case: fib(n) = fib(n-1) + fib(n-2)* `/ {/ @( N; T  r1 v2 h4 s9 Y

    : F! x" [/ d! ?. R) q  ~4 T" l" vpython* w/ W9 `! j0 Q6 F1 e7 f

    - L: d' J0 h5 p1 Q2 q% ?( _: G
    * A/ X; _* T% |" |def fibonacci(n):- D& r6 R$ J( J( i9 q  S
        # Base cases& p0 O3 j- p0 S! N- F4 \
        if n == 0:
    / d" ]/ s- k, c/ F/ m7 B' D) z        return 0( J1 Z7 [2 c$ L/ r1 F
        elif n == 1:& W4 H- g$ S  l0 o% ~. G
            return 1( f& ^1 w, ]& X9 i5 h
        # Recursive case" K& O8 [! M3 W, r+ o) Z
        else:, f! ~9 S- k5 G( |2 d! n8 H2 |
            return fibonacci(n - 1) + fibonacci(n - 2)! H  ~. L1 Q0 D: N; H
    3 z5 a- I& W. M
    # Example usage
    8 F. o" s- W9 ^/ eprint(fibonacci(6))  # Output: 8
      \' l% o- R  Z
    3 e, K! V& K, Y4 z. Q! ]Tail Recursion
    1 R! F9 o, O  s3 y$ G/ t3 t, W" f( Q4 G& }, R* W& J/ m
    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 D3 o% o+ X) ^- R6 v
    . n7 O. b, U2 D- b2 ^' t/ P, r- H
    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-8-22 04:20 , Processed in 0.046755 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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