设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    , G# n3 _% g* h) r0 v' P, d' ]7 H+ `0 d5 t
    , F; W0 r- z* \! g; z; e解释的不错
    . o; z, z1 h- s$ H) U1 [
    " s$ B6 ~! ]0 F2 h! z递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    1 R' I! s3 f5 ^; N
    " b! c( m7 D" C5 J* r  m! Z7 t 关键要素; Q. `; R8 \. o* g
    1. **基线条件(Base Case)**) G& F0 i. {( a% P8 _  z
       - 递归终止的条件,防止无限循环  y( ^5 \' I6 @* q! q; B
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1  i# t7 C4 s9 w4 C- h
    ; m8 h8 B- ], z$ {$ S; D
    2. **递归条件(Recursive Case)**
    9 w6 r% {, e  Q0 H   - 将原问题分解为更小的子问题9 s- k2 m' C3 ^2 B* a/ \% P
       - 例如:n! = n × (n-1)!9 z# Z/ k8 W/ B7 P6 V8 ?% M- x

    . V$ [" |! a1 Z6 j 经典示例:计算阶乘, W% x. f& s3 K4 h' p
    python: A% g; m  O! U5 O4 O5 N- M9 b
    def factorial(n):
    , P( b& }& @, T  ~; A$ V% L    if n == 0:        # 基线条件+ @; {8 e. D! ?3 K' M; M
            return 13 F7 x! S, }$ q- a
        else:             # 递归条件
    * W" ?2 S. I# i        return n * factorial(n-1)
    $ H# u; ]. O3 N' ?6 F4 V执行过程(以计算 3! 为例):2 |) x( `! _1 `5 V1 }0 }
    factorial(3)& f/ h& z% O3 g9 B
    3 * factorial(2)2 F- |. [+ A$ U( U& A
    3 * (2 * factorial(1))9 x2 b/ A: G, W8 ?2 ~# q
    3 * (2 * (1 * factorial(0)))4 p4 P0 M8 f- u$ V+ R4 S7 s: l
    3 * (2 * (1 * 1)) = 6$ ]1 W! ^7 v2 ?/ O  @. u; F
    : h" _% L6 C, N; h2 L9 y
    递归思维要点
    0 R/ ^, d0 J0 U  S8 L% l1 C1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    9 y9 m' Z  S8 [0 {. f2 g2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    7 p- Y6 p+ M8 J; e/ _3. **递推过程**:不断向下分解问题(递)4 I+ F' N, [, k! o' [; q8 B
    4. **回溯过程**:组合子问题结果返回(归)
    " z5 k6 S8 n$ h' p/ ^6 V1 }; ^: \# d% M! a
    注意事项
    ! ]. B7 w* G1 q7 M必须要有终止条件
    / @0 M: b" K: V3 z8 G2 m, D( V! I递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    1 ~; ]( P- S- j7 y某些问题用递归更直观(如树遍历),但效率可能不如迭代
    * d8 j* C# M% E$ L; v$ W尾递归优化可以提升效率(但Python不支持)9 Q* V9 ^. Y2 G- d
    : w- G' U( ]6 p( r0 c
    递归 vs 迭代, o% y' E) [3 `  Z  A9 ~4 q0 a9 B
    |          | 递归                          | 迭代               |
    9 u3 Z$ T4 E0 d+ p8 w|----------|-----------------------------|------------------|% u; |+ D# R# U1 G: t0 q
    | 实现方式    | 函数自调用                        | 循环结构            |6 c# |( M2 v3 u% a( w6 ~, y
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |8 c1 ]) Z& Y& ]3 g* n. s
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    6 m; N2 B1 x" ^& h8 X9 g| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    & N4 G( z! a& n* O
    ! e/ ^, c+ q8 Q: G9 m# L5 V 经典递归应用场景. A; D, H5 h  w* d
    1. 文件系统遍历(目录树结构)2 A! a6 X2 u# h5 B$ F: P; F
    2. 快速排序/归并排序算法
    3 \7 a" G1 v7 Y* M& j; g4 J3 G3. 汉诺塔问题" b9 B" K8 h; R- z# O) E
    4. 二叉树遍历(前序/中序/后序)% W: [1 N* Y" R7 K; i7 \
    5. 生成所有可能的组合(回溯算法)
    + O7 T* f6 M4 F1 R  S( z9 Z
    - W: p9 H. h  d% ^' @0 ]& Z试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    12 小时前
  • 签到天数: 3103 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    % Z, K" A3 X' Q" E5 S0 ?我推理机的核心算法应该是二叉树遍历的变种。
    8 E2 f! W0 m+ _. @2 `/ l- F4 {另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    # _4 v/ n0 j8 yKey Idea of Recursion
    / v" w" P/ L1 J4 Z: d, D* M* D1 @$ U$ |" Z" ^
    A recursive function solves a problem by:. Z9 N0 X6 s- |" @6 u: X5 ]

    4 h0 c; {1 R- E2 a; c- E    Breaking the problem into smaller instances of the same problem.. _9 G+ G, K. T2 H2 g
    1 B9 z% P( H% M1 u; g/ I4 S  k( a
        Solving the smallest instance directly (base case).
    " l0 E# c+ `7 @* K+ O% H9 j' `& v0 y) v# s. [
        Combining the results of smaller instances to solve the larger problem.
    % v7 W! E) f! A5 C  M. @) ^/ P
    * H0 h1 n  A* s/ c. LComponents of a Recursive Function
    7 }% W0 u: ~5 {( V9 ?* G: h  a1 A& w3 ?( ~/ q, Q- t: r: V4 g
        Base Case:0 L8 p: R' _4 ]% Z; {/ M. l* L
    . f0 u6 O1 i/ \& q8 y# b
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.# L+ l/ L2 u3 u, |8 X

    0 K  `6 `# S6 B        It acts as the stopping condition to prevent infinite recursion.
    9 s2 w4 a4 Z5 |1 N! L: J  v8 E% l0 G. H. h  }8 `
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.' p3 P' P. c! s" l

    5 o) {( ]4 F- @# L" E2 y# j    Recursive Case:$ E8 P* `! p6 y  Z9 {

    # O8 M7 A4 n" {0 |/ _; U8 `        This is where the function calls itself with a smaller or simpler version of the problem.5 A# h0 s; r& P& a% G
    ( G7 L' I6 r) F% l! ^2 f
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    . _! Q2 x' ^0 k* S8 }+ Z4 ?, p9 L) c6 L2 q
    Example: Factorial Calculation1 e: V4 h0 [) H  S1 S+ k
    # G4 f# `/ f# {6 o+ w: ~
    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:
    ( l- o( N* }' f! W8 W5 l( O& K* c$ U% j% \
        Base case: 0! = 1
    2 ]/ ?5 ^& ], b
      X4 m" d; X0 X0 J8 s    Recursive case: n! = n * (n-1)!
    ' W) ^. o! x, z) _8 r2 F1 l4 ~+ H. P! D) J, I- C
    Here’s how it looks in code (Python):
    2 x0 x+ m+ |. R: e4 N, Tpython' I( s" J' W, `) D

    7 S. S; ?! L, B/ D- f( ^3 K7 R3 b/ h1 G4 i) u5 H: h
    def factorial(n):- u: H  [$ R9 y; I& c2 s# t
        # Base case
    6 P* L" ]' |: H/ m  `& x. E$ |    if n == 0:( H( r  N: f) N  `4 \! i3 F; l
            return 1
    3 b! m) E' i$ F& K: V1 q    # Recursive case
    7 g2 q% B& y8 R) [9 q( q    else:
    % U! m: g+ I: M2 J+ P# h3 p. J        return n * factorial(n - 1)) q- c: y8 j5 H4 S+ _
    % I$ U% e) m5 H8 |* t
    # Example usage
    * s; j) x% n! B3 \" T9 c2 D. h( Yprint(factorial(5))  # Output: 120
    ! \1 j8 d1 W* a* }' e( R7 G! i( p, o, u8 l! b  R$ {. N; Q
    How Recursion Works
    4 z8 J# W4 s' O, t$ G8 }3 ^7 w' J4 I: V
        The function keeps calling itself with smaller inputs until it reaches the base case." u5 n( q3 s% u* c  J. a3 s

    , }9 D$ O& t6 z' Q    Once the base case is reached, the function starts returning values back up the call stack.2 [" b* h' u, V+ ]
    ( h! F- i5 `8 P8 W( X
        These returned values are combined to produce the final result.
    . ^+ y- g5 {& Z0 F" [0 X  |7 x  K. o' G/ b
    For factorial(5):* _) m7 T! O" o: o8 a" `! L, a
    7 m0 V) g0 b# i3 f# D: S% u% Q/ R8 Q, p
    6 Q/ [3 A4 d8 Y! Q
    factorial(5) = 5 * factorial(4)( ~$ T: V2 u6 a, [& D4 P
    factorial(4) = 4 * factorial(3)
    : l( C( _7 t8 hfactorial(3) = 3 * factorial(2)/ E, B$ g- U) H$ ]/ _! f& L$ X. H
    factorial(2) = 2 * factorial(1)0 }1 z) B1 W, [' P8 L
    factorial(1) = 1 * factorial(0)! ~$ {  p% r% R$ T+ U" h2 ^, I' o
    factorial(0) = 1  # Base case
    6 G7 H" X& D; R7 w3 F0 W+ l. m  J7 B. s$ X3 A$ I" \
    Then, the results are combined:
    ' {1 M" d5 u7 u0 V/ \$ S  ^) [. a, `8 A' F
    $ G) {' ~! W6 T5 s7 k1 z
    factorial(1) = 1 * 1 = 1: o3 C% q( [6 c3 G% a; ?2 u" t
    factorial(2) = 2 * 1 = 2! m  Q1 n  ?5 `. J8 _9 R' N
    factorial(3) = 3 * 2 = 6
    / o; A$ l8 y4 q* O& e( nfactorial(4) = 4 * 6 = 24
    $ o% D* _* n! v6 D! }1 F( tfactorial(5) = 5 * 24 = 120
    2 y1 d9 g# Z# u
    0 H+ G& J  I' A) d9 ?9 X3 t% DAdvantages of Recursion5 x) u8 h+ b$ ~8 I) b1 I( ^
    5 r" j* |$ v3 l
        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).
    3 u8 {% d( G% `0 [, a/ b
    ( |* S2 o) ?5 @: s* X2 Y; i    Readability: Recursive code can be more readable and concise compared to iterative solutions.  D8 D: g4 H( F  ?8 X. Y$ E* s, E

    # l$ d; C3 q6 z0 g) X; I( IDisadvantages of Recursion9 i: l# f* r! ~# E  u2 ]' S$ Q% \
    % v& J5 T  s, Z4 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.
    , `! P1 p/ G1 z- N- l
    ( S( I( R" T8 ]    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    / L/ u, ?4 [5 v4 {) P! K2 Z- p3 e0 T: L; V4 s4 S2 X1 F
    When to Use Recursion
    * B- q+ W6 f7 T7 t( ^+ B. Q8 h/ Q' w, Y5 |' C/ e
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).$ L: a' s9 X' A4 [9 i

      ?0 T# V; ^. y2 E& N* C    Problems with a clear base case and recursive case.3 @1 \0 l7 c* m  D' x$ ?

    ) j" {% @3 r  A# `Example: Fibonacci Sequence0 K; y/ i  x8 A5 @1 D) j% X

    $ O" K6 b" j, n( A# sThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    $ D0 \3 Z/ K; e3 N+ U
    # G9 H$ @0 n+ _" }2 H# k4 v+ E    Base case: fib(0) = 0, fib(1) = 1
    2 W: _) _; a. k5 P3 E% m& N4 |- G! L$ h
        Recursive case: fib(n) = fib(n-1) + fib(n-2)+ L/ e1 O7 t( g3 g3 T: b

    & b$ I/ @- w# f: m- z- S6 hpython
    ; I6 Z, f3 o7 a* U- r5 h; u0 d" V4 E$ a

    0 [# G5 W4 w5 Y/ R6 }4 E) f! Udef fibonacci(n):: c2 Q! ?7 l! V- W; w* P
        # Base cases! s: Z3 n9 Q( l
        if n == 0:
    3 @0 b" G& F4 @" g6 i5 Y* o8 b        return 02 o# t; ?4 a0 p! m- _/ [! H
        elif n == 1:
      m) U- O+ @2 }2 d8 K1 q        return 1
    $ f4 B) Z" s" q! s; s    # Recursive case7 x' ]" J9 _5 u0 t! j
        else:) @8 M: E3 F* n, t0 {
            return fibonacci(n - 1) + fibonacci(n - 2)
    ! A8 v* l# [( f* Q: A
    : P9 |5 Y* ?- e9 k. P& G# Example usage
    ' O) Y0 i8 H% T5 dprint(fibonacci(6))  # Output: 8
    ; M4 U7 _& b/ _9 `1 U" x( C8 Q
    " q; ?9 Y  y1 D  iTail Recursion& z' r1 W3 k' Y( M1 r8 k

    / ^" `! g3 s, Z# P. E1 [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).' |# r6 b4 v( j; @
    % m2 ~6 f, K2 ~
    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-28 20:16 , Processed in 0.030532 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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