设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 . A+ {- _9 Z3 b% F, g

    5 X4 w5 N+ A; p( X解释的不错
    1 `/ W% u% n  p. ]; t
    % _* {: n8 _1 P. P$ a* B递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    * P  |0 T% g4 }6 j& v2 k8 k0 t& h& c1 G* y; w2 t
    关键要素
    1 j" p6 E. g  g  |) \, T: d( H1. **基线条件(Base Case)**
    ) C( r& G/ e  Q& `' ~2 e   - 递归终止的条件,防止无限循环
    . f  ]4 l( _. x; B) }   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1' p" N* j# k! T- E" X( B

    % }" u7 x$ {, V: o' R2. **递归条件(Recursive Case)**
    - M  I5 [' d7 \6 E* u, O   - 将原问题分解为更小的子问题
    8 o  ^, E: u8 t& F8 h6 Y) O   - 例如:n! = n × (n-1)!
    ; F$ ]- q/ _5 \- Q/ D: _8 F$ K
    6 |7 w9 e0 l. F5 w 经典示例:计算阶乘
      f  k3 E6 }/ I/ Z/ Tpython
    . m! t! g, ?5 jdef factorial(n):
    - e. T! }, d- x" n- B: n    if n == 0:        # 基线条件/ p2 @3 I- m& y6 ?
            return 18 G& V% G5 G+ x* X0 m0 U+ Q# [/ g
        else:             # 递归条件( I4 M4 C% A/ D: j# [$ Z
            return n * factorial(n-1)
    , l1 k; H, s" F8 E5 [+ {) E6 g执行过程(以计算 3! 为例):
    ) G- {) s$ ~- C/ M. x/ qfactorial(3)
    : V6 ]6 O. E0 g/ M0 A4 S3 * factorial(2), _) W) m0 ~- p
    3 * (2 * factorial(1)); D8 d0 d; z6 x
    3 * (2 * (1 * factorial(0)))
    * }, a/ J: A) ^" ~1 ^8 N3 * (2 * (1 * 1)) = 6
    . k- O" P. x% ?7 i' S
    - Y' m1 D/ ?9 q0 i* P5 s 递归思维要点
    & }& _/ y7 n. m; {6 w9 i1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    & Q3 c3 B, y/ S2 l1 F2. **栈结构**:每次调用都会创建新的栈帧(内存空间)& D# F- b2 I; r: [
    3. **递推过程**:不断向下分解问题(递)
    2 K( j2 u( {1 x8 V4. **回溯过程**:组合子问题结果返回(归); l+ A( [; Q; [) n" [

    . j7 z) {, H2 m6 g- \4 r6 c$ I 注意事项
    3 ~' A7 o2 b- L必须要有终止条件
    & E0 I4 s# D1 P% j( z递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
      H! c8 v* i* O  f5 W" a某些问题用递归更直观(如树遍历),但效率可能不如迭代4 n" a9 {# f! k3 i. I4 Q; b
    尾递归优化可以提升效率(但Python不支持)
    9 X, {+ n3 J) ?* ~
    ) m9 \0 \3 a0 ?% h 递归 vs 迭代
    8 P5 g+ {# ]9 M; j8 s' ?  B|          | 递归                          | 迭代               |- x3 }; @8 X* K$ m  S' j1 H
    |----------|-----------------------------|------------------|# l0 F+ R# D2 y! z2 g+ {( i' G
    | 实现方式    | 函数自调用                        | 循环结构            |- }9 _) r# s0 |
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |. T8 \: V5 b" `
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |3 w/ W& `, b9 i) X3 z
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    . I2 ~' F' ?- [* o+ D: X  ?- i0 G0 q, Q' R& R
    经典递归应用场景
    : N* Q5 r' W) V$ T: M: e* k1. 文件系统遍历(目录树结构)
    3 C; `! N$ {" W$ j$ a  i2. 快速排序/归并排序算法9 q/ ^3 Y4 i: [& N
    3. 汉诺塔问题
    ' Q5 T9 O7 ]- j8 F' n4. 二叉树遍历(前序/中序/后序)
    ; l  }- u5 `. j5 M( A8 v5. 生成所有可能的组合(回溯算法); r+ S  |! B+ Y" T. I3 h

    ) Q3 D) h' g5 J试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    8 小时前
  • 签到天数: 3106 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,% P; {, z2 R+ i
    我推理机的核心算法应该是二叉树遍历的变种。# |. Y! ~7 ^. L  T- L" q: ^8 U( 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:
    ; V8 |7 t- S0 ~; \* K9 b6 L% F$ S" aKey Idea of Recursion" y+ e! p+ ?2 y: ?2 `+ ~0 a

    , o* [- C6 Z: S( U: g5 R/ @- h) S7 HA recursive function solves a problem by:
    : E' H  N& p" a
    8 d" a- ]" C7 B5 X2 n    Breaking the problem into smaller instances of the same problem.6 B" b" J& i0 H. [* W3 L, K1 y

    5 r+ ^8 T4 b7 `% m5 z    Solving the smallest instance directly (base case).+ \/ f: t! \. S; R3 z

    4 J0 h: ~/ S2 S# V* S) E    Combining the results of smaller instances to solve the larger problem.. L9 b9 H; X# p6 d
    ' t0 S! e$ f# R! O( z' m
    Components of a Recursive Function* G7 B( l( Q. M

    0 ^2 u1 R; c5 B" r    Base Case:
    4 C3 E( u9 v" S
    9 B. a$ A( D( C        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.( c* `! k8 Y! K+ ]

    . h9 e* ~' h5 o- N        It acts as the stopping condition to prevent infinite recursion.. T; T2 c4 k" G; v" ]
    . J6 G& h' Z) V. t+ p
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.1 R5 T9 V( l( z% G' Q

    1 g! E& d6 b% f+ ?$ s; \2 [    Recursive Case:
    + K5 K1 d% S; f( r! O; O* \" ^' G* f' E: V* e2 M! W
            This is where the function calls itself with a smaller or simpler version of the problem.
    ; t' }# w& h1 W, r* ]  Q/ G: H3 g/ M* H- C
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    2 z  O8 {: S& x) @% B
    - Q2 S+ Q+ ^2 v% vExample: Factorial Calculation' e; a. W& E" K+ t2 F
    9 H3 |! G) `! A; Q
    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:3 x* ?6 f' m# z6 }) c  [
    4 B8 T/ g) r! \* b* x
        Base case: 0! = 1
    : Z; B2 T# o! j7 i5 }# p3 m+ T4 {
    2 b3 x3 A( y4 ~5 G: r0 k    Recursive case: n! = n * (n-1)!
    ; y2 B2 g7 |5 ]% m7 \6 e( f0 q/ S! N* G' w" i
    Here’s how it looks in code (Python):$ T. h  @' @/ }( c! Z* u& ^
    python! ~/ }9 h- [) S' E0 y1 i

      Z& s9 g3 e2 K0 X7 e" P( L9 q
    % R1 E0 w* W0 xdef factorial(n):4 I5 E1 K, D) d
        # Base case
    ' C7 p+ ^! O/ W8 x: j    if n == 0:
    , `- }7 i: p6 ~: D8 z        return 1/ h3 r$ N5 |# U0 S8 U+ z1 {
        # Recursive case
    . q1 f9 B0 J" P; ?# W/ E    else:6 H2 }* n4 r' i' k/ E. q" f5 A$ h
            return n * factorial(n - 1): l8 o/ R$ C/ ~$ c
    6 v  B* T( H" |! w0 w, y
    # Example usage
    ; E7 b: D( N- I, {; |; uprint(factorial(5))  # Output: 120) D1 d2 \7 ]3 I6 P( u/ d. m
    % z& ]3 N) Z) W1 z" _0 b3 _7 L# Z- @2 i
    How Recursion Works
    : |9 D% u1 A6 [1 p5 ^+ b- |) c) F# W# M8 ^
        The function keeps calling itself with smaller inputs until it reaches the base case.
    6 s9 Q  ]% L( R$ V& G. |9 f' N+ R1 n# N7 W  L5 m
        Once the base case is reached, the function starts returning values back up the call stack.# ~, Z3 a! M+ R

    , t  V5 L& L0 L, `$ x4 D+ t    These returned values are combined to produce the final result.
    7 v+ D2 j+ b/ s# x" S5 [0 R% R+ l' I+ g- |! U
    For factorial(5):5 g" c  {$ A% v( t3 ]8 r

    ( r  z1 L0 [1 G7 F
    . Z* s/ B. S/ ~# A0 W/ v: Ufactorial(5) = 5 * factorial(4)
    1 ~: S1 Z4 J4 w3 F( Qfactorial(4) = 4 * factorial(3)" Z: s: z2 G. M$ W0 x
    factorial(3) = 3 * factorial(2)* ~# T8 h4 F! L0 T6 k
    factorial(2) = 2 * factorial(1); J! Q( K- C. p" X
    factorial(1) = 1 * factorial(0), C1 T. _: P; `6 K) y
    factorial(0) = 1  # Base case7 T8 h" }' C5 P+ E9 W

      ?3 ^6 x9 H9 r" n" Q; L. J0 LThen, the results are combined:
    ( Y* y. g4 A# E  W, Y5 i1 C5 G! u( N0 H
      [3 ?" G7 C: e) g! E
    factorial(1) = 1 * 1 = 1
    / r" z% Y, k+ h$ u0 c8 dfactorial(2) = 2 * 1 = 2
    6 d4 t7 Z( ]; P$ L0 f( Z/ Kfactorial(3) = 3 * 2 = 6/ `5 I& I5 b5 i: e" S
    factorial(4) = 4 * 6 = 249 m/ X6 u1 c8 ]
    factorial(5) = 5 * 24 = 120
    + X2 v2 x8 m( N9 F7 g: s6 m6 t* l- i/ Q* F( k, X2 o, L
    Advantages of Recursion7 _/ D6 c5 T# w- D1 ~2 b3 _9 Q3 q

    9 ?* }- P" o7 ~  N3 u6 r    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).
    - e  O, v* o* _$ _2 F8 f- X$ m( y7 F' E* }! m: N9 r, m4 |7 {
        Readability: Recursive code can be more readable and concise compared to iterative solutions.# I6 R# l/ u3 @- h+ a
    ; u5 A5 x7 `5 W) o+ }4 S" L
    Disadvantages of Recursion
    3 e6 P  Z! v. a$ }1 h& v* D. p$ A- P: P. V7 [! I0 p1 D4 L
        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.# E! E6 p6 A, Z) m8 m+ d8 d# c$ d
    & x: T) a3 z5 i& d( X0 q
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).8 D+ A$ l! I4 U2 Z) I# m
    , E+ Y3 p9 o3 w& u+ O1 Z) I
    When to Use Recursion
    % p$ k: ^* R/ \+ C7 J9 ~! Q( q
    & R2 G/ [) g1 t7 y4 C( k/ K+ R    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    2 D& ~+ A9 V* M0 V& A! k) ~
    & d2 q' W+ [3 f  K    Problems with a clear base case and recursive case.4 F5 g/ f# [, n4 M  \
    & H: M. {2 D8 E8 h9 @
    Example: Fibonacci Sequence
    1 y4 o& O% |, e0 c% d0 q
    9 U8 G: M& X$ HThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    * ]. c1 J3 e: x( P! _; f- u' B, ?3 o. G" f& g  n% j
        Base case: fib(0) = 0, fib(1) = 1- P2 G% K( B( F

    3 }. U% w! a, K9 ?+ G    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    + Z9 S4 O; Y& N
    , ~! `+ w# T- O7 ]+ upython, T4 o) T& n) |4 ^
    . D3 g5 G! L  _( O4 a
    4 s# E1 S5 I& U" H% q$ A
    def fibonacci(n):
    # f3 b# s$ _6 G( X, d, C+ n7 s( ^    # Base cases
    ( c/ u* [* J: m1 P  x    if n == 0:2 `, T( L6 w# y& P) Q
            return 0
    4 Q# Y7 R: G' n- k# I    elif n == 1:; W$ F* {1 i9 y0 ~& c! @$ [
            return 1/ O6 A0 |/ _$ J# x% p
        # Recursive case7 Q6 b7 A( }9 j* ^5 @$ b7 f
        else:+ s6 `" [. _& e, Y$ f/ ?1 i
            return fibonacci(n - 1) + fibonacci(n - 2)
    2 P' w3 }) p+ D' N7 k* q( w
    4 J; O4 t6 Z9 R/ i' f/ i5 ^# Example usage/ }. J: N4 {, `& g7 p& c  g0 J" [
    print(fibonacci(6))  # Output: 86 |# |* l! I- |  h) \

    5 ^4 X0 x" g; d1 PTail Recursion
    8 z# ?5 W9 j2 a) j8 [5 k- C, ~) U$ B
    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).% |- J- [7 Y: T5 n3 l8 s

    & P8 l8 z  x* W8 r) F. CIn 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-12-2 16:20 , Processed in 0.030958 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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