设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 & s4 |( J. K6 d3 e, F3 T: N2 A/ _4 d

    2 H4 T8 {0 D# x解释的不错
    . X! \' v: `! w( K. {# y- z3 D
    5 w0 Y) R7 A9 h/ u1 E递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    : T+ N- c/ t8 g: s5 t, V2 S3 Z9 k4 g% l3 ^
    关键要素$ e# C9 ?' y' J1 E' x  G
    1. **基线条件(Base Case)**" ]) G' E1 U- E, W! y
       - 递归终止的条件,防止无限循环  h! l8 t& {) ~& ?' a% |
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 13 x" ]8 N5 C2 o& H
    6 x. v* b' I: _% Z* _1 h
    2. **递归条件(Recursive Case)**
    - J2 ^: r7 h5 V( j, Z$ m0 e) n; y   - 将原问题分解为更小的子问题
    2 [0 @7 \# ]2 s- n0 [4 ]( @   - 例如:n! = n × (n-1)!
    4 U' H% P. ]4 j1 B, ^' M
    ( U7 Z/ S- c8 P  L; W9 O4 \ 经典示例:计算阶乘
    * u$ N; z: n% d7 i' zpython
      w. t, b0 U# |/ P' X3 _def factorial(n):9 r: y) y' U: H& I
        if n == 0:        # 基线条件$ u3 B1 N- r' [1 `
            return 1) C6 p* A4 f9 n& [! p3 t6 P
        else:             # 递归条件
    0 ~0 G' D" P7 t0 b4 h4 @        return n * factorial(n-1)1 j; i  j0 `/ l$ l2 o) C7 e* V
    执行过程(以计算 3! 为例):
    + |/ H& P3 j* L+ z) Q5 h/ tfactorial(3)6 ?3 Z) n# k2 \7 S- {! J3 Z- e2 S
    3 * factorial(2)
    $ h" E- i% E  U/ y0 Z) m3 * (2 * factorial(1))
    : z. y* {+ e5 I* W$ }) v" V3 * (2 * (1 * factorial(0)))' M9 F  l* Z5 \
    3 * (2 * (1 * 1)) = 6
    4 D; O7 \: ^7 R4 T! b1 v, \2 p) P! `7 q3 z# c) k1 D
    递归思维要点
    % }/ D, D7 `% z$ P+ {1. **信任递归**:假设子问题已经解决,专注当前层逻辑3 b! m5 A3 ?5 y& {; s5 S% F6 k
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)* \3 ?. I3 g: H# u* T0 j
    3. **递推过程**:不断向下分解问题(递)
    * y4 M7 P" ]" E; D4 i7 G5 `4. **回溯过程**:组合子问题结果返回(归)
    ) k3 h/ g; G. U, O- i
    5 ~7 y' y1 o3 b 注意事项
    3 @& Y: y; p$ V' p7 }必须要有终止条件
    , F5 ?/ ]; T# u: x4 y递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    6 B5 i$ G1 X2 d! m3 j. Q某些问题用递归更直观(如树遍历),但效率可能不如迭代. S" p% \& q0 o5 m
    尾递归优化可以提升效率(但Python不支持)' e9 k$ n2 @" G2 u, t1 i
    $ `) Z7 a  u( N! z
    递归 vs 迭代' C! w# |. w# m6 t; }2 G( H7 |4 L
    |          | 递归                          | 迭代               |
    # L3 m! {& t) w7 O# T|----------|-----------------------------|------------------|% a* h8 s- m% Z7 r, L0 H% j5 O
    | 实现方式    | 函数自调用                        | 循环结构            |4 x+ a! [' J' v. X% J: \2 Q
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |. X: r  }8 j$ Z% q8 o
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    + ?/ l+ K2 ]6 q3 z- q% w| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ; K5 ?3 o" t; W  |! L; J% s3 s/ l1 d& L* [& v1 N$ N6 c
    经典递归应用场景
    ) y" X3 r9 b% n) V$ s7 n8 D1. 文件系统遍历(目录树结构)
    $ c' n' [! n3 ?( F0 z4 h2. 快速排序/归并排序算法4 n- W+ y- N" Y: j/ P
    3. 汉诺塔问题: m4 v4 U7 B0 \  I1 k0 {1 O$ s
    4. 二叉树遍历(前序/中序/后序)
    0 A8 {, t8 K6 i5. 生成所有可能的组合(回溯算法)1 Q5 j4 L/ k0 d6 Q0 U* J
    & C, a; |0 G% {8 n# A+ ]
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    6 小时前
  • 签到天数: 2965 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    2 d& N6 d( S) K6 y6 Y我推理机的核心算法应该是二叉树遍历的变种。
    ) |) j# Q* A& c3 ?2 \+ G: e另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    # i3 M/ R) v6 g. \! G# b- e' ]% Q# I( j. ?Key Idea of Recursion2 q  O6 O9 z2 Y& n/ N( G( j4 f5 H
      L2 T: i0 A: E8 |
    A recursive function solves a problem by:* B) V& h. }( X  s
    5 O9 _! Q5 w3 O. v- {0 T( G
        Breaking the problem into smaller instances of the same problem.9 z! D3 R9 F4 ]* f, h3 g
    6 E3 h) `0 F' e$ y
        Solving the smallest instance directly (base case).
    $ s' s$ Y1 _1 a; q; Z$ n
    $ \  h1 j) H: W: i' M5 g3 j7 z    Combining the results of smaller instances to solve the larger problem.
    & ?# \4 ^7 g9 u0 R- t
    % Q4 Y' O8 J( L9 zComponents of a Recursive Function
    1 K! C7 T9 x+ O$ c
    6 c2 r7 c, m2 I4 P    Base Case:, V; b1 x9 Q7 u. g( j3 k) z
    % j& R  `' ]( L! \5 M$ l) s! p" h
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.8 d* c5 S  N0 @+ H+ {2 X. H

    % p3 R% f0 V: Q        It acts as the stopping condition to prevent infinite recursion.  |2 o2 {; y( F
    3 T- I+ s# R% w2 X, y" G
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.% c' D, [3 d; h* }- B2 L

    ; ]* ^8 a; b( p    Recursive Case:; R& ~! U) e# n/ @
    ) k6 }, W: b- E  n/ X" u
            This is where the function calls itself with a smaller or simpler version of the problem.
    7 |/ i$ j; K& ]
    " v5 U1 j5 m$ u+ j2 ?2 q        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).( J; @; g1 b6 I) I* j: I
    8 {; d( Y) a: ?2 z: d
    Example: Factorial Calculation3 `5 g" S7 T) h

    : h5 \6 N5 g# iThe 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:5 ?3 M: ]( }' }6 `, x

    , @/ l; ?4 @. k4 v& {    Base case: 0! = 1
    " C) k' ^+ T# w+ j& Y/ Q
    ( M" @! n) `2 G    Recursive case: n! = n * (n-1)!
    - e  D  f: T" V
    - l( s4 k2 A9 |4 s+ @% iHere’s how it looks in code (Python):$ K' P" C: k5 T9 I2 e& q
    python$ g* R0 u- q/ G
    3 l( p& s% b! ]( o+ {* e

    + p; P/ Z, R9 }% q7 ?def factorial(n):% i. U. ?1 z* L& r
        # Base case( y$ u3 m) L: I
        if n == 0:' H% \, e: w7 a% S8 x6 {: V: D
            return 1
    $ C: n7 U% {# T* _7 ?! @, s    # Recursive case# n9 x: H6 {! z" z
        else:
    : F9 B7 U& J2 c/ w; v        return n * factorial(n - 1). _+ K& a8 k3 |

    ) ^$ S1 t0 T$ L! s# Example usage
    & f: H' ]- Y# Y* H& D0 d$ Eprint(factorial(5))  # Output: 120+ R: `/ ]* j6 W
    ' l+ a7 ?: y6 n% @5 J
    How Recursion Works
    1 L8 O) M, h: E# b$ r( W/ p6 k0 `) T" {& ?+ w5 o
        The function keeps calling itself with smaller inputs until it reaches the base case.; S# e: g% V: Z
    ! G3 x* M$ x/ t$ ~# v
        Once the base case is reached, the function starts returning values back up the call stack.
      P- C, u0 A* |9 x+ l
    * b6 G. `* y. [2 B    These returned values are combined to produce the final result., x5 a4 U# q; g3 k, w
    2 |: Q, c; M) v
    For factorial(5):
    & G  {9 h( Q4 C0 h) I) V5 m& g9 g
    / X1 u: P0 K; k) U6 s$ e0 ?+ Z0 |) i2 K/ f! R' @( i5 U$ Y
    factorial(5) = 5 * factorial(4)  O5 ^! W/ b; G! m# p
    factorial(4) = 4 * factorial(3)
    1 f* E9 _  d" n/ j4 dfactorial(3) = 3 * factorial(2)$ V9 {& g, {( G$ V
    factorial(2) = 2 * factorial(1)
    " p  x, U4 i1 \' e. Gfactorial(1) = 1 * factorial(0)/ A" D( K# K& V% F
    factorial(0) = 1  # Base case
      c" T) ?. w4 _9 ~
    0 {$ v( R: f1 q% n4 M( r( gThen, the results are combined:
    ( Y" M1 D# g6 [. D7 A* ~) K9 L$ N" a' e  B4 J
    % I/ x0 x) `, J7 l: T) S& B
    factorial(1) = 1 * 1 = 1& S  U3 E% w8 Z
    factorial(2) = 2 * 1 = 2" g3 }1 f& _5 P6 U
    factorial(3) = 3 * 2 = 6: Y9 y3 s& w/ R6 ^: H7 D
    factorial(4) = 4 * 6 = 24/ n3 o% ]. _% M/ u- `' [, K
    factorial(5) = 5 * 24 = 120
    1 G8 d9 i9 s0 o$ \* I! W" Y0 E
    3 V8 A& n/ P; G. gAdvantages of Recursion
    ; a3 C- u7 B! S" g6 l* b) c
    4 p: w# Q& O% q) F$ F/ I( C    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).* M2 O$ e3 g2 L3 f9 y/ {% d

    0 a% S( E/ _! [( T    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ) ^' O- G3 R  D- J! W& C" e+ B
    4 k8 b( d& ~- G. aDisadvantages of Recursion* K2 _8 g% J" h& B
    $ _7 `  V* M3 L2 t
        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.
    * r# u0 \* c: A( b
    4 H! F% O$ M1 @2 m    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).( @. k5 O1 @# A/ P
    0 D, x. ]3 T2 }3 G, J
    When to Use Recursion9 j) R" S. b: L+ _7 {  v
    $ K+ {" S9 _5 b
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).- N  z4 n/ [1 D
    " {* w+ V; c/ N3 y4 y! f
        Problems with a clear base case and recursive case.0 n9 V' `* T5 s
    . w7 N- v- Z- C3 |& r1 t8 n! m
    Example: Fibonacci Sequence
    " I  ~- `0 ?/ N* g" a4 U4 I3 [% h
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    # a( P6 l; j' z! s3 w( J6 ^3 a8 L6 B3 c# P4 t' F
        Base case: fib(0) = 0, fib(1) = 1
    5 V' Z" ]1 a4 z# ]9 W. W7 g+ {3 J9 W" W5 f2 i
        Recursive case: fib(n) = fib(n-1) + fib(n-2)+ t7 y: i7 m1 C0 o

    7 ]  {. y* }, X) i9 ?; R& h" Bpython$ X7 R% `) a- j4 I/ ^
    2 F1 M+ P5 l2 o8 u4 }

    5 _2 e2 V) J/ T/ J" wdef fibonacci(n):2 M& i) l8 A  h6 ]5 I% Q
        # Base cases* [# }# k8 K; l, ]( W$ Z! q- Y
        if n == 0:
    ) {& D/ S, i  X3 Y, n' j$ L        return 0( Z5 g( @9 Z6 T: j: K
        elif n == 1:* H8 O. t& z) |# A0 `
            return 1
    ( ?5 c  L. m. h" Y3 [0 n    # Recursive case
    ' b) w) }, i  p( M4 |    else:
    4 A# R! }6 V" G. G" m- }8 i        return fibonacci(n - 1) + fibonacci(n - 2)
    / K, S7 m% X8 T0 @. U
    - \/ I6 ]$ o7 A# Example usage
    " q" }5 {5 J$ M% J8 I: z8 v2 @print(fibonacci(6))  # Output: 8
    6 Y5 j( }- y9 i2 I6 o& c) G9 ?3 ^: z& m$ X- r8 G! [& L: U3 F
    Tail Recursion
    ( J- i4 A2 w4 V
    % h' ]& m5 }* m, j! ^' ITail 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).
    ; x9 H+ i' R) n% P# Q
    ; k, e# q! A/ @) _1 lIn 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-6-29 11:56 , Processed in 0.038233 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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