设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    & y1 ^2 {0 J( x& \/ k; d9 C/ y
    解释的不错
    7 X  W7 U$ r# p( o
    $ D. M3 V8 {3 |, p' `5 A: t递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。, h# N0 I; \8 e4 l+ Y0 l/ }

    8 M2 Z0 {" D! [/ i. a1 t. F 关键要素
    * m7 E, @" e" Z4 X1. **基线条件(Base Case)**
    2 m" X( `" D' U) v   - 递归终止的条件,防止无限循环: E; F, e  A8 C2 E9 H7 B8 V# J
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    + m2 T* r( v2 Z) F% t. Y0 `2 _. w0 W3 J6 |% M# K+ u- s* _. @1 d
    2. **递归条件(Recursive Case)**
    3 d7 b" C' T& g* Y# P! F   - 将原问题分解为更小的子问题6 @3 }' s% D; I  i
       - 例如:n! = n × (n-1)!) P) ~$ g! V& K& o; ]3 `
    2 X( Y! c2 H2 d$ I- h+ l3 i
    经典示例:计算阶乘6 c7 k* `- D# P1 m( M
    python+ [; e  W2 U, {% ~4 o
    def factorial(n):
    5 i2 N/ Y- e0 X    if n == 0:        # 基线条件$ m2 q) ~! W3 ^
            return 13 k9 G% |' t( f8 h% K! @' V
        else:             # 递归条件
    $ k* b4 Z9 ?9 B& s        return n * factorial(n-1)) O; c6 h9 ^$ c* Y+ [( t
    执行过程(以计算 3! 为例):$ S0 R' l3 l5 @
    factorial(3)
    ' c7 B! W6 `( I" N& A9 H+ r3 * factorial(2)
    ! W1 K+ c# C" f/ S8 o) y3 * (2 * factorial(1))9 W: w6 ]; ^2 T
    3 * (2 * (1 * factorial(0)))
    4 t8 u" F- d7 n0 X& j4 J$ {3 * (2 * (1 * 1)) = 6
    6 \  T" ]! M  }4 X
    , m6 c# }# t, b 递归思维要点+ k" ~7 \3 v, q  E# k. O
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    + Q# j4 m! `* J/ G* f2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ; q, h3 e& E) E9 i3. **递推过程**:不断向下分解问题(递)
    : A$ I7 R. [) }" c4 S- U4. **回溯过程**:组合子问题结果返回(归); j; L3 p, J7 O0 s( B" Z; v
      t% f7 U8 h; [; b$ t2 Y3 R/ Y5 N
    注意事项; V+ A$ X8 k- @9 V9 {7 |
    必须要有终止条件+ W. S4 m4 ^: B# [2 [" y
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)5 f5 h: I3 v8 i3 w" M$ w: d& W
    某些问题用递归更直观(如树遍历),但效率可能不如迭代4 U' X4 S$ k, B# V% x
    尾递归优化可以提升效率(但Python不支持)# r7 s+ z' E# r3 p+ Z5 b5 c
    ; w% t" V& A8 l! ^' ?9 E# X; z
    递归 vs 迭代
    % Q3 ^+ }' f+ C: V$ P2 ~5 M, J# j3 C|          | 递归                          | 迭代               |# L; a# ^( N, B6 f6 N( d; H
    |----------|-----------------------------|------------------|
    1 ]9 K4 b! h4 m- T| 实现方式    | 函数自调用                        | 循环结构            |2 [7 m2 F6 H* x' u+ W. t
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
      K) b3 g1 C# ]4 R% T4 i| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    1 D  H5 K+ [: N/ `, n| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |. t& L- G1 `) |( d) f

    ( R1 {5 p5 U4 I+ `% i% s 经典递归应用场景
    / g5 I$ m1 A, F- g# r1. 文件系统遍历(目录树结构)
    ) Z1 j+ z; p, C) x" d% m2. 快速排序/归并排序算法( `+ ~3 J/ R* e( w  b
    3. 汉诺塔问题  t$ Y0 `8 x8 e6 a) h9 p
    4. 二叉树遍历(前序/中序/后序)
    8 Z/ Q1 T2 D- B" g) c4 M3 \8 y) |6 f5. 生成所有可能的组合(回溯算法)
    ( C: j* ^& F# C
      I& |$ w1 t/ y& i7 I7 p4 n试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    12 小时前
  • 签到天数: 3094 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,9 {* D! E0 R, P
    我推理机的核心算法应该是二叉树遍历的变种。: B1 h1 c1 C8 [
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:: x1 k+ S2 M) L3 E6 |( W
    Key Idea of Recursion) f5 a9 g' r* T0 G
    5 K. V+ ~, f' G! Q
    A recursive function solves a problem by:; W% @4 |8 ~4 v: q- ~+ V

    6 j7 I( R& R+ Y1 O    Breaking the problem into smaller instances of the same problem.
    & o5 ~7 S- l$ [& Q8 o! E$ n( X' W# N7 L& }$ |
        Solving the smallest instance directly (base case).- z, `( _3 {$ l" a3 U# Q
    . b# D, m! h& [3 t' f
        Combining the results of smaller instances to solve the larger problem.2 V+ r! q& w% T- Y4 D7 L, [7 g
    , D  U3 G  M) z" s* J) @+ ?
    Components of a Recursive Function
    & C/ Y: z6 t4 n+ f9 P# \% u$ {6 F5 u% E; Y/ c
        Base Case:
    + k& s6 {& a! T& a' l! o, B& ~+ y. Y* g, u% q
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ; `+ Y6 x# `7 g" O; T( f# n  [
    % R! K  l7 z! C3 K1 d  l        It acts as the stopping condition to prevent infinite recursion.
    - D, ~1 t* ]0 V9 B& z
    . y( {- W/ e' h5 W        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    * B' }. J* |9 K. x5 d" I# i) S) F. Y5 h# ~7 {
        Recursive Case:( ~) s0 N& e8 G$ S* v8 G
    " X/ p: t& k% B, M1 q
            This is where the function calls itself with a smaller or simpler version of the problem.
    9 u& y' |5 l, h) z4 L& i( |( D. n. M1 n- h; \
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ; `( {! f* h' ?5 h" C; r4 `9 u/ ^5 N- I* q
    Example: Factorial Calculation- L2 }3 n9 ?/ g1 S0 v
    - H" S2 l- l8 a" f- a; V% _
    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:% n% T; @- y' b5 z
    - j) }% m- n3 ]# w/ f
        Base case: 0! = 1
    $ d  I2 A% a! y  x% C0 U4 B) U; t3 J( j! L( W
        Recursive case: n! = n * (n-1)!" C( j. r1 Q* }4 z
    ( r1 i' m/ R% T) @' s4 w- }( M
    Here’s how it looks in code (Python):
    ) X) z/ G# P; Rpython7 f9 r" `8 J$ C& E: |& ]

    % f1 I& X$ q9 a% s) M7 _7 P. L) E5 D5 c5 j+ a" a
    def factorial(n):' {# y9 M; S' G8 B; I8 d) d8 O
        # Base case
    6 s; N  J+ I& K( N. e3 W/ I5 |    if n == 0:1 R6 J4 l/ r. `) N* ]- e
            return 1
    # |3 G: H2 d3 v+ [    # Recursive case
    ) g, k& X- ]1 A0 c  P3 I0 ?. ^0 f    else:
    * @, ]2 t' c/ Q8 S# {- g/ Y9 ~+ y- B# |        return n * factorial(n - 1)
    - J, J: N/ _& s9 Z. |) G/ e6 g) {" u8 |4 z2 A! R/ C6 a
    # Example usage3 d& Q4 V! O$ v! i: X) e& E6 p4 p
    print(factorial(5))  # Output: 120  y* C8 N1 g; f

    0 o  n8 {! i; P* m+ d/ r0 v" K- P0 WHow Recursion Works
    * z0 P, k/ L7 X" A! |7 [$ f, k6 t6 {& O6 s) B
        The function keeps calling itself with smaller inputs until it reaches the base case.5 S; D8 q6 [+ V1 m' Z& h& Y3 [2 c& v) z

    . _" L. V$ W. m( j  b9 _1 K    Once the base case is reached, the function starts returning values back up the call stack.5 s" s% D1 w( I6 Q
    ( M2 p: Z. }, [) I4 F, S; x6 ]! @
        These returned values are combined to produce the final result.
    $ K: M' r4 J# z0 L" J$ P- u" t' D2 \; T4 Q
    For factorial(5):
    9 y& {3 ^' D0 [2 Y. L6 A5 ]. @" u5 g% u, T( e, M- g

    9 B. ~- B$ M. @$ b, o& B) ~factorial(5) = 5 * factorial(4)
    ; D0 v9 }% U& N0 w+ Q% B; f7 m- W& m/ xfactorial(4) = 4 * factorial(3)
    , j. Z) X7 `8 ~9 _factorial(3) = 3 * factorial(2)
    8 C& L4 G/ z* O- ifactorial(2) = 2 * factorial(1); A3 d. h6 W' H2 R& t* E6 w
    factorial(1) = 1 * factorial(0)
      x7 l( ?& n  F: Lfactorial(0) = 1  # Base case
    ! \4 r" B  D$ @$ q7 M' }5 W+ r8 w: K$ P9 q0 u
    Then, the results are combined:
    3 r$ T: H# V% x, O
    9 s4 {7 @. F" l0 u7 E6 v' U2 i0 }" n. \4 F3 d1 f+ s! R6 _1 S
    factorial(1) = 1 * 1 = 14 i) ]4 B7 [- m+ ?9 a+ O, [, G
    factorial(2) = 2 * 1 = 2
    6 L/ b5 S5 _7 @/ `5 o' Z" a7 Lfactorial(3) = 3 * 2 = 6& g; [3 |5 Q& t
    factorial(4) = 4 * 6 = 24
    5 n+ U5 I/ a- _+ K( q2 Rfactorial(5) = 5 * 24 = 120
    6 P* n1 k9 b: D
    2 @* i- U- z0 w& hAdvantages of Recursion
    0 y" ^( a" @: ^' c
    8 J+ H, x' `, }, F) {, E    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).
    2 s8 m2 F& u7 M- h% s
    ) l0 O& N& e5 }    Readability: Recursive code can be more readable and concise compared to iterative solutions./ g) }* g$ n. p  D" q

    7 y0 a5 `5 |, W8 b( k3 JDisadvantages of Recursion
    : H+ z( Z  u7 H) G- d! l/ e7 j1 ]4 [# r# n' c# S3 z+ y2 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.
    ) O( u: m7 Q  e6 L" R& p& E1 k& L8 z+ E7 @
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    9 k$ M6 {* [- |) m" A0 M  U
    & ]  ?4 [) Q9 o7 IWhen to Use Recursion
    , i1 U4 w) f* ~  U0 V- S- ^2 S# {* C
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).# G  g' m% P& w9 n5 F7 ~
    ( v7 j+ q: u% T1 {
        Problems with a clear base case and recursive case.! J9 Z% T1 V6 W1 y; @

    5 u; s! X( |& @/ T0 {# ]( G  \Example: Fibonacci Sequence
    2 e; z! y5 G4 K" j9 V8 c5 }
    * O! e2 v( N; B" J& G4 U/ jThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:5 i# c* n% S4 H/ \
      @; ^$ p, ?' E3 h
        Base case: fib(0) = 0, fib(1) = 1
    : N( V0 ~4 |) f! e
    8 R& |0 i  j; ?  U# ?1 Z    Recursive case: fib(n) = fib(n-1) + fib(n-2)
      F' f0 M  L. d9 E! W5 y0 r, m' `. O" J+ r* U3 ^! E: W* a
    python- c8 K6 w: ~6 Y/ u  _

    $ @1 A! V  m" J! F
    " V. {4 `# N& Z% L( F& J+ sdef fibonacci(n):3 u- J& P: i+ L2 `
        # Base cases# A, u/ V) U5 D( F
        if n == 0:+ s7 D  x, Z+ p2 s8 L2 d4 x
            return 09 ^, l' [9 u7 }' ]: i# {
        elif n == 1:1 Y6 X) l+ U) i/ P+ l( K) t/ f
            return 1
    ; V' O+ m6 X! a: i5 a2 W    # Recursive case
      x& d4 p  Q% Z! ?! f    else:
    ( J! V' _- [& |6 h* t        return fibonacci(n - 1) + fibonacci(n - 2)
    " h% _. H) l7 N8 Z0 U: B9 _: n0 ^9 M
    # Example usage
    8 ^, }% \4 B5 V' q/ x! yprint(fibonacci(6))  # Output: 8
    . B3 g, {/ z3 S0 y9 H  g* c  `2 n2 B+ g
    Tail Recursion
    " \- m4 b; H2 I5 c: B, }% ?4 O& M
    5 o  Q! o7 F7 U" u9 b3 MTail 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).
    5 P# }- r% S. v& O  q2 u  E. i
    0 B- l/ Z" J, g/ d4 ]& [! YIn 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-16 19:39 , Processed in 0.032181 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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