设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ! ^& y) D. n: U
    - ~: k3 z# U6 K解释的不错
    ; S( x8 m2 a  O2 t% }( @8 k# H! D, y% O  d  ~" M" x- M1 `2 {( f
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。# t. B/ C& Z' K& q* l

    ( \3 p% ]5 F3 y% V- L7 X1 ] 关键要素
    3 o9 K& R& g0 ?: D5 n1. **基线条件(Base Case)**5 `8 C* W4 A  R3 H, D
       - 递归终止的条件,防止无限循环- V2 }5 u. a; ]
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    # s1 J% h* w, C* U; x5 a. ]! b# v3 n3 V; v4 P0 u$ I
    2. **递归条件(Recursive Case)**
    $ W9 e) H4 @2 R# ]1 v( Q( ?   - 将原问题分解为更小的子问题$ m3 _, P% x2 g$ i
       - 例如:n! = n × (n-1)!
    " t8 y0 m* N- m) m/ M
    6 o0 V- R1 v: K$ r 经典示例:计算阶乘' W2 S. w8 T1 d2 G3 p9 u$ M3 ]1 u
    python
    * |# m* {: ^+ S) _3 e. Sdef factorial(n):
    " W, G8 b" E) o2 t    if n == 0:        # 基线条件+ _! l# M' L8 g0 h3 X* W0 ]% g
            return 1+ }8 P, |( q$ e0 `
        else:             # 递归条件
    ; A2 @& B8 ~0 ^* v2 L) x0 r) m        return n * factorial(n-1)
    ; W+ O$ y3 t4 ?3 ?+ s! e$ y/ X执行过程(以计算 3! 为例):. T/ B* |) Q) B+ A) W$ _
    factorial(3)4 j3 S8 @5 v3 e% l" X
    3 * factorial(2)
      O+ x$ p$ O9 H  y3 * (2 * factorial(1))
    % K6 \. {2 C1 i9 S# J1 ^3 * (2 * (1 * factorial(0))). M# x% Z. l6 y; H* D3 ]; S
    3 * (2 * (1 * 1)) = 6
    4 X* W$ B, f  W4 {# ^2 q
    0 I7 l- r/ E# o 递归思维要点* m4 e3 H7 }! c. M' a9 t* L
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑. Z0 x8 R7 c+ y9 L
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    9 }- M; ]6 O! Z1 D* e- F+ D- |! ~6 l- v3. **递推过程**:不断向下分解问题(递)
    ; E, [4 u1 s$ H$ |# x( n4. **回溯过程**:组合子问题结果返回(归)
    , d! q0 x( P. O+ |/ v. E# @0 \; r: P
    注意事项% G0 Y5 O* G6 o- Y/ O( @
    必须要有终止条件% i; Z% ~! D+ D+ |( n
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    $ w9 Q$ r, u/ t, j% r某些问题用递归更直观(如树遍历),但效率可能不如迭代! I+ d& b: G! R6 F8 y. `
    尾递归优化可以提升效率(但Python不支持)
    , w, t+ \. t* J' I0 |2 m1 o. l6 {# h
    / K9 ?) B8 `4 n0 [  W 递归 vs 迭代
    7 K. z, b! O9 T: G4 U8 r|          | 递归                          | 迭代               |
    % ?4 B: O4 O/ Y0 _$ c|----------|-----------------------------|------------------|
    ( W( d0 L9 a4 B( p. N' m| 实现方式    | 函数自调用                        | 循环结构            |1 k( ]/ K/ D3 b) K; D0 g
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |) y3 M7 x$ d. m6 |, x) ~
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |$ M4 U! S, P# p. y2 N2 x  {
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |& [: o. S' D' d* L2 Z/ i& D

    ' y- `- E4 m" k6 V 经典递归应用场景$ E& p/ S: X9 @+ y4 C
    1. 文件系统遍历(目录树结构)3 c; b6 ]1 ~+ c! E2 M
    2. 快速排序/归并排序算法
    : j1 m$ P3 P8 I1 X3. 汉诺塔问题- C0 J5 T7 v7 v/ o3 `
    4. 二叉树遍历(前序/中序/后序), ]8 o+ V( C4 M2 W0 M
    5. 生成所有可能的组合(回溯算法)
    9 C5 _; \$ }! t+ O
    . k! r6 J( G; _8 Q9 T7 t' r试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,6 Z: M* y+ Q4 t' s! t9 ?
    我推理机的核心算法应该是二叉树遍历的变种。: r  y: N( R  q
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    ) Z+ t) z9 a- p0 k" GKey Idea of Recursion$ Q1 }' {, G" ^( E# f" z; S/ S
    ; {0 k2 U+ B* k- K  _) s6 I
    A recursive function solves a problem by:
    2 X: [( t3 b. f& h" D9 I: D, P9 ~5 F2 x7 M8 x
        Breaking the problem into smaller instances of the same problem.
    6 s2 K4 o3 S' h+ M( r$ r
    4 h0 G2 \8 v0 x: K; y    Solving the smallest instance directly (base case).; [5 b& x# Z8 O& _- x& f3 d* }

    + W* w) X' M; B    Combining the results of smaller instances to solve the larger problem.$ g) m: [/ t. F8 o- a* C. H0 t, T
    , _+ a  U' d' }- ?: f, G* c. g
    Components of a Recursive Function
    7 P! Z% \/ |: f7 e6 Z1 M2 V2 h) e& [% \( E. l
        Base Case:
    - [2 F' T( k  \) W) w# L% m
    , U  Z$ |0 a; ?3 `. x8 j        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.2 l& J! g. G! _! M6 L$ \7 p5 p) ^
    3 ?* K! r/ s" C! U
            It acts as the stopping condition to prevent infinite recursion.
    5 P! A$ V" Y* T3 j8 o% ~9 l4 q: k, {
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    - J" E- I0 j) h, a) o0 w* f) @% M; {: w) X
        Recursive Case:
    : F! G# S* T' w; \  E0 _) B) Y+ C2 X
            This is where the function calls itself with a smaller or simpler version of the problem.  R# T4 m. P3 M$ G

    9 S/ _0 \7 v. y: u: n        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    7 ]1 V$ U0 o3 r2 ?( N& g
    & d5 v6 T! Y, Y- K: M2 s& BExample: Factorial Calculation
    ; ?& N8 y0 |) J0 E* s
    4 V' P9 f* Y" Z/ W, K5 \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:* I0 ~* X" y3 _- B
    1 p" q6 u2 Y9 m
        Base case: 0! = 1& C2 E3 _5 ]0 B) `5 T! P  O2 i
    - J  A3 Q2 U7 o! z
        Recursive case: n! = n * (n-1)!- r2 x5 |' o; c$ S
    * a- Q: w2 z9 s3 g" b' Y6 u. x& e; l
    Here’s how it looks in code (Python):
    $ j8 `$ C  F( @& Ipython
    $ \2 v" ?8 C3 k. K$ a
    4 ^0 B, c7 g6 G$ ~  \
    9 L: T0 m  Q0 }2 mdef factorial(n):  I8 }, a8 F9 M/ W8 Y! ]7 |9 g
        # Base case
    $ ~' r2 z& s0 C5 S! L. ]( z' _    if n == 0:0 v1 _2 a. K$ ]7 v; E5 P+ f) [
            return 1# ?2 P% Y7 y& v8 {) @
        # Recursive case
    3 @" S4 T: _$ Q4 S    else:
    + [% s- i4 ~4 e2 A        return n * factorial(n - 1)2 D8 A+ S5 @/ f: _4 A
    - {9 Q! F" o9 E; b
    # Example usage1 K7 p6 j6 V% v3 g/ a
    print(factorial(5))  # Output: 120, b( U  n) |) f* Y3 a/ B
    $ U, o' u& s$ A( Z! r
    How Recursion Works$ c* s- u6 |0 g, y6 o9 K
    & c# n( u  `& h3 X
        The function keeps calling itself with smaller inputs until it reaches the base case.
    9 V) r$ E- x# S9 o, U6 I2 l. h7 t! t- L0 O
        Once the base case is reached, the function starts returning values back up the call stack.
    ( ?9 ?& D' W2 F7 e1 R" |, G% h+ B4 ]9 \2 m
        These returned values are combined to produce the final result.* f' y; b) A. n6 \, ?( J$ A5 q( X
    ) R+ A* N" _, e0 o3 f* O
    For factorial(5):8 e4 X; t8 F0 V! E( q7 m! ?% E2 [7 \

    2 N! [/ F0 {  J8 e7 ?4 |8 l0 r. Y4 X# v' o* U; o
    factorial(5) = 5 * factorial(4)( t* g& m2 ?7 p4 U
    factorial(4) = 4 * factorial(3)( V* d3 V% u+ @, T2 Q0 V. p
    factorial(3) = 3 * factorial(2)! f0 P; R% }$ K
    factorial(2) = 2 * factorial(1)9 {6 i7 R! Y% A9 w% A% m# a2 J
    factorial(1) = 1 * factorial(0)# u6 h! U% w- P9 _, \0 l, t3 ]
    factorial(0) = 1  # Base case6 ]+ u: ?3 i" X; s2 m9 n
    , d; ]. j% p3 c, W4 Y, C# O+ z3 B- X
    Then, the results are combined:
    ; n8 e1 J; }7 {$ X4 I
    ( {' n; U, K/ F4 h8 {0 q0 k$ y4 B7 M3 l+ g$ c, V+ w* L7 p& Y
    factorial(1) = 1 * 1 = 1  n% n  e, Z! P! X5 d! I
    factorial(2) = 2 * 1 = 2
    $ g) ]  [6 j  @3 W2 }5 O8 Jfactorial(3) = 3 * 2 = 6
    0 i& e/ G; j3 B8 afactorial(4) = 4 * 6 = 246 O& }* K$ ^: y% E' N
    factorial(5) = 5 * 24 = 120
    3 [+ R$ z) V2 ]( A, x2 A
    % z# `# |/ \2 P# @: E( Y  oAdvantages of Recursion
    $ A+ p+ s; ~! t0 L$ c. V- f% ^% @3 b* S* c; m5 N3 i! u, j
        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).
    9 |5 {( b% {5 |+ j1 v; O0 [
    6 {) P: ?" ~$ g+ y7 [    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    * h. p" s* h% o  G9 r6 x* M
    5 h: ^! ?0 M. J1 n! ODisadvantages of Recursion
    # m! a/ x+ c6 q+ S% L& j4 Y' B% c, G. e# f6 h+ i; y
        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.( Q* U/ w3 q$ r% M
    0 {0 H- R1 \& w0 q: f3 r! j  k
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    : k4 _9 L2 `5 b; p4 k1 X4 f% b4 ^" V
    When to Use Recursion
    - {5 i5 K! h0 D) A; L& f" m9 c/ f; [5 Q: b! h+ g' @, }' V
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).' L6 i% N% C. g+ B2 L1 R* d4 x
    9 b% n4 D; X& h9 K' S
        Problems with a clear base case and recursive case.1 |( `. ~3 U; q% _2 K: s7 H1 S

    " S6 _6 w* B3 {. S& g# r! L1 H& RExample: Fibonacci Sequence; y% X6 j+ T! F" F/ ?9 r* S$ {5 U1 ]) O9 r# u
    . Q$ Q& z. D5 u; G( z! A
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:& }+ s$ z! d  Y& r" E( Z6 k/ N3 r
    ( |" i$ F- p7 _4 |( j
        Base case: fib(0) = 0, fib(1) = 1" ~% M; z6 w1 o
    ) z) @7 b3 K- R9 u/ z4 e6 \
        Recursive case: fib(n) = fib(n-1) + fib(n-2)3 F" B- g6 K( b

    3 l+ f: _" b7 {& V2 F0 x, l. U" ?! ypython' ^& G7 X8 n& O& ^
    ' R# O. S" G4 t3 @

    $ c( ?' a4 f' N7 Kdef fibonacci(n):
    ( Z: R6 \8 E6 ~) |+ Y4 {! A    # Base cases8 e0 ]& E1 r# o, J9 \
        if n == 0:
    " W4 `3 R9 y% W; D$ ?9 t        return 0" e8 Z: L% _3 i
        elif n == 1:6 w3 {* P8 K% f. \% X) v' U
            return 1
    7 h2 o/ C6 f9 ]    # Recursive case+ b: Y% k3 E; e0 C5 h  l
        else:
    . k0 D) V# |, W5 v* p$ p3 B8 }        return fibonacci(n - 1) + fibonacci(n - 2)
    : i8 M3 w; {* f/ R
    , Q+ `3 L4 c: _7 ?# Example usage- @% b3 V3 M: y5 W
    print(fibonacci(6))  # Output: 8+ g6 y" v/ U  ]1 s9 m9 e% B
    ' o4 C5 J; W; z2 F8 E5 o& `6 y3 N
    Tail Recursion7 r8 R( q' |+ }# ~/ u$ q1 y, Y
    ( @4 F( L8 a; j2 S3 a. _- T
    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).
    ' d" k' q2 F8 e; o5 n9 V4 ?" ?: t" f# [  x' S- \. E
    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-12-2 23:12 , Processed in 0.029880 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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