设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    8 b8 l8 c1 X; C0 {; s, `7 d/ m% d
    解释的不错
    . f' `8 w% l" Y( ?" g5 y& G7 m) @, X) J5 D. X0 m. D& d
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    + ]# \$ p( `* |& W  A$ i5 s# }& x' G* v0 H
    关键要素
    1 T$ ?) `' j. ?4 A3 P7 n/ |1. **基线条件(Base Case)**) Q- V+ w, ^4 n1 v" P
       - 递归终止的条件,防止无限循环
    - S* Z7 E  ^- F7 x   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 12 {" e: \7 A8 Q9 j) l  R5 Q" @

    : n& \6 Y( |% E- a+ L) k2. **递归条件(Recursive Case)**4 O" g  ?% E: \! ]/ `0 Q
       - 将原问题分解为更小的子问题) c% {0 v  j- h0 g! ?# E' a. D
       - 例如:n! = n × (n-1)!
    ( X, z+ b0 m6 c! M$ g) V6 q
    , b# V( F3 l9 H8 n6 ^( B 经典示例:计算阶乘
    - W, M( m1 l7 a( x, \python
    % N* [4 C0 @6 b+ pdef factorial(n):
    : T1 p1 R1 U/ z: q8 L2 G  m. @+ u    if n == 0:        # 基线条件
    2 a- b1 U3 f3 h; Z" w5 d        return 17 Z- O9 p% T7 D' M
        else:             # 递归条件
    # C4 V$ |, a0 b        return n * factorial(n-1)/ N/ u& J  o6 M
    执行过程(以计算 3! 为例):
    + I& O, O/ C" ufactorial(3); J+ a6 V/ _5 w8 F
    3 * factorial(2)& k) p1 L+ P% e* C9 l' g
    3 * (2 * factorial(1))
    ! U2 A, X4 c3 |5 s+ l! g3 * (2 * (1 * factorial(0)))
    0 s$ n. R* q  K3 * (2 * (1 * 1)) = 6
    ; c6 ]/ t1 L2 ?4 B: ?# Y( Q' i/ }1 b" {$ @
    递归思维要点( h7 V8 Y0 R% Q) y
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ) H. X+ c6 i, q$ j3 W5 y2. **栈结构**:每次调用都会创建新的栈帧(内存空间)' S) u3 O( u& D7 g
    3. **递推过程**:不断向下分解问题(递)3 G  @9 D5 E2 y$ K
    4. **回溯过程**:组合子问题结果返回(归)
    % E" L8 H8 j- I. f: ]
    ; h8 l+ _; Q3 T. {: ^! R, { 注意事项
    - K) q4 Y9 Q! w& g4 T& ?必须要有终止条件' b' l2 c3 R2 L9 a# w3 Q3 ^' I
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    + i4 c. ~1 k+ f: n) _某些问题用递归更直观(如树遍历),但效率可能不如迭代
    3 T  R( K. \% z/ x尾递归优化可以提升效率(但Python不支持)
    " @) c7 b/ Q* \4 T
    2 r6 V9 X- l6 ~  B% Y8 y 递归 vs 迭代
    + Z% M/ Q8 s! h4 f, b" J$ o8 t|          | 递归                          | 迭代               |
    1 c4 v, s8 I' I  c  I|----------|-----------------------------|------------------|
    % e4 K$ e  o( Y3 ]0 _| 实现方式    | 函数自调用                        | 循环结构            |, R& H* S  o. o$ M/ h
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    * T* X, ]' U& `  M| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    4 e2 X7 x# w* Z( o+ o| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    " I3 O: j# D' L; _* {6 Y+ S# t4 j" S5 G  W8 r7 q
    经典递归应用场景0 P; Y6 i: [$ ^! @( P  a" ~! M
    1. 文件系统遍历(目录树结构)2 m- X% u7 U  M  e/ k9 n9 a; i
    2. 快速排序/归并排序算法! D2 H% q+ ]4 W- G  d
    3. 汉诺塔问题; r' e: X+ c2 v. C6 W4 U. B
    4. 二叉树遍历(前序/中序/后序)
    ; V8 B: j4 d1 d) J: m5. 生成所有可能的组合(回溯算法)
    ' Y' N8 w- m+ a2 o$ z+ E- b3 J& W- U$ R
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    昨天 06:39
  • 签到天数: 3105 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ( J4 n/ N1 [. R  l5 I" n我推理机的核心算法应该是二叉树遍历的变种。
    - g- b! s8 i6 ~" g- l% `6 a* }另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:4 o+ m1 a( Y5 B  x
    Key Idea of Recursion  ]: r; I7 `" p7 l/ j, W
    1 Z% l- _. O. n! @4 w* C8 R7 w
    A recursive function solves a problem by:
    ) |& [, B/ X. z5 K! b5 N# W0 g! t0 t1 q2 u! X
        Breaking the problem into smaller instances of the same problem., h7 ]3 V: i5 l/ l  Y" J. U
    , L* N$ x$ c& m! H. y8 H" B
        Solving the smallest instance directly (base case).- {1 h$ w. G7 E0 W8 ?) x5 s1 S

    ) _3 P  P! a0 Z1 f2 M/ u% f    Combining the results of smaller instances to solve the larger problem.: a, Z- L& c3 w  h8 H" ]

    + o: t& i  F, d. y" tComponents of a Recursive Function/ c0 D* V! f1 E

    7 Y* _" ?% _& Z3 q% Y* _    Base Case:
    3 v1 o$ L3 A& G
    7 R  O8 ?8 }3 n, {7 Z' F" P        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.) ]7 z* n: M3 \5 U' Z8 ~
    / T! p/ m. u. d4 ]
            It acts as the stopping condition to prevent infinite recursion.
    ' l8 e8 C9 }0 U
    ( o( W& B# \/ @  A/ w        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    * F- A. R/ R/ X: `1 \' r
      j' w4 ?! S! }. v5 t! M8 E0 s    Recursive Case:8 O" \: O; p' s8 h. z8 ^8 K
    9 m+ E, U8 G- N2 h
            This is where the function calls itself with a smaller or simpler version of the problem.
    * G1 A8 T" p/ k9 K& s5 a3 a
    ! K3 P0 W  }: O/ ?0 s0 m        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)./ D6 N) o  X" S) \9 y" G
    5 ^) O' c4 T4 E4 E) ?$ U4 f9 ], T
    Example: Factorial Calculation
    0 n( U2 `8 \) e3 G3 G& |) S8 i, X: w# l; d( @" c
    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:
    , s5 R' j/ g* E4 g% ]1 L5 ~# w8 ~4 f$ w* R. z
        Base case: 0! = 1% i* G2 K; M$ P- W

    : ]. p1 F4 s! q5 V( H: A- m% _% O) ~    Recursive case: n! = n * (n-1)!
    1 I$ ^4 N3 I& A- X) L) X/ W; L; ^# a5 }
    Here’s how it looks in code (Python):0 x6 `1 N9 J6 o. T
    python
    5 {  j, b) _1 n3 f5 l$ r8 V: r2 e6 x1 ?4 P: _7 @& @

    " F# u! D" ^2 T, {# W. Zdef factorial(n):
    5 H9 ^  g% X& _4 d) [# c    # Base case, Z" \8 r8 N. P8 M- _; @4 n
        if n == 0:* [: n7 K8 D8 {; h( e
            return 14 m0 s) F$ t% c; B2 Q! B
        # Recursive case
    & r9 t$ _) a/ b, F7 G& b    else:+ Q4 Y' O- Z9 _/ V) i- ]" c8 c% {
            return n * factorial(n - 1)
    # o4 I) N" \) j2 x! l
    0 P! C# G: {* d4 i+ s4 s2 D4 O# Example usage
    4 e3 }, R, x/ C. R2 c  }( |print(factorial(5))  # Output: 120
    0 O; d+ C& ^! _5 B- D4 w5 U: q+ B+ P: w
    How Recursion Works
    0 l7 D$ [! b# ?5 }* D2 }
    3 [6 _$ W" W' |8 G2 o! D8 Z0 Y- m    The function keeps calling itself with smaller inputs until it reaches the base case.
    ( J) D1 x6 Y' t) |7 b
    ; [* v( C/ q$ o$ g1 Q& f    Once the base case is reached, the function starts returning values back up the call stack.1 I, n; I  @0 C7 _* u
    ! ^- |% h9 Y' S1 t3 ]0 J( P
        These returned values are combined to produce the final result.
    # P6 w' z( }# E# U$ e  v: j! O1 A6 J9 ~, ?; q- O9 m, C1 S  j
    For factorial(5):- m# }- r4 t+ w

    # y9 z  c4 G& u8 d( B9 {$ M; K! }8 y  H' M2 i1 p
    factorial(5) = 5 * factorial(4)
    0 @) V* N  A2 ^- A7 y4 W3 g5 b6 jfactorial(4) = 4 * factorial(3)
    # k  I8 c$ {) `* H4 ifactorial(3) = 3 * factorial(2)" g+ T, H7 {" ?
    factorial(2) = 2 * factorial(1)
    , ^* ^. f7 L2 I$ J$ o. Wfactorial(1) = 1 * factorial(0)
    ' N6 ~% c* T& h( B/ ^( [6 ofactorial(0) = 1  # Base case
    . X0 F0 c6 f, ]5 K! @7 X+ q" q6 I, i. W
    Then, the results are combined:
    4 B  [) u* j- R3 ?: s, l* J' K
    + f( a: `* f+ q6 C
    - D2 ]4 h8 f! I6 Q4 o( Mfactorial(1) = 1 * 1 = 1+ B1 p! T9 o* s/ k4 v! p# [
    factorial(2) = 2 * 1 = 2
    8 C) r5 u$ L# D. q- `/ U# tfactorial(3) = 3 * 2 = 6
    - r+ B& ]) E* c: w: |. Jfactorial(4) = 4 * 6 = 24  Z. j8 B" l4 N' X
    factorial(5) = 5 * 24 = 120$ W! r8 ^; l( E/ s, Y
    / R& v1 W( u8 }; i' ^
    Advantages of Recursion1 Q  S, K8 o! i& {9 X" \- z

    5 F0 c4 W1 @; y. B9 K) _/ y* D/ V    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).
    1 ~% g6 S# B$ S+ \6 f5 w. t& b1 \. }, @. k  r. d) w$ E
        Readability: Recursive code can be more readable and concise compared to iterative solutions.: K% O( T& h, y7 K# H5 s: E; }

      U. J# k9 u& V! x" aDisadvantages of Recursion$ u& h" p) k. e, J

    7 l- Y8 I; ~0 H+ l) P/ j    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.
    : |( ~) G0 U4 b: T; h& ?! @; e0 X2 r& i
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    / ?) Q' r' I7 a9 m% u( F5 m$ V
    ) E+ m( c' u3 N: V8 Q4 D- K4 HWhen to Use Recursion
    - v1 D  Y8 W, Q3 [' }
    ! R$ R( I6 `# X. _    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).$ {: {. f4 E4 e5 u2 W

    ' J3 W( k- Y8 Y# Q: Q# e' `7 x$ g  Q    Problems with a clear base case and recursive case.$ [  z6 |4 y% D; V) I0 g" R, d

    0 k  K- D+ n& u0 z- F9 H$ yExample: Fibonacci Sequence
    4 j) m4 r# ^$ U  D; N! _8 h; X; W3 a( S1 A: K! f2 d
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    . d0 \- E0 b8 M0 G+ j+ X0 Q; [" P4 M9 v% |' q- s  j
        Base case: fib(0) = 0, fib(1) = 1
    ! c5 H9 \/ i6 f  \2 }
    ' ]7 p7 U7 S, M. O    Recursive case: fib(n) = fib(n-1) + fib(n-2)' ^- H6 P; C( T

    6 r1 l7 ?0 _2 g, Q5 P8 ipython' D! t6 ~5 ^' T

    6 K7 r$ {) ]( x
      e" y; _& f) M1 u: q, bdef fibonacci(n):
    4 {6 Y, C! r- C: k$ A    # Base cases
    6 i! A6 _! V4 [    if n == 0:
    + j3 Z. a( _% f) q: k4 ~) @        return 0* g$ M, G1 r, @2 x- K5 {3 A
        elif n == 1:; t) ]% z3 p6 O* I' a9 |
            return 1
      G, ]' t2 m# q, h$ o  H    # Recursive case3 D8 r& z4 l. R- O' s8 R1 Y
        else:6 b  `% Y* P1 O, e+ N. f$ {
            return fibonacci(n - 1) + fibonacci(n - 2)
    , ^+ r) D% u$ y  |
    8 y* R4 E! ^( j+ R# Example usage
    7 e! ^0 |0 o* T# Bprint(fibonacci(6))  # Output: 8
    ! \+ T1 P& Z2 H8 A0 g: w
      J- v; W  P" |/ O3 E* DTail Recursion
    % c' ], v% }( M! E2 o7 `9 H
    , e: g  e% A5 PTail 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).
    - a% i9 J4 |7 h9 _+ k% `  W
    ( \5 k# U6 R6 h2 [! hIn 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-1 06:24 , Processed in 0.031293 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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