设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ( a, c" A) D; V/ T) c1 x4 h
    1 a3 ^6 m1 G4 o# ]4 g! A5 }6 Y/ R! O8 g
    解释的不错( A% `/ d9 o, y) K
    2 b1 c* S# e+ |  C
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。% v% R! K$ s! @+ m" Q( L
    . b. ~! Q3 n* u7 o+ @
    关键要素- C  ]3 }; V5 D
    1. **基线条件(Base Case)**
    - P/ ^) i. K& S5 x0 _1 S   - 递归终止的条件,防止无限循环9 P% v+ @9 i4 o, v$ l( z
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ; }7 L$ u% e8 Z5 R
    ) O) ?) E6 P+ ?4 s% Q2. **递归条件(Recursive Case)**( x- j8 t/ }& D5 ~7 P
       - 将原问题分解为更小的子问题
    ) g/ O8 m" P, w2 b   - 例如:n! = n × (n-1)!
    . @/ D+ |0 E& u  e& b
    % k1 v2 y$ B" f! O! s7 r- l% I 经典示例:计算阶乘8 R4 C2 w  w: e& J: h5 }
    python* t6 G  {8 ?4 f) I7 d9 Q
    def factorial(n):7 U5 L$ z( w8 @' D: L+ k5 B
        if n == 0:        # 基线条件' H3 e% u" |( _- s- O, d3 Q
            return 1( f8 W/ L4 g- m
        else:             # 递归条件
    " |4 U2 U! n& C) M3 d; Z0 C1 X- A        return n * factorial(n-1)
    / ]6 U( Z, K2 A6 t( z8 R- }执行过程(以计算 3! 为例):
    9 C" `' C. c! n" L! q- Tfactorial(3)
    * x3 ]) S$ @, Y4 F3 * factorial(2)
    / n( H' e" }) e7 n: r3 * (2 * factorial(1))9 R, j: s8 D5 T" I! s
    3 * (2 * (1 * factorial(0)))
    5 E- i& `+ V/ I* r. A3 * (2 * (1 * 1)) = 69 L# _8 E7 S  Y# N- u

    1 N% Z' k1 x8 }$ S 递归思维要点
    : e# c- a% M! a0 h3 [  T, l1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    . b) _+ y. o# h; P1 ]  e! x) y2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    $ z6 u0 J4 Y7 I, i. Y4 k3. **递推过程**:不断向下分解问题(递)- l" k7 V' x" L# X
    4. **回溯过程**:组合子问题结果返回(归)
    1 W! {- d. F( k1 l, Y+ V$ S1 L; y9 p7 W  g( }7 G
    注意事项
    4 W5 j( e! {! j必须要有终止条件
    ) l" |, V' b$ b递归深度过大可能导致栈溢出(Python默认递归深度约1000层)! n& K5 n! f# Y3 f
    某些问题用递归更直观(如树遍历),但效率可能不如迭代$ f+ D  ~5 y' w; _$ W% e
    尾递归优化可以提升效率(但Python不支持)
    # O* T6 a( ]; P4 m& l0 r# z. T+ B5 b9 E5 o$ G
    递归 vs 迭代8 P: M+ L' M( U8 Q  \8 s  |
    |          | 递归                          | 迭代               |
    # v  M. e+ x0 k. o5 D8 k9 y* u|----------|-----------------------------|------------------|
    . U3 Y0 ]  ]( `1 K| 实现方式    | 函数自调用                        | 循环结构            |
    3 |8 x+ n" O& [) U$ l7 R6 C| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ) H% s" T5 j7 v9 K1 X| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    * y' g# w- A- ~| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |' |' F0 P2 w! n
    * k( `( j  M4 J) |! s! Z9 a) d
    经典递归应用场景
    % ?# S+ P& z. O" R8 C, F1. 文件系统遍历(目录树结构)
    $ n% @7 b: g5 ^6 e# V  H( v6 O# M2. 快速排序/归并排序算法- O, D2 w' I" i' q; Z3 r; z' j
    3. 汉诺塔问题
    ; j/ t9 ~, m0 f4 Q. o4. 二叉树遍历(前序/中序/后序); H% w! ?* Q: ]6 J
    5. 生成所有可能的组合(回溯算法)" J; G+ a" x/ d/ G5 S' Z. V5 K. |

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

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,7 z, A) m" n! U- Q& \2 P- t- C3 K
    我推理机的核心算法应该是二叉树遍历的变种。
    & e7 u  m' [3 X8 i2 u. {另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:! R4 _) O. V* v4 H( T4 P7 y' q
    Key Idea of Recursion3 K$ h! O; F4 }. z# I1 P
    ; @* _, U# o% x6 P
    A recursive function solves a problem by:
    9 ^+ p  Z5 b/ x7 d4 y: C8 \# [: \9 U. l9 |
        Breaking the problem into smaller instances of the same problem.1 U2 y$ n9 P. x  ?( f

    " F6 @( d6 m1 @2 t5 a9 M  ]( e    Solving the smallest instance directly (base case).1 q) e( A/ Z6 z- p. ~
    ( U1 U# G6 c0 `( j) ]) K1 R
        Combining the results of smaller instances to solve the larger problem.
    # Y/ ?, B7 ~/ ^  n5 m! G( z6 |3 i( W6 ^1 p* R, o' P& L
    Components of a Recursive Function
    . K' g& d; `& L- ]3 e* \9 H5 y0 x0 F1 }9 c) i9 A2 j% b3 H
        Base Case:7 t2 L. P' n" c3 |1 s# n6 @9 s

    9 c  t( L( P" S, @6 p        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    0 Y0 z- I  p+ F! u: [* {
    ) X( p; L  P; U6 S        It acts as the stopping condition to prevent infinite recursion.. q3 |! x+ A1 Z4 P; U1 `

    3 {3 @& R4 {' R* Z: w        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
      p, N/ C- W# Z6 x) E  H' R1 V* _$ p0 F: N
        Recursive Case:
    2 Y" }* @/ J$ _7 z, r  C" {4 B+ E* T8 _- j8 H+ Q9 Q
            This is where the function calls itself with a smaller or simpler version of the problem.
    1 Z7 ^4 K' a* g8 T/ R. B& K3 g7 G1 Y3 S( E, ^% W* w
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).4 T2 v3 r- H" z& Q6 M; L# |- _6 O

    % g7 U& Y7 O( \) eExample: Factorial Calculation
    5 g6 p0 b# C# B4 @6 U* c* U% a* V" y' 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:) z) Z+ e! m! c! g* B3 w' a1 I& d# T
    3 ~' x; q) ?$ i8 L) U+ m: N0 \5 `/ s
        Base case: 0! = 1
    * j; I8 ~) X( V: x
    . j- H% B* y1 v0 c, C    Recursive case: n! = n * (n-1)!' H8 x# B* U/ a6 _) g( q

    ( o8 I. f# ?! }6 f8 J6 YHere’s how it looks in code (Python):* L/ M5 s/ ~0 ~2 n4 _' ~: H
    python$ `& N* Z. g; L2 T
    ' r9 U) d% \) d  p8 a# k$ a! w4 X

    " b6 y4 t- ~3 wdef factorial(n):; e7 E1 d' L" @( i
        # Base case. E! ~3 s& m$ O2 |
        if n == 0:# k7 u" @. X0 A2 Z
            return 1
    4 F* n" r: L' t    # Recursive case5 T! ?6 v. n) T* x/ z+ G. ], p
        else:
    2 I$ v" u; g0 ^- X        return n * factorial(n - 1)2 X6 N3 ^/ c- R- Y% g$ {6 R4 b

    4 g' T8 A6 n3 |" I+ r$ `4 ]# v; |# Example usage
    + q6 y1 C" ]- u5 `; Iprint(factorial(5))  # Output: 120
      T/ |; _) G8 J* G5 }8 k* g% A( E- k. m4 s5 n, F% R% _; I1 U  _
    How Recursion Works; f, O, E* j: ]2 ?6 @
    % \6 z4 s5 [, t7 q, h# Z2 r
        The function keeps calling itself with smaller inputs until it reaches the base case.
    $ k9 Z+ K0 a3 e, v6 h6 ]
    ' T) ~+ `7 f- V/ V4 n    Once the base case is reached, the function starts returning values back up the call stack.- P, I: G6 \* t. `6 Z3 {

      V+ p+ q1 W' }5 a! j    These returned values are combined to produce the final result.
    9 b: Y+ e+ c( c" M
    ' L2 j6 ]9 F! F, f4 g9 uFor factorial(5):3 ^) X8 A4 C: I6 J0 j4 X- ^' }5 S

    2 s$ y0 ^" {" e$ r, w
    8 v3 t$ g( v" K# F: Z3 H* P. Qfactorial(5) = 5 * factorial(4)
    8 o4 ~1 D6 X! \3 Kfactorial(4) = 4 * factorial(3): d) Q( I* v+ t2 F7 Q7 v$ X1 n
    factorial(3) = 3 * factorial(2)( c! l  N9 ?- ^! w' w+ I2 H$ d
    factorial(2) = 2 * factorial(1)
    5 W; S# m8 h0 g' g6 P9 Bfactorial(1) = 1 * factorial(0)  F* R/ T3 N* L! L
    factorial(0) = 1  # Base case
    8 R# u  _4 C- \9 E* w1 D8 _
    6 P( t  ^$ ]% [) h8 B' o3 lThen, the results are combined:& k0 P" D1 v& `6 G: ?

    4 Y* B! a0 p% ~) `" `
    " n( E: Z; I: ]; Sfactorial(1) = 1 * 1 = 1
    0 a0 O: c8 ~% h* `factorial(2) = 2 * 1 = 2
    5 P; J# ?% D% m) l: Hfactorial(3) = 3 * 2 = 65 m0 {/ C, u/ R  G9 K/ o( U$ N
    factorial(4) = 4 * 6 = 24' O) `" F& T! b6 s0 G; q6 V2 ]! X
    factorial(5) = 5 * 24 = 120
    3 R: w$ b( a: K0 `3 s" Q/ ]0 A- F" [; N7 ^& O
    Advantages of Recursion. E* w$ D1 U" |: ]4 r: a

    8 c9 M# {& [% V6 m! M- b' 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).: m  c. D; s& }) {
    5 b& _! p# D+ x- A2 j
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    " \3 F: t. z- o) F8 A( m9 ]5 ^7 J% L' S8 l5 F
    Disadvantages of Recursion
    - T* R" H" ^2 m
    0 r% Z, o+ X' j2 ^' ^4 x' s    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.2 X, P# q# ?  z, c2 Y2 z2 A
    9 J# C( N4 d* p! c& R& N# ~
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).& b! g- t- b  E) H3 D' |
    4 J3 t& o; v) g
    When to Use Recursion  P7 F2 F1 P8 R5 S

    ! K7 x4 p) v' m& G    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    . q; G* }8 _2 b4 w% O
    . `# u) C8 M% |    Problems with a clear base case and recursive case.1 k: l2 q: D; c2 O1 r5 u
    . d  t* q6 o* C# `( I$ M: U
    Example: Fibonacci Sequence7 |7 j0 z  N+ v9 z. d& Z

    ) ]' j8 s6 P; w/ m3 {7 o# {The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    - r2 T4 W- C5 @2 @+ a1 k& I) }& s" f7 N3 s. z
        Base case: fib(0) = 0, fib(1) = 1
      C$ ~$ K. M$ E9 x- _1 F  C. w% S/ ?6 ~' S( A3 N  ]5 |
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    4 i: c8 D5 a" C' |* _: h- T5 _5 {5 v* ^; q" `6 n6 C6 U
    python7 k0 r1 _" b7 ]& z4 W% c1 u

    2 {4 ^+ U" I, y  j6 N. p. b6 `! p; v* Z% ]; |3 S
    def fibonacci(n):
    , x. {) u$ f, @; x( [6 n3 r: Q' ^: M    # Base cases
    5 s' L" u/ O' \  C: y    if n == 0:4 f6 ?& L& n! Z5 K9 t
            return 05 F7 n1 g, }4 y' h
        elif n == 1:# l9 h8 j% ]7 M3 I, q7 A5 d
            return 1
    ! ]% a  b! B+ K; X    # Recursive case) E- p2 e- R$ f8 B) F& R8 r! p
        else:
    3 ?( ^* D3 {9 j' i6 v$ T# W2 E* L        return fibonacci(n - 1) + fibonacci(n - 2)1 J: }6 ?! d$ d$ o' ~- ?! F

    + R1 j: F6 }8 z  r+ D  b# Example usage9 M  t, }5 f- i! o5 o
    print(fibonacci(6))  # Output: 83 W# O6 c' ^& \5 ^' W* w

    9 ]/ T  U- R- A( ETail Recursion
    9 u' c9 i, e! L7 {3 g7 Y5 o9 J+ R7 U5 p& C; P, H- I) S
    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).6 ^  v. l- T) p0 C
    1 B, M1 s; k# T$ N! J& i
    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, 2026-4-6 16:22 , Processed in 0.056625 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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