设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 2 M8 _! k" q9 K/ R

    0 q1 ~& B3 G1 }9 i解释的不错/ G# f! A/ U+ q( T$ w

    . Y# z$ R" L1 i/ l6 l递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    7 ^4 a! Y- b( l" h* |+ [
    ; `% N5 M6 D! [  h' h 关键要素
    ) H" a3 u* P. p2 O  `( v# k1. **基线条件(Base Case)**/ {! x. r1 _! U' M8 f
       - 递归终止的条件,防止无限循环
    3 F+ A! \8 X% V: W$ @0 \% f' ^+ N" S   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    % l) m; S0 x! ]
    3 {! ?! C. r6 ^) U4 Z2. **递归条件(Recursive Case)**; u" ]+ [& G. l, a
       - 将原问题分解为更小的子问题8 V1 Z/ V  M7 G7 L/ v
       - 例如:n! = n × (n-1)!
    ( m3 Z6 Z( c7 q5 b+ L( |; |2 [; e: S: u* ]6 s" H/ W  M
    经典示例:计算阶乘2 b: A5 H% y: K9 v# u+ s; S
    python+ |: N/ v% i, Q: v$ |& l
    def factorial(n):/ x1 v4 D2 @3 A% P$ l( s& X
        if n == 0:        # 基线条件- Q/ X4 Q+ D& M$ T8 r) D8 {3 h& C, V
            return 14 O$ O+ H+ R( e: A3 K) G$ d
        else:             # 递归条件* _0 v& l  g3 ], v( W, O
            return n * factorial(n-1)3 e' r4 y0 X6 x* x
    执行过程(以计算 3! 为例):) \) F& i& i7 _
    factorial(3)! m( f/ G, M- ~& [: J& D
    3 * factorial(2)
    % i3 x( j& r+ Y# F" s1 m% j* ~3 * (2 * factorial(1))* R3 E7 n5 U  V9 `# G
    3 * (2 * (1 * factorial(0)))
    6 O9 s5 D2 T! t3 * (2 * (1 * 1)) = 6
    / V% U" n4 O) r: o8 o$ {; i% i  g; o
    8 B1 `; E- |0 ~1 [9 p* F; ] 递归思维要点8 M" V# i& ?+ p5 Q) c) y& ?3 z
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    4 W; D- k9 }& c: i  R% B( e2. **栈结构**:每次调用都会创建新的栈帧(内存空间)7 p, j# e* Z! a3 _3 ]% v
    3. **递推过程**:不断向下分解问题(递)
    0 r$ u5 Q3 Q/ `) F- N5 M$ o4. **回溯过程**:组合子问题结果返回(归)0 |7 {* D$ \3 ~$ g2 K1 r7 a$ H
    7 U2 \& g0 G" M) r4 s
    注意事项
    + \- j3 M+ S5 J5 N必须要有终止条件
    6 A" D/ }6 v" S( u! E% i; B- V6 e4 [递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    # b0 i7 R! q- b$ L某些问题用递归更直观(如树遍历),但效率可能不如迭代. x9 F% u: x) `0 e
    尾递归优化可以提升效率(但Python不支持)+ R* y. H  c5 d" V: i) y
    ' u0 g8 l( S  P' Y
    递归 vs 迭代
    / H; n# U6 E1 d( w" g! |3 q, v|          | 递归                          | 迭代               |% A* i  ]& T8 |1 L4 o% G" C: @3 ~
    |----------|-----------------------------|------------------|) L) \0 v! p# W1 T
    | 实现方式    | 函数自调用                        | 循环结构            |
    4 T1 V8 G) A! C# V8 |6 W| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |, f# N  r$ M( R$ l% A( I7 C. `9 `
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |$ x% v; a' c3 h* i! U; n" A. f
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |6 L* n/ v" U) M, d) _' q
    3 z$ B4 k' F* E# c6 U" v
    经典递归应用场景% r6 E# V+ |4 |+ R7 \
    1. 文件系统遍历(目录树结构)5 a/ \4 L4 `3 _$ _; Q% D
    2. 快速排序/归并排序算法2 I7 z  O9 p0 {$ g! J
    3. 汉诺塔问题
    ) d) Y- X) A, S1 ^% w3 {7 h" L/ W4. 二叉树遍历(前序/中序/后序)
    7 g- D& T: ?& z% t# E6 l5. 生成所有可能的组合(回溯算法)7 r: v5 }( z- H' J/ B2 O

    & w$ s0 }5 s8 {( M- J( S试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    13 分钟前
  • 签到天数: 3088 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,4 L; P8 T4 {* j# x
    我推理机的核心算法应该是二叉树遍历的变种。
    ; s* D* _: L0 j5 n  B4 H另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    : W! ?/ \5 o' b8 G" h2 CKey Idea of Recursion
    * a8 O; ?/ A1 N  v
    $ n2 J+ J' i' h5 i8 Y0 WA recursive function solves a problem by:
    0 x* n: X2 D  Q2 r4 g) N; ~+ A" G8 \
        Breaking the problem into smaller instances of the same problem.% K- ^5 L$ K; U( ~

    5 {0 o0 }. q/ n: m  t3 c    Solving the smallest instance directly (base case).
    - C: h, ^' f7 v. f+ z4 U
    4 r8 d# i- h( ~( w" b    Combining the results of smaller instances to solve the larger problem.
    8 @" U- `! s% d1 Q* d' L
    # j1 i+ M/ D2 Z" {) @Components of a Recursive Function
    ( ]9 s2 F3 }1 r) ^
    1 P1 _9 d: s/ E! x" A. @  D) y* u    Base Case:
    + _! m5 I8 T4 Z7 y+ x* |+ n) b- x. L+ Z* K- @
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.4 k* h, l$ o( ~0 x) C4 N- E
    8 _8 K5 E) e7 O. w
            It acts as the stopping condition to prevent infinite recursion.
    % S7 I1 J+ x! Z$ G& O3 v$ p3 ~1 \: M$ i4 d
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    * y4 X; M& K8 w8 \* Q- x8 N( y
    ' m2 t! u& \' c    Recursive Case:
    5 N3 J+ j4 ^. M! H/ T: m/ H& }1 U8 {
            This is where the function calls itself with a smaller or simpler version of the problem.: b5 K) h4 H: J2 X

    7 ?, R6 y* X$ S7 O  z, p2 T        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)./ E0 T$ X% M/ W& K" s$ N2 L

    . `* S' k7 x- b2 I; x' gExample: Factorial Calculation
    , M3 \; j. J. U: d! p
    / o9 d3 {7 O! C1 X- o' B- r& jThe 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:
    1 U4 O% P9 S- n' b) f9 @
    7 s: B& B0 V/ m4 I3 i, T* n    Base case: 0! = 1
    " U* Y% F, c* j% ?
    * N% \! F  n0 m* A    Recursive case: n! = n * (n-1)!) T$ X6 l% d7 n" X7 m+ y$ _

    ; @- M: w1 V6 A: d3 ^Here’s how it looks in code (Python):
    ' P9 m  S- S0 A% b; `( ?python' o" \' Y+ T6 Q# {, ]# z
    , v( @# h& S8 r0 [

    9 j% s$ a9 e2 {/ x& R. Mdef factorial(n):# @& ?7 S" P; c& @0 W" @
        # Base case
    - \- b/ x" K; l    if n == 0:
    8 d7 x6 a; t# }        return 1+ _' @' M( k) s) ^
        # Recursive case
    7 u0 p. X5 F! i8 B7 M    else:
    0 j0 x1 J3 N0 X  G3 {2 h. `9 F& S        return n * factorial(n - 1)3 l4 u% ?! g" l2 K3 C

    6 P& ~. r2 Z7 i6 P# Example usage
    . L$ z% @; c' F0 J/ k- z- uprint(factorial(5))  # Output: 1207 i7 m( \! _% A8 N0 j
    2 ]2 R+ @* r. C) C
    How Recursion Works1 y( M2 @* {. _% D2 ^6 @

    1 b6 j) c( D% K; y3 S    The function keeps calling itself with smaller inputs until it reaches the base case.' @2 Z6 Y; T& l0 s

    ) R, N0 P3 o7 N6 ^' D. J6 |* f    Once the base case is reached, the function starts returning values back up the call stack.
    ! a1 K4 o# J) y. h7 C; m& E8 |
    6 r8 N9 Q% m3 U    These returned values are combined to produce the final result.
    8 `- u6 M: q" {8 u; V7 m4 s0 j$ w+ k2 x# q6 z* [  [: _
    For factorial(5):9 ^; \  |9 C& Y" O8 e( f
    # A: E1 b2 L2 {

    - J/ Y: h" Z4 B; ufactorial(5) = 5 * factorial(4)
    / v) d$ y% R/ A9 r1 k& w7 l" \factorial(4) = 4 * factorial(3)
    2 F; B) Y& A4 {# i6 V% {! y& X2 Lfactorial(3) = 3 * factorial(2)) W- H! n* w" G5 z
    factorial(2) = 2 * factorial(1)" u9 X! C3 a& X7 q# C
    factorial(1) = 1 * factorial(0)
    0 R( a& l2 `9 hfactorial(0) = 1  # Base case3 y& S6 c9 R6 H3 j/ m/ X# y

    1 B2 W1 R6 ^9 I+ L- _Then, the results are combined:" n1 L. F/ N  ]+ E; H/ R: ^3 N/ k
      G( F- ~4 Z$ ~5 e8 d! K

    0 M# I& M, \# l8 f  M* Cfactorial(1) = 1 * 1 = 1' }5 V. k4 Q: Z# T1 h/ h
    factorial(2) = 2 * 1 = 2
    6 l4 S: U9 K2 l' G. D3 Rfactorial(3) = 3 * 2 = 6
    ! d+ Z( }+ E+ }/ tfactorial(4) = 4 * 6 = 24
    - x: K, @* g# Ifactorial(5) = 5 * 24 = 120
    4 J6 D; K! ?* H* X7 u: D+ v. y" f8 F
    % k6 @$ K6 v1 I3 ?9 O- r# u9 V0 GAdvantages of Recursion# M) w" B7 I8 X/ A

    / p/ P% \" |8 g7 d9 o/ @/ 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).# U9 ^$ I/ ?4 z1 i; s5 R

    # H2 _1 m- b, U; L, p    Readability: Recursive code can be more readable and concise compared to iterative solutions.! O& z% m3 Q0 a4 @
    1 H( M! z! f& F- X
    Disadvantages of Recursion: W  O3 z# o) @0 p
      k3 M3 j" [, B2 |$ F3 k* s, V' k
        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.+ \- n% @# P  G

    / [5 U0 X7 q% _. _. q' k& ^    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    9 F4 f# o) `1 C1 L1 K! b7 ]6 d& u) p, d) L0 c! F- k+ D
    When to Use Recursion/ R- a; L+ v% r8 s
    9 F" N. v) G( H' o/ t. E$ B
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ; s" |9 d! C% K4 n, f$ `/ f$ E% v5 k0 k: x5 u8 W, H+ m+ U/ C
        Problems with a clear base case and recursive case.! m5 V  S, O) D" g5 D# w
    0 f" I- n1 h; D2 A, C7 O
    Example: Fibonacci Sequence
    8 @- e0 B% O# G5 Q1 a8 _4 y9 y2 D; S  P
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:5 h  ~0 }7 T1 {
    / S. k; r6 a, {& ?1 m7 l
        Base case: fib(0) = 0, fib(1) = 1
      U; _$ u' C- }, N4 _8 ?% j
    & r2 g4 ?' {3 H: B    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    0 Y# L4 x( F8 J. A) ]; h0 A0 u2 i5 P: L2 k
    python
    * B8 P3 q7 n8 X7 \4 Y1 ?$ Q( U6 p. P" p
    - N! }4 w7 m2 h$ h0 m
    0 C& `3 }4 n+ u5 b/ F6 E. jdef fibonacci(n):/ R) @2 Z1 Y3 a
        # Base cases0 S/ {& N3 p0 [7 r6 C; T
        if n == 0:
    1 t9 A5 n$ H' }) C6 g        return 0, _2 T2 V: \# y# A
        elif n == 1:
    & y' u- h( A3 E) f: d3 E. t        return 1! c: X) y0 |/ s4 h0 n( F3 R
        # Recursive case
    0 ^7 z; _8 w6 N( T+ L9 K    else:- y+ o2 N$ H( d8 v
            return fibonacci(n - 1) + fibonacci(n - 2)
    . J9 l3 o, Y/ o- b- O0 p7 v& I5 `' s. M6 S. w
    # Example usage# b' H: Y7 u2 a
    print(fibonacci(6))  # Output: 8! D  C( _% w. x) T+ [2 q
    ! ^6 |3 R8 n) j6 U4 i7 E- ~% p
    Tail Recursion
    7 e: r9 U3 |: c5 h/ |7 r1 I
      i* q& M" L0 x; z! y) q/ bTail 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).
    4 b+ O) m9 d" G2 ^8 ?* K0 `( a" Y2 J$ r" r( u  g& S# \+ |
    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-11-9 06:32 , Processed in 0.029796 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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