设为首页收藏本站

爱吱声

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

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

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

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 : H- _3 h; m% ~! k4 }

      w% V: w' ]1 f0 O) g解释的不错
    ( z1 t6 M. Q9 M  X9 i- U; U
    ( u) ^+ d* m6 r3 T# w/ W* s7 [递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
      O. O- }3 f- O( k. \3 _+ v! k; f: V/ D4 r; y
    关键要素
    1 R- }8 x/ b* E. G  m* i8 F1. **基线条件(Base Case)**
    " P! n7 D& c% e4 O- [6 r% B3 S   - 递归终止的条件,防止无限循环" [2 o. [) D: Y1 |/ ~4 M
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1! p5 Z& o# S6 K
      N# x( Y2 E/ o* N; ?+ ?) J7 G
    2. **递归条件(Recursive Case)**' R" f% W8 C$ m) |9 Y" D3 o
       - 将原问题分解为更小的子问题+ s7 ~/ _$ t/ I. {5 t2 Y/ R
       - 例如:n! = n × (n-1)!% E! j2 ^, ~) p% s9 o9 L( K# \
    3 e$ m1 G0 ]: V5 [8 n: g
    经典示例:计算阶乘
    : p0 g, c8 ^  \% J5 F# @& \! ~python+ g% a7 c  W% B1 Q7 Q0 g/ v
    def factorial(n):
    + j5 f7 F( l# t    if n == 0:        # 基线条件2 ?# y, l+ x* b' }
            return 1
    6 Z; y4 P9 u0 e    else:             # 递归条件
    4 K& L  H( i9 e8 f        return n * factorial(n-1)' L6 }# v5 N+ [0 F# B1 z! n
    执行过程(以计算 3! 为例):$ B8 O% h2 x, _! t. d) F$ k7 R* I
    factorial(3)$ {+ j& W2 K) `
    3 * factorial(2)5 u& g6 P' b. |) p/ z+ m. o# J9 [
    3 * (2 * factorial(1))' f) m' ?; D: T- \0 {1 a! P
    3 * (2 * (1 * factorial(0)))0 x" _0 i  J* X  T: r, n
    3 * (2 * (1 * 1)) = 6
    , T9 d8 d4 T4 L5 B6 _8 z! T1 I. P* p3 u- Z! _, \! k
    递归思维要点0 C; x. J' K+ E, C( S: w: p) W
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ; C8 [5 x1 n/ d- D& p  p2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    . O9 l/ u7 s: a3. **递推过程**:不断向下分解问题(递)
    # M7 u/ {9 i, `9 F/ X& [4. **回溯过程**:组合子问题结果返回(归)
    ' z' q* c0 `: F  I9 Q# p# ?  ^) y' ?& n6 v4 Y5 A' v
    注意事项8 l6 V: t  \0 P3 o6 {
    必须要有终止条件
    " p9 ?) F) z) O递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    9 y# t6 D$ j4 n+ _$ K/ \1 H某些问题用递归更直观(如树遍历),但效率可能不如迭代* M9 h, h/ c$ P/ o; V( ^
    尾递归优化可以提升效率(但Python不支持)
    ! g6 L- n. }7 x& i9 W( `
    3 Q/ M$ T" n) x2 D3 I 递归 vs 迭代
    & R3 H" e: n/ D1 v|          | 递归                          | 迭代               |( l2 z; u. l% G' i( ]
    |----------|-----------------------------|------------------|
    8 e* }* D3 E* _2 }* }- t| 实现方式    | 函数自调用                        | 循环结构            |
    2 O7 @- J; n: {1 F| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |; y  A6 |( L: a* ?" x
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    2 k9 K5 V9 d" J( R| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |+ N4 }' A; J5 k- Z' K
    5 X2 M! F6 \9 }+ ~3 l; o
    经典递归应用场景
    % _8 D+ s; L5 Q0 Q1. 文件系统遍历(目录树结构)0 d& k  l5 F# I) `3 G
    2. 快速排序/归并排序算法; M+ o4 \( a  \: M) m" }
    3. 汉诺塔问题/ x  T7 s1 x! I, S
    4. 二叉树遍历(前序/中序/后序); p+ ?$ F, l* B; V
    5. 生成所有可能的组合(回溯算法)
    , X  v, v* v$ s( V
    " [  U' H! J) p试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    17 小时前
  • 签到天数: 3008 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,& N2 n6 C$ p1 U( p6 z/ K
    我推理机的核心算法应该是二叉树遍历的变种。6 `) {* S1 L6 F. W
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:" c5 z  T9 i3 q
    Key Idea of Recursion8 h) B& @$ E5 ?7 K
    2 d8 D7 O/ T- }; B9 G3 D9 D1 r
    A recursive function solves a problem by:
      [) ~# M7 {1 n
    . X3 n$ ^( h. f4 Q    Breaking the problem into smaller instances of the same problem.+ @0 c; i, L- K5 @& [! j

    2 C" x, b) s+ v6 k6 C& L0 q    Solving the smallest instance directly (base case).
    1 @- Y1 l& U- R2 e" J
    ) ?5 D0 E- F6 Q8 M& L" ~4 [    Combining the results of smaller instances to solve the larger problem.* L8 g' N% b# Y5 B* Y+ ~- Y

    ) }. q+ Q+ @* Q: K) WComponents of a Recursive Function
    0 ~! @6 R3 y% B: H: s% M! g/ b/ w9 L
    0 T; @. {; {4 E8 C2 _    Base Case:
    # I. b  z2 {' ?, Z2 A( Y' I, C8 V1 U. ~- X8 J$ V, y
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.# k! l. w/ A% T, R" M
    ! H. q4 J9 n: b: ]6 }/ a
            It acts as the stopping condition to prevent infinite recursion.
    2 J% v2 L% L. R7 n2 Z; c1 O- w$ r- ?
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.2 _- o% w% ?: _  U! Z6 y6 b, ~
    6 m5 G* J2 N' W" B4 n8 v# [
        Recursive Case:7 O6 x0 Y7 `; m+ y  {9 K" Z, x. l

    3 X, W1 A8 g/ Q- ^$ H4 L9 T1 s3 t        This is where the function calls itself with a smaller or simpler version of the problem.
    % v  o7 j2 M5 y; v4 N
    . I0 o3 Y* q- t0 y$ s        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).4 y4 W. L: N* V* E! M8 T

    3 b: ~* ?' D9 V# mExample: Factorial Calculation/ h5 B: |- P2 N% f4 |

    2 S3 _+ `2 d" ]2 M, `/ KThe 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:
    ( H$ ^& q& h0 _/ e( d' Y
    9 g* d/ w( f( G5 O) _" L+ {    Base case: 0! = 1
    $ q) g& s" \8 ?) S, ^- X1 Q% s! t6 \- V# L
        Recursive case: n! = n * (n-1)!; w3 A8 g: a  W* @  S. n: l" C

      @+ q# u* E; y, t, d$ d$ n) HHere’s how it looks in code (Python):; R+ r) N  f5 l9 y1 A& C* E
    python7 [( F1 h$ p$ d7 H6 F3 W3 y

    ) u, t" p* |/ e  c, i. Y) _, I0 R5 Q. N
    def factorial(n):
    ' o$ j. u) b* e0 A    # Base case7 W5 r  W! @$ ]  X! t
        if n == 0:. X1 ?( G, \9 T; {2 a
            return 1: @  o& s9 y5 ^1 d. }
        # Recursive case+ L% T! s, p/ N) X1 I$ h5 Q; Z
        else:& \6 B7 h6 b# e3 R
            return n * factorial(n - 1)
    " n* M7 }. W7 f. ]: a/ v8 S8 z( R
    # Example usage0 ^2 e; H6 g4 B& j- O
    print(factorial(5))  # Output: 1204 H. ?! b; o  }7 m* @

    : |% l( T. K( S4 T8 D, s0 F: E5 n6 SHow Recursion Works' M. R- C+ ^9 s" S

    ; ^/ e; z7 S0 t    The function keeps calling itself with smaller inputs until it reaches the base case.
    0 E+ B( v+ a: ?' G4 u/ X1 G6 k4 B
        Once the base case is reached, the function starts returning values back up the call stack., V1 v1 o1 u& _; s8 H
    $ F- Y4 [! g3 y
        These returned values are combined to produce the final result.
    + L) y' Y+ _; D9 B
    ) {8 [9 u- K) R( h" r+ t1 p% SFor factorial(5):
    ( d+ ?( l- ]) U* c# Y# L: D1 g5 {0 K4 q7 e

    ' Q& p0 I6 v' A5 O2 b3 M4 afactorial(5) = 5 * factorial(4)+ e3 p. ~) X+ M9 l; I5 u1 R/ `
    factorial(4) = 4 * factorial(3)
    * g+ @& E2 l' F4 n* {$ Cfactorial(3) = 3 * factorial(2)
    2 _- z' u0 }  p: C  y5 D3 N4 Zfactorial(2) = 2 * factorial(1)2 u* `7 M8 n3 l
    factorial(1) = 1 * factorial(0)
    ) P0 U0 F8 s# D3 M* O+ ifactorial(0) = 1  # Base case" A, @. [- u, A1 E8 M$ l% `- G& k
    & G: @9 Y, |, z) K/ ]7 h  x
    Then, the results are combined:
    ! Q3 e- I0 j- r8 Q8 ^3 R& S; [$ i2 _' D6 h" Q( ]

    1 {( y0 e( E, e6 X) L0 j; Efactorial(1) = 1 * 1 = 1* A( k2 J3 y' ^) p* o, K( }5 E, T
    factorial(2) = 2 * 1 = 23 P: E* x* e1 x! J# d
    factorial(3) = 3 * 2 = 6
    . Z7 h6 F) Z) wfactorial(4) = 4 * 6 = 240 ^' c/ z! a. |' \1 ]: N6 n0 D8 s
    factorial(5) = 5 * 24 = 120
    4 E$ a' \3 p( f* R4 M! g  ^8 L# v
    - t3 ^7 C+ {) Y9 Y& tAdvantages of Recursion4 U; x7 g9 P4 T- y
    ' Y8 M) {* f6 E3 J
        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).- h7 B6 O9 s: [& q' S8 E

    " I. J- H. O" ?; f    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    : G" V' b( D) k& V7 p
    - a6 Y! x0 U' q- VDisadvantages of Recursion! }- `/ i) ^" w4 R& j6 w' s
    & t0 m; |1 C8 d3 _/ 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.
    ' b* ?* A& D5 X" d, Y, G: [, x) b. x( b( {$ S+ B- C! u
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).+ k. N  C9 [+ Z% w+ D5 }, \
    9 T- n5 A2 k( U$ B, [; \
    When to Use Recursion3 Y0 \$ b& K" M5 X6 \* T
    $ ^; D" w. ^5 b" A1 S  l7 y4 Z8 z
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    # \# Q& o5 u5 B7 ?" ^9 F/ P$ O- q, {9 }5 Y! N0 a: m5 W; D7 K
        Problems with a clear base case and recursive case.
    0 ^- d$ L( w( O4 g8 N& Z
    ) ]/ |: ?1 a$ R% o- S% m; B/ wExample: Fibonacci Sequence9 l4 o# l* Z# R) A
    - D0 z- Q3 |+ j' \" r6 E$ ]% Y) _
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:2 Z% d4 z) n2 b3 C
    4 m6 Q7 m" h- n2 g3 a6 M0 q
        Base case: fib(0) = 0, fib(1) = 1* o; V. a6 x% ^3 \; `: j% q$ n
    & V" z  I' R( ~- h4 ]& a, w
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ! j2 w6 I4 n/ ]7 A" c6 P% A$ L! Y, M" U1 I. N
    python
    1 J' U' d) `/ Q2 `) g" c: f0 x
    1 |% d9 h$ e9 a- }; a
    ' c, r7 z2 v; T% ?+ c' K; H: Adef fibonacci(n):
    & i+ y, Y8 a$ [( K    # Base cases
    ! }/ `5 J3 M6 W8 y% b' _+ T    if n == 0:
    / h8 H& P8 U% C- T. Q$ Q- `        return 0
    ) j9 q/ M$ I, j7 a7 z- ~5 H! h1 D) Y    elif n == 1:7 E' ^9 m# s, A
            return 1
    + A6 G( e+ t2 V$ q- B& i    # Recursive case9 y- R4 D; c' Y
        else:
    / `6 ]3 r, l& ]+ ~. W- C        return fibonacci(n - 1) + fibonacci(n - 2)0 x( f6 h, h, l" Q

    ' n1 [& x. y; _6 I# Example usage) x6 y- G2 k; C: v
    print(fibonacci(6))  # Output: 8
    3 J& ^- d8 m- E+ E6 b3 f& c% d/ {) u/ f5 s
    Tail Recursion- E; l7 w# B( G4 s7 G/ G

    3 P# k, a4 _, ATail 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).
    & j& }; ~$ E6 @2 y$ @9 Q, D8 C
    : u7 Z: @- V8 {- s5 [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-13 23:31 , Processed in 0.036476 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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