设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    / s5 o! D, Z0 G, ^
    2 A1 y! J1 C" j/ S) d# Q解释的不错8 {# L" `* }6 [) u
    ( m/ z, R/ a) e6 \: b2 s
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。% q2 K& v: {1 S5 m
    ; W- w  @# v- z/ `2 R* K
    关键要素) r! a. _3 ]# ]( x* k- S* w
    1. **基线条件(Base Case)**
    0 f( Y: l$ e1 F- ]9 o  U$ `) q. ~0 R& d   - 递归终止的条件,防止无限循环
    5 d) d  q5 s2 R. k# m   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 10 P1 M4 r$ @/ H% Q% S
    7 U/ F8 ?' {; I$ ~) Z
    2. **递归条件(Recursive Case)**3 u# e1 w1 Q' U; `- y
       - 将原问题分解为更小的子问题
    ' ]; P3 B& [7 L( `: f& f   - 例如:n! = n × (n-1)!+ G5 i& M: @' ?

    1 R" h# }3 T* J! Y1 r; x& j$ U 经典示例:计算阶乘
    , m9 S- v  o. N, ]+ ]python
    + V; g+ J* A; T$ J* v+ h7 V) fdef factorial(n):
    % d6 Y0 N) ]( A* {    if n == 0:        # 基线条件3 f: x% K3 V) u
            return 1# V+ y/ q- Z# g/ ^: t& ~
        else:             # 递归条件4 X, V5 T5 h+ J- k
            return n * factorial(n-1)( j8 t. N" `( B6 J) q/ z% F
    执行过程(以计算 3! 为例):' C8 d# b& f6 C
    factorial(3)
    - S' D" @7 V1 k7 d- U* M3 * factorial(2)
    2 B5 Z8 M5 G  m) f4 A3 U8 k/ F3 * (2 * factorial(1))& L; s5 M0 h. N4 J
    3 * (2 * (1 * factorial(0)))% S4 ]9 L% b9 L
    3 * (2 * (1 * 1)) = 6
    ) _6 D- Z' w9 W  a% \8 i' W
    + Z( r0 b; Q/ F0 |, ` 递归思维要点
    0 E7 H, Q" Z5 \! ^1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    , y. f& A! _& F" R1 Y6 a4 e2. **栈结构**:每次调用都会创建新的栈帧(内存空间)' j8 \4 n) n+ ~' S9 l
    3. **递推过程**:不断向下分解问题(递)% q% T& \0 C, E; X/ i5 ^& e
    4. **回溯过程**:组合子问题结果返回(归)% _, X+ [9 t* x7 ]

    . X7 S! ?7 k7 g" m 注意事项' x1 o* l( `; t# K9 [2 B* c' C# A& ~
    必须要有终止条件
    , q" w0 S7 C. f2 t递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    $ G- G6 Y4 S$ p+ z5 a% c某些问题用递归更直观(如树遍历),但效率可能不如迭代
    3 e, @8 D7 ^& _尾递归优化可以提升效率(但Python不支持)# f. o6 z" [/ J& N: P& e4 v) E

    ! g' Z. U5 O9 y' u 递归 vs 迭代+ X- ?* @5 B' L6 Z
    |          | 递归                          | 迭代               |7 d# S/ Q% e/ ~+ N' y3 b" o) O$ |
    |----------|-----------------------------|------------------|
    3 Z8 B) |7 l' g* _| 实现方式    | 函数自调用                        | 循环结构            |
    1 d& N0 G# R, c: c; _) |0 N. A9 T| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |. G+ _: M# e3 ?8 l, [7 k
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |1 N+ R. o$ r! ], g" c! g0 t$ v( Y/ k
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |4 b" E2 i5 R( f
    5 F0 E. J9 v0 }- m
    经典递归应用场景
    ' h3 {6 S- E5 |2 ?) W$ p1. 文件系统遍历(目录树结构)2 d1 `: h: r( V8 f/ T% W
    2. 快速排序/归并排序算法; t; A# P' ]) b: P/ W) B* E
    3. 汉诺塔问题
    9 `6 V' Y3 B; r# C4. 二叉树遍历(前序/中序/后序)3 K! u$ A# _3 B1 P' g) k; g- u
    5. 生成所有可能的组合(回溯算法)4 ^4 q1 l/ ^* C# h7 V
    $ K2 S, Y, w, n% C
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    昨天 06:34
  • 签到天数: 3204 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,4 y: h6 O' l( s" b7 j; R
    我推理机的核心算法应该是二叉树遍历的变种。
    0 y4 m+ r9 H, M( l, Z! Z另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:: p7 ?2 C7 y6 l
    Key Idea of Recursion
    7 l" j7 Z& E5 e; @  _6 B+ `& S
    4 g# h7 p: e: o- g; AA recursive function solves a problem by:& g9 f& P  e( Z( n

    ; e' j1 E) l$ [  h- V    Breaking the problem into smaller instances of the same problem.+ q3 B5 C& I1 z% P
    # K. _9 Y- N+ k1 v# t$ d
        Solving the smallest instance directly (base case).
    3 i4 S+ M/ @+ P6 f0 B
    * h  B; ?  d  B: n. b& {    Combining the results of smaller instances to solve the larger problem.5 k% s; v' H% T; B) u. A
    0 A5 m" t* p' b+ F
    Components of a Recursive Function
    6 {( v& b% g, m- ?$ Y3 S) ?% p9 x- |& Q- x+ k  \
        Base Case:9 \6 H- W9 u  \" p5 K, C" x; B
    8 f0 y% s2 j4 l9 M! B
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.+ v* n: |5 F2 U/ j- j6 }' U! ?
    ; a1 Y3 M: a/ @
            It acts as the stopping condition to prevent infinite recursion.3 s$ K  Y5 a4 d% G

    8 l$ `  X# T# h9 J        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.5 J5 ~5 K/ k/ G& P

    . _2 t+ n' L: s/ L    Recursive Case:( _) b2 C! W1 G  }/ m
    8 p& f* U7 v2 ^9 S* C
            This is where the function calls itself with a smaller or simpler version of the problem.
    ! l) y5 T. W; d$ M8 g9 |. B& L6 k4 N$ W" X2 g* S0 V
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).- G8 ^" z. c7 [: @+ Q
      s4 c& X8 z5 g0 \0 {! g8 L2 S( M
    Example: Factorial Calculation! P, Z2 |5 n; ?& B: R

    ! a% Q/ R% B4 v. SThe 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:
    , m* C  ]8 B$ w4 h- J
    + S/ v3 e! ~* P% f% ?) s- n* W+ `0 e    Base case: 0! = 12 I: x8 ^2 a( j( l

    : \: Y5 l2 z% {$ v4 R    Recursive case: n! = n * (n-1)!
      R# j6 x2 c  N$ B" s6 N. e+ o2 v" a8 P$ X
    Here’s how it looks in code (Python):
    1 Z+ O* O7 ~" i3 n5 Z5 ~) Q  Epython/ e+ T1 E' G% H9 J( o* n7 u

    ; y2 R8 q, ]& O9 U. b; o. b# D6 X/ H5 i# U" Z& Y2 m3 j* R3 H8 U
    def factorial(n):
    3 K7 k: v: X) k7 D8 ?    # Base case; U' o  e% e# C7 |
        if n == 0:; }7 D7 x# f: {
            return 18 U5 i+ T2 W- t
        # Recursive case
    # ]2 f6 A! }- t/ }9 M    else:* E+ \( m. I, n  ~% X4 |6 C
            return n * factorial(n - 1)
    * z7 F: C' a( A% Z; x2 ^2 R0 Y8 }% M
    # Example usage
    6 S4 O; Y( A! ?8 A, Z/ M" _5 v* gprint(factorial(5))  # Output: 120
    + X9 i. r+ i: K' |* J( w0 f7 j
    3 o: C, R8 a& l, m6 xHow Recursion Works
    & v0 K; X2 B: V) Z2 `1 H! c( [9 i  N
        The function keeps calling itself with smaller inputs until it reaches the base case.
    % ?9 v7 D# ]7 U* E# R, }0 r, t; l3 _+ J8 |/ l
        Once the base case is reached, the function starts returning values back up the call stack., E6 q# S! u1 D+ v

    1 @; P9 t4 u( p7 u    These returned values are combined to produce the final result.
    " S5 c9 D$ I  s) q1 g+ G7 Y3 ~5 p  q
    For factorial(5):! |' w3 H* |0 V3 ?' I% C2 E% K0 c& {
    0 Y3 X5 `( {9 ?# H" r* _
    8 i8 }' i' W. m$ ]% r- M0 L
    factorial(5) = 5 * factorial(4)
    1 q5 E. I" T' T; G& J! l: D& hfactorial(4) = 4 * factorial(3)3 R4 e9 D) V1 H/ V' h
    factorial(3) = 3 * factorial(2)
    8 ^8 j, }; q0 _1 M. ]8 l% e" Cfactorial(2) = 2 * factorial(1)
    . L: ?* F  P. [* i" K5 \factorial(1) = 1 * factorial(0)9 @6 J7 L: u1 {( j4 F
    factorial(0) = 1  # Base case7 M8 b; E# u0 ]0 F) L8 F
    % P: j2 U+ @+ B& _- G& g+ s) f$ J
    Then, the results are combined:) b7 S! m9 ?9 [5 A: n9 y

    . i  }2 j  B& J  X  Q7 d& S; o
    ! ?" X: c0 b/ ^& J- v& |factorial(1) = 1 * 1 = 18 _( x5 Z1 |! n6 g9 |6 d
    factorial(2) = 2 * 1 = 2
    8 d0 Z) v3 n! W# v6 Bfactorial(3) = 3 * 2 = 68 U5 u+ U# [3 ^8 {( w" ]* n; N6 W
    factorial(4) = 4 * 6 = 24! U9 Y* w" @7 |7 E  c! X3 H
    factorial(5) = 5 * 24 = 120
    " d5 q7 N/ ~( u/ k+ J# g8 `9 @6 Y9 E% {7 K4 R% T
    Advantages of Recursion
    3 {: Y2 X4 W5 a& I! ^1 m+ |+ H+ d9 X7 K2 ]
        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).7 w6 k% S: M4 A+ n: p

    - f! X% K! K! I/ r  P; K4 o    Readability: Recursive code can be more readable and concise compared to iterative solutions., V! c9 q$ R8 I4 X( [9 U
    & N* b- c% W, L, W  b
    Disadvantages of Recursion: u# e( h" s4 T3 h9 w& D

    , A0 B1 X( f% K, r" 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.- [: |, t4 u( M9 y5 c6 j
    " W9 N: e3 D$ _6 \- U$ E% D
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).; A/ Y' z  ^3 e( d# ^

    ( O0 R1 t# V6 ^) w( NWhen to Use Recursion
      A, n! f' n7 m' ]: X3 w# K6 W
    . X! i  Q2 f* X6 c0 l    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    " p: m: W9 |% D  M- {% d3 [
    & O% \- w5 F$ \$ G    Problems with a clear base case and recursive case.( _/ k% a% g3 U, M' D
      w6 S  Q/ S, a
    Example: Fibonacci Sequence
    6 m$ I8 ]9 i; {0 D, _4 _- D# t; K$ o2 q4 E
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ' @% s0 w  }, }4 r  r7 J6 j8 M4 q% y& l8 Q0 Q3 B
        Base case: fib(0) = 0, fib(1) = 1
    7 _6 V0 O9 f1 Y. i
    ' w2 f3 I: `* j4 E    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    . h3 v* d' \1 G+ P' `7 ^4 _$ b3 [) B- t
    python; u  `9 b6 H, ]& Y0 i

    / {, ^, s+ x& q% K* a2 V: j) n0 h( m% V5 M1 A) N, k
    def fibonacci(n):  `& A- y% a. y5 q# c. `! s
        # Base cases% U: _/ d* T& W" R# O3 x
        if n == 0:
    5 c, t1 E9 O: ~        return 0* c; `' H1 H+ E; f/ f3 B/ a
        elif n == 1:7 P# V- w+ ]& |" S* v$ v2 o6 S+ }
            return 1& [! R/ V1 ?$ F* T- U
        # Recursive case
    2 L5 T8 W; y  {    else:
    ! f# ?% U% I) \6 O2 K9 O) g        return fibonacci(n - 1) + fibonacci(n - 2)
    / x) f/ y, d( |0 E$ c( P  @& A4 Z* U3 H
    # Example usage
    - M0 U) h+ f, v4 Zprint(fibonacci(6))  # Output: 81 i( c$ a! I3 m1 @
      G) ?: D9 `( c3 _5 N. @
    Tail Recursion( f- ^  N$ T  X* a9 `
    ' w9 C7 v" v) I$ g0 C6 `
    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).# V( I, C: e  P$ A4 R1 i: `' E
    5 w4 E+ {& F; W! ]/ a+ q
    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, 2026-3-19 01:38 , Processed in 0.083767 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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