设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    7 M, c- T4 h& Z  a( v( T( R" B! Q% _6 a( d
    解释的不错2 I5 U4 S, L9 ^5 h; h1 {4 B4 R

    ; O# m+ F$ T, _' |递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。. ]" T) Y0 p7 g5 {& z% j3 P
    0 f7 ?) S) x2 t( A; e) n
    关键要素
    . \- X- M4 P& _" |  t( z5 C% n- P1. **基线条件(Base Case)**
    + F3 z9 [* q* @+ g9 l4 B   - 递归终止的条件,防止无限循环& C% K* z8 H" n
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    * v8 `5 R: u  o7 h& t9 B# @8 V
    ) d% u9 ]. A( [* \3 T3 w/ S2. **递归条件(Recursive Case)**
    * G2 F* B! a4 b6 F' F/ I   - 将原问题分解为更小的子问题. t4 n" F* A- a4 d7 L: P6 S( d
       - 例如:n! = n × (n-1)!0 d# K* z/ z( v- b: C7 m

      Y: K9 W% H/ x2 b7 c: K 经典示例:计算阶乘
    4 ^, K7 t$ [& J' [python: z4 }/ h' P& G2 q9 m! Y) G
    def factorial(n):
    : |* N1 A) z2 j" Y& h4 Z" P    if n == 0:        # 基线条件
    6 f2 o3 C9 H5 E) N1 k- O/ V5 Q% z        return 1) L6 l8 L) v1 c6 d7 n2 U% Z  _
        else:             # 递归条件
    , V3 n& k4 T2 x        return n * factorial(n-1)$ C7 G) K* ?) Z3 f; t# ?5 Y. P$ K, }
    执行过程(以计算 3! 为例):
    9 K" y3 x4 T( p' V, o0 J" ]  Pfactorial(3)/ h+ T6 \- B  x/ w& c
    3 * factorial(2)
    6 U) V& f3 k2 q* l3 * (2 * factorial(1))
    & |& k8 _' W  {! K; g: j3 * (2 * (1 * factorial(0))). V% }, C1 h0 |2 `% H1 F6 z
    3 * (2 * (1 * 1)) = 6
    ; C9 q* n0 }% q7 r
    % `% [' u5 c0 p# _) ]5 H 递归思维要点
    ' O, \5 j, W% N1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    # S7 H; @4 w% ]$ D- D% g2. **栈结构**:每次调用都会创建新的栈帧(内存空间)" @4 n7 j; o+ a* @6 Y- g2 s
    3. **递推过程**:不断向下分解问题(递)
    * p! R8 B" C9 F4. **回溯过程**:组合子问题结果返回(归)
    + e8 I$ {+ h" w/ f, a- [
    ( K, S$ ^, Q  L- d# w! A 注意事项" {+ `' z3 V  S% ^3 D1 a
    必须要有终止条件
    ! [' @1 f$ Y$ u  ?# \+ A递归深度过大可能导致栈溢出(Python默认递归深度约1000层)7 X# _: k$ |0 f4 c
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    1 K$ h/ `# W) e/ H; c尾递归优化可以提升效率(但Python不支持)( k4 v/ ^+ X+ u/ B4 C$ ~5 Q+ X2 N
    : }3 C" G: p8 w6 t8 ?
    递归 vs 迭代
    * Q8 `. L. ?1 c$ F  W0 Y|          | 递归                          | 迭代               |
    $ Z+ T& Q( \  C2 X7 m|----------|-----------------------------|------------------|1 G7 C7 e& s. a& K# h8 \
    | 实现方式    | 函数自调用                        | 循环结构            |
    / t+ r* p" B; X3 N) @4 y# v| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |, ^; u) N4 n; W8 y6 B: |
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    " l- [6 j9 q) _+ e; G7 F5 K| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ' s3 s3 G4 a6 B. y
    6 d1 Z; \( D. H! v* W, s 经典递归应用场景4 a' k) m( Z) ]" x" ~
    1. 文件系统遍历(目录树结构)- Q0 S2 p# ~0 h) @4 z( J
    2. 快速排序/归并排序算法
    / R' W- ~1 d, g$ ]+ X5 W! b3. 汉诺塔问题
    0 d+ f1 g1 {+ ~) ^  ^' {% s8 [4. 二叉树遍历(前序/中序/后序)% g' X/ y4 Y" }. L( W
    5. 生成所有可能的组合(回溯算法)
    8 o* ]" p% o/ @" p: O8 s  K7 W4 O$ ~0 t
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    昨天 06:01
  • 签到天数: 3248 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,) R& c; m1 y' V6 T- Z7 [0 M
    我推理机的核心算法应该是二叉树遍历的变种。7 @* Z/ M6 v* {* I% ?" ^/ d2 O9 R
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:" `+ [. k4 u3 W
    Key Idea of Recursion. P) z9 O0 P! c7 r8 m

    # o! x- O+ g: S' s7 D) Z! OA recursive function solves a problem by:' Z$ w! O- G& ~; V9 {/ U: {

    , D; `, H* G  u! X, c- K5 A! I$ v    Breaking the problem into smaller instances of the same problem.! A  U8 n# Y8 U4 @+ Z. `  R+ t* ?

    7 T" g9 Y+ z) M    Solving the smallest instance directly (base case).
    % ]! f7 I1 a) S+ [3 A% Z1 N4 e
    " g+ H! k% Z8 z' ?6 c1 X    Combining the results of smaller instances to solve the larger problem.. D" i5 c& O1 K

    ; R  v0 v7 G6 t% _5 t" g+ g* HComponents of a Recursive Function) I! ?: ]$ `- ~- v2 S
    & \( ^4 H% G: h% w5 |
        Base Case:; q6 g$ ~' `6 h7 Y
    5 V7 B0 [+ F& ]' N- u7 m! I& Q$ M
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.: i- ^0 _( E: u

    0 c" Q% v1 i& x1 F) L        It acts as the stopping condition to prevent infinite recursion.
    , c' P6 _* o7 E: }! u# ]0 R  `9 ?/ f+ E0 s1 s% l5 ?
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ; g& o0 Z6 S4 O6 v' @" c- b) ^7 X3 k  b
        Recursive Case:& H- H2 P. ^1 W2 U, w7 ]' u

    ' u$ Q% _6 I. _) g        This is where the function calls itself with a smaller or simpler version of the problem.
    / C; n$ ~; I/ q" Q; `" Z
    * o2 R! T, b8 P4 I1 v        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).2 ]3 M4 R( `  F

    . o( p, F. r+ ~, E# |Example: Factorial Calculation
    ' x3 d! k2 G$ |1 j" Y
    & }6 f- D/ V3 `5 YThe 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:8 _4 u8 I! q, X: M7 \2 l
    " S6 j; Z( X  v& T9 k" T. e
        Base case: 0! = 1
    9 M% p( p8 v4 E3 w, s
    ; V' s5 H8 `, y- q' r: h+ k    Recursive case: n! = n * (n-1)!
    8 g. r) g% [0 h1 H5 n0 r2 O2 A, a
    / x, T4 d! o7 z9 E* T  s" tHere’s how it looks in code (Python):# j4 p/ Y( }2 |
    python1 q5 X2 ?. I- B) i5 s. i& X

    : t- ]1 t7 u1 }( t* C" o5 v$ s  D# ?9 n7 j6 v; q! z9 B
    def factorial(n):
    8 Z9 A/ _: }) W: B4 j    # Base case+ A4 k+ K6 L" e$ c
        if n == 0:) m9 F. e7 A; F& O/ X  I
            return 13 P" u' d3 ^/ O* x: u
        # Recursive case( q) O( s0 C* B; i" r; T
        else:
    ; }0 v- r) u, Y' e. n/ }, R: u0 y0 V        return n * factorial(n - 1)  g) j% h* C8 {) r4 T
    7 |( h& k3 J; D- @7 A+ }! _
    # Example usage
    * q1 w8 k6 K  k0 K1 q# a' @$ uprint(factorial(5))  # Output: 120
    $ B9 Y; `; Q2 [7 |- L% |3 M* a
    ( U+ L9 c# p8 f& PHow Recursion Works
    $ C! c$ |% L' U! h! V
    9 y. }3 Y( I6 j7 t2 a+ l0 v    The function keeps calling itself with smaller inputs until it reaches the base case.
    . N- L6 _# f) b- ^
    ) S2 U( U/ u" J* B7 V2 h" D    Once the base case is reached, the function starts returning values back up the call stack.
      p) v" t) Z9 @- t  R6 F
    $ o2 B7 Z- g6 ]  A) \  a    These returned values are combined to produce the final result.
    , o  i" h% R8 K" a
    - g2 f2 K1 u8 \5 z+ j" a: l! LFor factorial(5):
    + z9 M/ G$ X$ B; B( N1 @6 W& L. _4 x  v- W8 N

    % j' }' d2 N' w8 w) afactorial(5) = 5 * factorial(4)
    ; ?1 i- c; v9 efactorial(4) = 4 * factorial(3)
    4 N8 I& r* ^$ ]: p. a0 A4 `factorial(3) = 3 * factorial(2)
    5 u9 O/ A9 w6 W, B: qfactorial(2) = 2 * factorial(1)0 @7 g/ F& H+ M! s! N$ |# ]
    factorial(1) = 1 * factorial(0)
    4 h, E5 S2 v7 Q+ Wfactorial(0) = 1  # Base case
    - x" @8 [6 g. o2 \
    + ^$ @3 ?9 x4 f) vThen, the results are combined:  i8 A, H6 s6 i9 w# o. T
    6 P3 B8 S. [2 ?' w% E! ~

    1 a& o, m# t8 |- Lfactorial(1) = 1 * 1 = 1* U" F7 v0 M0 Y
    factorial(2) = 2 * 1 = 2
    ( G' o' B. Q- xfactorial(3) = 3 * 2 = 6
    8 z7 X' |9 e+ T% ffactorial(4) = 4 * 6 = 24
    0 s3 |, Q1 a$ D% {) v$ y: u3 N, Nfactorial(5) = 5 * 24 = 120
      F5 K* O$ q( Y) |. |- m( F+ u5 z9 ~
    # a( {3 G/ _* r6 NAdvantages of Recursion  \3 o" h/ k; b- L

    7 z- n4 C6 O- N% D: D    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).
    / I0 v/ C$ P9 c) q
    5 l& k3 i. _, ]1 Z    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    - g) ^0 o$ c: Z3 @- p9 G/ p/ z, w7 L9 z3 P- d: }
    Disadvantages of Recursion
    9 ~; _' A2 N/ l/ X3 T  ~& I9 z/ v+ o! ^) V: x& z5 p
        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.# V0 N5 J- K1 U" @/ L# }
    4 O9 {" o6 l$ F- R4 w8 D
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    1 Z$ `5 O, l9 d& D& z$ }% Z4 ^7 q, t, O1 U
    When to Use Recursion; v! d2 K" ~: }& _  e

    9 @: V. G* u/ K$ S7 X0 W    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    " Z9 {  G, ?( A" E  w, y' n' L$ [" ]$ A  g! |  S
        Problems with a clear base case and recursive case.1 }9 A% w$ q0 T9 {3 X3 {+ h

    7 @+ ], @% T$ ?4 fExample: Fibonacci Sequence* r, d: @+ J& B# }( K
    7 j8 o. t' }- T) |8 `
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ) @/ d1 z7 X2 p" c7 E
    & x) ]$ H- r6 j6 L    Base case: fib(0) = 0, fib(1) = 1' S. t3 g% r' f* f! ^) e+ P/ v! y# c
    * E6 _# ]6 x, u/ }+ X/ z- R: J! t
        Recursive case: fib(n) = fib(n-1) + fib(n-2)6 {6 z) h; W9 j. _

    . b4 C" M& r8 |+ ~0 b' J( ?; [python
    ! f, h$ x. p' v4 J$ e/ n4 q1 ^$ s6 k/ N' \' e) L% p2 j

    7 d7 n  q  H9 L* ydef fibonacci(n):: L" Z  d: S* ?/ R
        # Base cases5 h1 V4 Q$ H5 o) X8 i
        if n == 0:
    ! `, z* b' t4 o' F* J+ V* M        return 0
    4 Q8 ~! ^+ q( C- @* A    elif n == 1:
    ) M* u' y$ _; g9 w# t* \# U        return 1
    & y" }( n* c1 t2 i0 g    # Recursive case4 {) l# X: n* L; i1 E6 l( y3 `
        else:2 v' L  |$ c3 X- r) d1 K
            return fibonacci(n - 1) + fibonacci(n - 2)
    8 `+ [* d9 h' q6 ~1 P6 S: Q+ C9 p& c- |/ }3 ^4 o
    # Example usage2 q1 d% U7 S, I$ b
    print(fibonacci(6))  # Output: 8; k  y5 c, Y$ i2 N, q
    . F: m: v9 U( M/ e$ B
    Tail Recursion$ S- R8 _- f  P5 o
    ' v0 b1 e5 F8 i
    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).5 ]+ h6 C* e1 V

    $ R2 Y( L3 {7 @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-5-25 02:38 , Processed in 0.069354 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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