设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    7 |& ~1 U  S% M" x) r( R6 z' b% `4 U& z2 u- g
    解释的不错
    + k3 ~, K4 k# w, c6 T
    1 o' _$ Q$ I2 p8 E5 E2 J递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。- H6 I, H+ Y0 R- {' X9 n( Q
    ) G$ m+ ]% [' S
    关键要素) v/ [' I, B3 `- j  |9 J, G3 R
    1. **基线条件(Base Case)**
    , n* w( H: ~1 \- Q   - 递归终止的条件,防止无限循环  [0 |; @" P; v/ F% _
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    7 }9 q) Y# T( z  [
    ' S" C% A$ }( u6 J2. **递归条件(Recursive Case)**
    9 I& ?) a- Z* n1 \   - 将原问题分解为更小的子问题$ E* b/ X" k* t  x' O2 {1 o
       - 例如:n! = n × (n-1)!
    $ r: _; k$ W( e* e& a9 g  n
    6 T- S' {; i  _: g3 v/ A1 [ 经典示例:计算阶乘
    * z" J* q4 r5 P* g8 Y; Z3 @" G* e5 Ypython: i4 Y# N6 g! Y1 W+ i
    def factorial(n):3 n. X( r! x  n* S# T- u; |
        if n == 0:        # 基线条件8 t& C% B0 q( |* t, `
            return 1
    ; Z( o' |5 N" n- n& M    else:             # 递归条件) r! o  X" ^  \" V
            return n * factorial(n-1)
    2 D' ]/ V& `1 U# H执行过程(以计算 3! 为例):* N, u* @1 q  R7 A: X: M+ j& G
    factorial(3)
    6 i9 |7 I4 ]8 R1 b3 * factorial(2)
    8 {! \3 X3 Q  R3 * (2 * factorial(1))4 R. A7 C1 G  V$ t
    3 * (2 * (1 * factorial(0)))
    ( [6 }! o) Z! @" y0 n3 * (2 * (1 * 1)) = 6, M# Z, G$ a* i; Y1 {

    6 B6 @+ y& O. h/ M8 E6 n) x' y 递归思维要点
    , u9 K+ @0 U# _, ?+ z+ R* c' ]: p9 L- o1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    3 `0 q3 e; }  Y- {: V5 H. _- p' J$ r2. **栈结构**:每次调用都会创建新的栈帧(内存空间)$ b0 v  R5 n6 q3 @+ N+ g
    3. **递推过程**:不断向下分解问题(递)2 i4 w  L0 A1 A9 b% }8 T$ r2 b( `
    4. **回溯过程**:组合子问题结果返回(归)0 h* c+ [3 j+ f2 }! O+ b; P; ?

    & C4 ^0 H2 e9 u6 p# r" k 注意事项& z* o4 l: T! c( b0 |8 r' Q
    必须要有终止条件
    ( K; x0 h$ d) X% u) V. i递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    5 C$ [" V3 M0 H3 r3 r某些问题用递归更直观(如树遍历),但效率可能不如迭代
    8 s- O8 \4 v3 {4 H; N" r& I( B尾递归优化可以提升效率(但Python不支持): B( `3 z8 q: s) y0 Z

    % W& K/ Q( v4 S; s 递归 vs 迭代
    ; a+ j# x( }+ [9 a- j$ x( u4 ||          | 递归                          | 迭代               |: V% u* p8 g7 V; d
    |----------|-----------------------------|------------------|
    * e8 @+ p4 b  q* Y) R) a| 实现方式    | 函数自调用                        | 循环结构            |. B9 S3 t. t! ]2 N+ j9 F6 w6 d( m
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    7 }& l9 D1 b7 E) I6 q| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ! `* M! {1 S% b: \/ K7 Z| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    " j& v. e$ r) q' k# `8 ]( U. L9 X
    经典递归应用场景
    - h3 o4 R" q2 @1 w1. 文件系统遍历(目录树结构)
    / j* j5 q! b& S# }0 g3 C! Q2. 快速排序/归并排序算法' p* _- w% p8 E! A
    3. 汉诺塔问题; l  H/ h; e3 T/ @( n9 ?( |
    4. 二叉树遍历(前序/中序/后序)/ K8 }- U( b* r  ?: W
    5. 生成所有可能的组合(回溯算法)
    1 ?' s/ E9 H- P( U/ O0 S( n; N) ?( t) b' t' k% k5 g- O& C
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    昨天 07:47
  • 签到天数: 3103 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    : X" K7 M" r5 u! f: r我推理机的核心算法应该是二叉树遍历的变种。' p% M4 s; h3 g) @6 }7 {# I
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:* _7 Q& G! i2 p/ K
    Key Idea of Recursion4 V: N. R0 p* x+ \2 E# ~

    ! u" t. H; e/ ]9 VA recursive function solves a problem by:
    3 z2 t, `1 Y# D/ B& F' k6 P  d1 }
    9 c" |* q3 S, p' S/ E  E    Breaking the problem into smaller instances of the same problem.+ P1 b5 L# |# M; W( V5 {; e
    4 C% d4 ~# D8 K- \- J6 I
        Solving the smallest instance directly (base case).! u; _) z0 |; H1 j% t6 B% d3 M

    - _. \4 ?2 R+ P    Combining the results of smaller instances to solve the larger problem.7 @: }4 q  u$ e3 J: {. V
    : j. h$ S" O0 X' g
    Components of a Recursive Function- U& \1 y( u4 l" ?5 }

    ! J' e# }  |- [/ u# m    Base Case:
      `+ z9 \) @1 M& H2 a. M- O  y# {; p  l% q/ W
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.* h( L9 S/ u+ p# V. ^- E. w& ~$ }

    & X' m+ c; h2 R  [        It acts as the stopping condition to prevent infinite recursion.
    1 P' G# d2 r  |5 x6 T( `7 p* [1 r% }- }1 @8 g- U/ W4 q) {
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    6 P- @& S/ |3 G5 t7 M, B+ R
    , {8 @7 d# B: E/ k0 i    Recursive Case:; P6 N/ ^# N  s3 v+ }0 F

    2 ?1 G7 t5 w# B" Z        This is where the function calls itself with a smaller or simpler version of the problem.  p; P$ ?0 O) \8 s+ |

    * m% d0 ?6 I9 K4 e0 i! Y        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).1 h5 ~. m* [- k& D* |& [
    + h+ \* V, u" C9 v7 p
    Example: Factorial Calculation8 L+ S9 D( P0 `* x5 j8 P5 \2 s

    " I! L9 S+ m/ @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:
    " p0 M5 B3 L) d
    & T# l. C' ?( Q& ~1 N9 n8 `    Base case: 0! = 1
    8 p, z: p  X2 v
    . _' o0 _  \4 Q7 E+ I    Recursive case: n! = n * (n-1)!
    # q7 {- W+ @* P; X" ~, c* M+ f  G0 z" O7 o3 ?$ C( o6 \
    Here’s how it looks in code (Python):! M* b( U' f& A3 J: P
    python/ ^" c6 x6 C0 e
    8 q3 n$ B# r; y3 D+ |

    & L& n$ Z! d' A: Kdef factorial(n):8 O, h: E, ^5 W2 e9 D8 A- Y) w, E7 ]/ j
        # Base case
    # _) o$ v# l: i- Y2 V6 I7 S' v    if n == 0:
      |- I; K  c8 f8 q" o( P: O! w        return 1
    $ H% q  E: X/ a/ g7 z; k# `    # Recursive case1 s: s( t5 {. @
        else:
    ! @7 v, R; m5 j/ F        return n * factorial(n - 1)
    9 j/ ^/ y( z6 l, v
    1 O: b( }6 T' Z/ f# Example usage
    ) V9 _  `8 n8 k& ?- nprint(factorial(5))  # Output: 120) B) i4 ]' U, z  l& ^5 [

    2 q3 c* n% F( L( f1 l! F5 nHow Recursion Works- |( d7 f8 H% L  @

    4 l3 o' z6 ]- ~9 x6 a& R8 K    The function keeps calling itself with smaller inputs until it reaches the base case.$ R' }, I: q: u3 g' i
    8 r& t: [2 R' [" e
        Once the base case is reached, the function starts returning values back up the call stack.
    . f  G  s8 k1 i* x% R
    % e; ]1 A) {0 y4 K  c' n    These returned values are combined to produce the final result.  D; f4 Y/ F; F8 G8 O
    + B7 K6 f  E+ _' q2 \
    For factorial(5):, I+ P% q5 m( O- N0 X' h

    6 ?# P8 L0 B0 F0 H/ {( b/ m7 p% W" Q/ C
    factorial(5) = 5 * factorial(4). h9 i6 @! s0 m5 d/ ~& o
    factorial(4) = 4 * factorial(3)  k) Q# B; K$ |5 s) s$ J
    factorial(3) = 3 * factorial(2)
    8 X* r' P; H; G3 K% b4 _factorial(2) = 2 * factorial(1)! w: N, X" M3 O
    factorial(1) = 1 * factorial(0)0 e7 t/ x3 ^0 @' W$ H- L) i* e
    factorial(0) = 1  # Base case# w; W( D/ W! V" d$ o; b( N/ [

    + d5 [1 L" j# ~+ q2 |7 LThen, the results are combined:! V" L& t/ Q& i  P0 W, N4 x- X
    8 h, n3 Q; U* A  N2 b8 ?" X1 j! p/ u) J' W
    % |* ]# d( a8 C) S. Q0 `2 g
    factorial(1) = 1 * 1 = 1
    5 l2 N: M6 J& ]5 b5 a  Qfactorial(2) = 2 * 1 = 2" q0 ^% n6 S; G7 \2 I5 x: g- p) j3 u
    factorial(3) = 3 * 2 = 6- r: p, T. E4 m6 V- o( i* T
    factorial(4) = 4 * 6 = 24. a# S6 ~% q( h0 O$ T( y  j( J( F  b
    factorial(5) = 5 * 24 = 120
    5 R+ v# W/ H0 A" _
      H, B  c  q5 U4 e* F6 NAdvantages of Recursion
      A9 m9 U5 I' j" b$ r' d/ v; ^' a+ c3 R! x: e  G: b
        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).
    + x7 L* J' w" f7 z1 `, Z
    1 q/ d, D, \8 s$ W: d- i    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    7 C, ]9 `: v1 T$ ~% o  M# ~" S4 P( B+ K
    7 s) [, w0 H7 r& v2 K$ sDisadvantages of Recursion  T6 r0 K$ a# Q

    1 f7 j4 j4 a# S9 a: s9 i    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.
    ) c( c6 {4 C& G1 Z7 P/ s  y+ C5 E% @7 }
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).: P( t' F. z+ J6 L; @
      T% k' l( P$ _  Q7 w
    When to Use Recursion9 `0 @) R: j; x9 g6 Y) \
    ) y  b: m4 Z2 s+ J3 O9 ^
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    0 |' b; p. u/ ]* [9 w
    ; f) y( z! u( x# n9 g: T8 U    Problems with a clear base case and recursive case.7 W( x7 u: c6 u4 r
    2 Y/ x/ A$ C3 q+ M, T' E, k
    Example: Fibonacci Sequence
    . D( V4 ]1 {: D8 J7 r+ Z/ K  d' ^; w% e0 W& Z5 ]
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    8 D& ^; k% r0 l2 i. {
    ) `* A! N% E, O7 u# Y: [    Base case: fib(0) = 0, fib(1) = 1* M/ b2 f: W* ^$ f! ]2 L6 L. P

    6 G- V# Z) a1 c7 L& t( U# C0 C    Recursive case: fib(n) = fib(n-1) + fib(n-2)( _, H9 Q9 U  Q

    6 f9 q+ @, b' d  O- s  jpython- [# ^1 ?" ]; F! u- _6 A
    ! T0 w0 ~) @0 y" F9 u- r
    7 h8 Y- R# d+ E  ^
    def fibonacci(n):
    2 @0 |  P! h% [# @6 n) b    # Base cases
    : T4 u% [. T+ M/ X    if n == 0:# r7 B$ t6 ^+ h0 D* U. b
            return 0
    / d" _" s+ Q& l) l( W- B    elif n == 1:
    5 {: S3 U4 g4 h) {5 r: U, j" Q        return 1
      u# x; Q+ o. L* C4 k. Z" [  ?2 i    # Recursive case
    ; O1 J4 i9 ~- W' R& Z7 V  q    else:- f; a) j- |3 z$ |% }# k
            return fibonacci(n - 1) + fibonacci(n - 2)" F: E" [. x5 L+ I9 k7 B* d( x

    0 \; X& s* B! V8 g! U1 f# Example usage
    " H( r2 D# N. F& e5 P# ]$ j4 Xprint(fibonacci(6))  # Output: 8
    : g6 `# g/ O: v, l: H$ {& O& W7 o' N* }9 R
    Tail Recursion5 o9 {6 Z. `+ A. m, b' Z
    2 X8 B: U, @/ ]
    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).
    ) E) o- i* A! ?+ T4 U; F7 A/ h; ?! _" X/ |5 a
    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-11-29 05:45 , Processed in 0.031450 second(s), 22 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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