设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 - {1 u$ f! K- A

    + X( C8 g3 y! R" W* r- a/ n  @( p解释的不错' v: J! h# z  y6 W& \: O
    5 I) Q$ `/ @" \: l: }; A( ~
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。" a  v! {1 L, u$ K

    + b2 ^) }) d2 h* E' R6 ~ 关键要素7 v) s5 c$ W  h' j" O
    1. **基线条件(Base Case)**
    9 D4 {$ h/ B" s/ C1 |! v  P   - 递归终止的条件,防止无限循环
    6 W1 Z. d! H3 J6 ?+ h6 S6 k0 Q   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    4 \8 q, K/ D3 ], ~3 M" F: R2 q2 r4 X4 i8 g4 E# |7 _6 _
    2. **递归条件(Recursive Case)**9 U0 G$ W9 t* \: s8 m
       - 将原问题分解为更小的子问题
    , M: d8 f  m9 w2 o  B1 m) R& \; A   - 例如:n! = n × (n-1)!0 r% U0 p2 l, }: A1 N( B' U

    / [# D" t3 U' g# d- O9 b& L8 b1 i5 n; X 经典示例:计算阶乘
    9 [' C3 l6 ^2 }6 f/ Q) apython
    2 h0 G, W4 r( x! g6 ndef factorial(n):9 P& [4 L" o0 Q; |3 m  }$ Y
        if n == 0:        # 基线条件- s! @/ f; W) y" v  L% B% ]4 F
            return 1& [* d, l: f" |3 L( n  N
        else:             # 递归条件) P) _; |3 i; o
            return n * factorial(n-1), T+ K& ^) m9 b5 V; m8 R! f
    执行过程(以计算 3! 为例):
    7 G" u/ C( f5 {9 d8 f! Nfactorial(3)) a5 l: ^" x1 J; v. m
    3 * factorial(2)
    + w/ [8 ^; D0 B0 o3 * (2 * factorial(1))
    ! |7 ^4 t0 b- K8 @' h8 H3 U1 F3 * (2 * (1 * factorial(0)))( O) a0 M: M3 X( K9 Z! |
    3 * (2 * (1 * 1)) = 62 N8 }# q. [8 ]2 w. R
    . @/ v+ Q3 G1 n, i- }
    递归思维要点1 k) w0 d! F0 e$ c+ |1 D
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑2 ]% K4 ^1 L9 `: o9 I
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)* u" Q+ ^' G5 Z+ Y
    3. **递推过程**:不断向下分解问题(递)6 X3 K' S* y% K6 m$ L: _3 z8 S- b
    4. **回溯过程**:组合子问题结果返回(归)
    + v9 ]7 q3 J+ l% m5 I: p5 Y/ o& C1 W' `3 `. s. a/ u
    注意事项
    ' _" _" M( |# A  D% [9 l6 }必须要有终止条件. E6 ?- j4 X0 ~# {- R1 R1 O3 H4 h
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)/ K7 ~8 _3 U  S3 [2 s; ^/ L
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    7 p$ h% u; Y  l6 B' r: I6 P* @: H尾递归优化可以提升效率(但Python不支持)- C  g. J% H$ N4 ]8 U% {
    8 G& q' }7 L* P; @2 L
    递归 vs 迭代
    ) s& }) Z6 V/ ?6 _|          | 递归                          | 迭代               |
    - K. n3 I/ D* h# h- n# R|----------|-----------------------------|------------------|
    6 b. G. c" S  E- e| 实现方式    | 函数自调用                        | 循环结构            |
    & T) O3 @$ e) ?. j| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    5 N2 X! j! l7 G. y6 g- N! L/ j| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |( Y6 f" W9 ?7 _& I! t, W# b0 S( h% Z* l
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    1 V6 Z% c1 P( y( D; w& B% t$ g+ _- C8 X
    经典递归应用场景; u4 T1 J& Y" ?3 A8 b; [4 ]# e9 b! J" p
    1. 文件系统遍历(目录树结构)
    . R1 r  V, \$ n) R1 {6 W3 O  G2. 快速排序/归并排序算法" }& \$ K- r: R1 i. Q
    3. 汉诺塔问题2 |4 Q1 [5 c  u
    4. 二叉树遍历(前序/中序/后序)1 r* p, _2 `8 m4 ]* S- j
    5. 生成所有可能的组合(回溯算法)
    " n, B: V. c. z/ T2 U0 a
    ; g1 t9 I$ M1 L9 \- o/ O7 ?试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    昨天 07:37
  • 签到天数: 3085 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,) d0 ], y. t3 Q4 Z( L0 `
    我推理机的核心算法应该是二叉树遍历的变种。
    # [3 W9 h! Z. p% k. ]+ s另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    3 |: Y& v! R5 a- |! u- y; ?% aKey Idea of Recursion! i2 n1 j  ~  G
    9 D6 o9 y' _" ?
    A recursive function solves a problem by:
    & v$ S: a5 k1 |2 \& t+ g. ]. G, {$ A+ P) t9 B- {' ~. S
        Breaking the problem into smaller instances of the same problem.( F4 c& q) b6 T
      c& |0 T/ M) ]* j) V
        Solving the smallest instance directly (base case)./ S$ m5 r/ a; c. b0 S
    0 @( j/ m! w' T; b) A7 a
        Combining the results of smaller instances to solve the larger problem.; W+ u; J$ V" U
    8 X" v' W7 y6 F) c# f
    Components of a Recursive Function- M4 M, A$ d/ m) p0 e* M
    7 h, ], u" W' [
        Base Case:/ V% K2 k( M5 @

    2 q- y6 ~0 v" L9 |' L8 m7 u        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    # a6 c9 d! E$ W' v- F* A. m* S& O  B
            It acts as the stopping condition to prevent infinite recursion.
    7 n9 @, P$ b+ E1 s6 i' i8 c0 l( t# C! e& U+ i2 o" ]
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.5 U( o$ v8 {: }: C9 ~  z7 X
    * s. Z: x3 d; `& B
        Recursive Case:
    ! s1 V/ d5 E4 y+ ]$ \9 [, B
    6 [) c* _! j7 G- J( _' j        This is where the function calls itself with a smaller or simpler version of the problem.
    0 ]0 W) q  }$ |; q# V
    # U+ f" T" x. ~        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).' |4 B: V% X( X4 F' W: B" I" p( N
    % ?) u$ [; q$ o% @# U0 n5 n
    Example: Factorial Calculation
    7 s% e4 r& y& o
    % C% @( ~1 T1 {. W1 JThe 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:
    / ?4 \# u- z" T5 N3 C" g3 ]' \& o; V( E% J! v
        Base case: 0! = 1# u3 l, P1 `4 b6 @+ f; B$ }1 C

    ; U1 @0 ?, r1 F2 ~    Recursive case: n! = n * (n-1)!+ N( ^8 B; B- j6 f3 _

    . _$ z1 f4 G$ k( sHere’s how it looks in code (Python):
    8 _. k: K# h+ M6 tpython
    6 r$ a$ o& L% c: q% g1 g+ r4 U/ y$ n, ?( {
    % s* A/ ~* Q7 y( ~
    def factorial(n):
    8 ?6 k3 e6 p9 `$ d& F) d' z    # Base case5 K" a4 H. W( W3 _3 O4 `
        if n == 0:) X. j1 S* I' @" ?$ I: n; S
            return 1
    : ]/ f0 ?% Y, z* W    # Recursive case
    / |. i; f1 y+ Q2 m; Y  w    else:5 l2 _! W" K. I/ y5 H' U$ l: q
            return n * factorial(n - 1)
    ( R1 b9 k0 K$ g# C2 N1 ~0 N2 O9 W& z) T5 n" P1 `
    # Example usage7 Z* }2 B! I3 S7 j* G! s
    print(factorial(5))  # Output: 120
    - |/ }% p- h& K, A3 j7 K
    6 G2 i- \4 e2 xHow Recursion Works
    - z  r4 A3 V9 f
    6 T: b% J% i2 }) m6 b    The function keeps calling itself with smaller inputs until it reaches the base case.
    ! V8 t9 p7 E) K& t* `
    : W; ?( g/ ?, S) c. j* V$ p    Once the base case is reached, the function starts returning values back up the call stack.
    , k6 I% m7 a4 r% s$ N) K% u3 `
    $ [. _' h5 {9 q! f- }; i    These returned values are combined to produce the final result.
    # f, }" \$ g3 c/ o% `# p1 k+ G
    4 g3 i' X* V& e4 lFor factorial(5):5 u+ P9 E& R. x4 O# H8 t% r1 ~

    , V! S0 L/ c0 L9 V4 D: p* t' ?% t6 d5 S% A
    factorial(5) = 5 * factorial(4)
    1 j% T8 k' i: M; m  `factorial(4) = 4 * factorial(3)
    ! ^: Y* j+ U) ^/ Y7 g8 Ffactorial(3) = 3 * factorial(2)
    7 R( J( X1 i% j& A5 Vfactorial(2) = 2 * factorial(1)
    7 z2 o4 b0 \+ j1 S. L& ]8 Nfactorial(1) = 1 * factorial(0)
    - `( Y2 ^* o0 S. @" H7 C+ @6 Lfactorial(0) = 1  # Base case
    - S. m* G: _5 N9 `% ^: M- n! ~. n* }
    Then, the results are combined:+ q5 Q5 e; a- L5 ?  |

    5 H: z+ ]. _6 ?
    % w) g5 @( ^% j! f1 Xfactorial(1) = 1 * 1 = 1; s9 L2 D/ H' q1 T
    factorial(2) = 2 * 1 = 2. H# n; G7 `% j3 M
    factorial(3) = 3 * 2 = 6- w3 _7 T& g) y* B4 r3 a( G
    factorial(4) = 4 * 6 = 24: n% v" [$ ~6 D  |( `' \+ u
    factorial(5) = 5 * 24 = 120$ v0 |# k, h2 l4 W. A
    ( v; X/ P: z$ L7 E- ~
    Advantages of Recursion  A) r+ S" E( e( r" c

    8 A; {/ J0 [) T  T1 m    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).
    * U. C/ Y, w/ W2 I2 A6 t% t7 h+ K
        Readability: Recursive code can be more readable and concise compared to iterative solutions.7 O% @1 v* t( D5 x
    " W/ Z2 l, K+ O! {
    Disadvantages of Recursion
    # l; W. z! B+ m9 K" j
    ' @  g2 v! W( Q7 p0 p1 w    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.  {3 B9 ^8 g" M2 z4 J

    $ H' `- S, i" k6 s1 p    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).3 o! n3 s- R% b; N
    ) x! o0 S7 F( y0 m# B
    When to Use Recursion
    - t3 J' g7 m  q" N6 S3 W$ y3 L. M9 S6 B) V5 J  e' ?
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort)." Q9 k* l) M# V9 q9 F; ~8 K

    ' b" `' x5 Q6 D* }4 r    Problems with a clear base case and recursive case.: T& x. W( y/ }& ?" Y' r

    + F: y7 _# \  o% T3 R; b4 K; N! w3 TExample: Fibonacci Sequence
    ' o) @/ H) ^+ H2 w: H& d5 O8 ^' X% l, f, I
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    2 Q2 n: O1 Y/ W# w6 L4 V
    7 ~8 ?# a$ D' n    Base case: fib(0) = 0, fib(1) = 1
    * G" H, h6 J( d3 D$ X
    ) _2 q  ?6 y& J0 Q$ g' d8 l    Recursive case: fib(n) = fib(n-1) + fib(n-2)0 Q* z* g( h) B4 M

    - ~! _6 ~' d/ K' _) \9 bpython
    ( k% s' F9 d! M% b: \; U  k; r& w/ A
    % [9 }5 X- N' B7 s/ s9 E1 R) k9 o: d- ~3 X$ {0 V8 e$ y/ g
    def fibonacci(n):7 I/ T/ X7 h7 X$ u/ ^" Z
        # Base cases
    " E+ N( e7 ]9 [9 h# E6 k% G    if n == 0:
    5 j) p2 A( H* W' R1 J3 T( ]4 r        return 00 T9 T$ c/ v2 ?# I3 H
        elif n == 1:
    3 V; T  R9 Y4 A" W4 ~        return 1
    " n" D, H: Z: {5 l0 f. D    # Recursive case: O$ k8 B8 y: w0 L; N' x+ O5 Y
        else:5 Z: u' O7 E% A) q/ h* B
            return fibonacci(n - 1) + fibonacci(n - 2)
    2 a# k; p! h% x; A( P. W) U. S5 ?  m/ v2 `
    # Example usage* K8 o/ {' X+ K# S, v, R
    print(fibonacci(6))  # Output: 8
    / |) w% {7 u; ~8 v; f. |* V9 T9 F* f
    % n. t! G. P* {" S1 O) Q3 tTail Recursion; L4 ^. R8 l/ Z

    % ?/ ]2 K  \! C% ]3 ~  ITail 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).
    : c9 u8 {. D0 q$ }0 i
    7 Q8 B1 j9 y# A+ FIn 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-11-6 18:00 , Processed in 0.032491 second(s), 22 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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