设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    3 f/ C2 W4 R; s) n7 u. G" ]7 D
    4 u+ Z- [3 ?  [- s8 D& f解释的不错' j# q& }$ f2 v$ V# y
    4 @5 M' ]6 {: R- ^2 j2 P
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    3 H+ ?$ y) V* P  `) N1 V( e. k4 [9 o
    关键要素2 \/ a, k: A1 H) b  H& }  ?
    1. **基线条件(Base Case)**$ ~: I* m  e2 N1 X( k
       - 递归终止的条件,防止无限循环
    : \- n2 L6 j/ ?% D. }, b7 x   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 15 M5 _" }. l* k9 n! `! @# g! j3 {( Y
    0 `6 `: t2 d) v. |& v' J; ~
    2. **递归条件(Recursive Case)**4 f: o7 O% F% c9 w$ l6 ~
       - 将原问题分解为更小的子问题
    0 U+ i+ ?6 j3 T: m! `% Y   - 例如:n! = n × (n-1)!1 w0 A' c# H5 q. j: D

    ' }/ m" n$ s# [0 p7 D, k 经典示例:计算阶乘
    : G" d+ V! b. v" I" b9 ?python4 `$ x* q7 N3 }, c9 ?
    def factorial(n):  t' j, {. E, J3 H; p
        if n == 0:        # 基线条件  ~2 C0 m9 E$ p2 Q+ t( h/ q' U
            return 14 L8 Z$ W' `7 r. S
        else:             # 递归条件* G6 B4 Z) C! R; g
            return n * factorial(n-1)
    ; c* E  }9 E: E2 f执行过程(以计算 3! 为例):9 W( {7 n& l' ~7 P8 }
    factorial(3)
    3 h. K* @( ^4 e3 W+ T3 * factorial(2)
    2 U: c- Y/ \, i) {3 * (2 * factorial(1))7 o. g# |1 k# L4 |; o* y9 m$ j
    3 * (2 * (1 * factorial(0)))
    ) K  u, @( T& e- |8 v7 B$ v3 * (2 * (1 * 1)) = 6
    9 }4 X; O- e# N  P
    : H8 B7 a: i2 u5 U0 O* l 递归思维要点: l% i3 i8 I) o1 v$ e
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    " ?9 Z* {/ m' G" U2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    " R/ U$ v, ]* s. S* U3. **递推过程**:不断向下分解问题(递)
    7 Y/ h: @$ p% W: ?) w2 q- G4. **回溯过程**:组合子问题结果返回(归)* m8 `3 s2 ?0 V: J; J% S" q
    1 `0 t' h7 F/ ]8 z
    注意事项1 Q9 d" V0 A! X1 ?, t
    必须要有终止条件
    ) o8 q0 j9 Y, P: C递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    1 _+ A8 Y3 E5 }* n$ @1 g2 `某些问题用递归更直观(如树遍历),但效率可能不如迭代
      B, ~4 c" W$ L: C% |0 S尾递归优化可以提升效率(但Python不支持)
    * E3 A, {; l& c; D8 a
    7 j  n4 f6 V, m4 i, M: w+ F 递归 vs 迭代$ G  B& M2 N+ N) k( X# p
    |          | 递归                          | 迭代               |% B3 }- c* ?' c* i# \
    |----------|-----------------------------|------------------|
    1 c/ u& k' M; j. J( Z6 q# C| 实现方式    | 函数自调用                        | 循环结构            |* W9 e% N6 j  d
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |% t- ?4 q4 Q! A6 e% I& O2 |7 U* f
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |" L9 {- O) _! M! h9 T0 G
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |: d9 C, U( e& u) x# [% H

    6 U( U2 `- d& M 经典递归应用场景
    5 n/ Z" e! O# g1 H2 M( F; S9 l1. 文件系统遍历(目录树结构)4 Z$ z  u6 H0 d/ F* X  @" [
    2. 快速排序/归并排序算法
    ' u' @( [# w( ]7 N% M: u3. 汉诺塔问题
    & q; j  m& B0 r% ?7 q4. 二叉树遍历(前序/中序/后序): K$ f6 a: {  o' \1 v8 v7 c8 ~
    5. 生成所有可能的组合(回溯算法)
    ) z7 Y2 ]1 F) c* K! u3 }9 h2 S0 E" m  N# g6 I+ U% F
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    3 小时前
  • 签到天数: 3123 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    , K+ U; x+ u) O; C% }我推理机的核心算法应该是二叉树遍历的变种。8 E3 ^9 J9 o& W3 _, 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:5 ]9 H% I) I) F, g
    Key Idea of Recursion( O! e' u' w0 |; e. _  p

    8 _/ D) X% C# N$ ]" E# nA recursive function solves a problem by:' t  }8 D9 _3 T7 [8 [" Z$ e  x
    . |+ S, f$ k% O% F( X! y$ [
        Breaking the problem into smaller instances of the same problem.
    ; ^  M! S! P( k3 M4 r% I( ?# n% A9 c' ]7 f. p3 t8 P
        Solving the smallest instance directly (base case).- g: F6 @& L: k7 A' J

    & ^+ F( v+ Z6 Z: i/ b2 y: G    Combining the results of smaller instances to solve the larger problem.' Z$ [. i* E3 y$ x

    7 w* |, ~) @; |9 K( `3 H2 C. DComponents of a Recursive Function
    1 N8 u8 W8 n9 X: A9 r5 A( ~1 i9 C7 V9 R* t' z% I
        Base Case:! X7 ~; a4 \3 n0 h6 _- V8 A- v# ~  k

    - M5 d) U6 q7 W& c9 |        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    8 `4 r9 X/ F$ j- L: M1 O
    5 _) B" {9 G0 ^3 ^# k8 y        It acts as the stopping condition to prevent infinite recursion.& D6 f0 y! B5 p  H# B

    3 {" w- J2 i7 _3 O% t6 |- {        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.2 ~0 t. N  M7 g
    - T: r6 T: C; n- F2 U  Z1 S5 p
        Recursive Case:
    . H( I- \' G1 J6 B# p5 z! Y6 E7 M% ~; r1 m! ^: Z2 N, |$ A$ S
            This is where the function calls itself with a smaller or simpler version of the problem.- z% E; @- }) M$ Q7 {) ~3 }2 M/ k; e
    ' A0 E" D& c7 I: i
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    " A+ R  K2 }$ y) x$ M3 p. s/ W7 @' B/ u( c2 y' R- S* F2 D* C
    Example: Factorial Calculation2 W$ l) }6 H3 O

    9 ]" d9 j* [' U/ x) A! G( ~& HThe 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:
    7 x5 h0 m& A7 K5 q# ?" f8 J8 t( v6 u4 b" x- h% E! V2 l; J& J
        Base case: 0! = 1
    : R% P3 D7 E- n* ^0 O9 s* \2 S) x. U7 n3 i
        Recursive case: n! = n * (n-1)!6 ^; w9 `( |: K+ f' ^7 \' Q9 f
    ) H5 f9 d- P* F/ J1 P3 |- y6 e% K
    Here’s how it looks in code (Python):. A3 r. V! B  v+ a
    python
    - h0 O1 X0 ?0 r
    5 i! c4 ]9 ]* p2 h& I2 t7 g: {. \2 c4 f; C
    def factorial(n):4 m* N  E) b, m  n' @3 m& F1 C
        # Base case% w# e7 T; o+ o1 U4 L& \
        if n == 0:
    . d1 i0 z/ |$ Z4 R+ }        return 1
    ; ?9 l! c+ K* {" j( t9 z- D; `5 W2 k$ ~3 I    # Recursive case
    * ]5 I5 I' V( \9 N    else:! S& k; t" e! T
            return n * factorial(n - 1)
    ' T9 q# L9 H; v$ ~/ E
    7 x3 g8 m) a: O! ~, N) I1 x# Example usage5 F% e  N* ]- ~2 M
    print(factorial(5))  # Output: 120
    % c( a: \) }' k8 K
    # A5 L, C* X- EHow Recursion Works+ d* o3 I7 d/ }1 a; ]
    8 q! B$ x: _6 O" S2 C% d5 ?' K
        The function keeps calling itself with smaller inputs until it reaches the base case.* e& M7 y4 j# j* [: ?6 |) B
    8 }$ `( F! S8 {" ]1 Z. u# J, V8 r
        Once the base case is reached, the function starts returning values back up the call stack.3 A- m6 k- @+ F; P! x

    8 o) R. a' O8 Y+ G4 y    These returned values are combined to produce the final result.
    , m* N5 a$ E9 }4 t
    ! H; ?: w/ P6 J1 U/ O' d7 ?3 xFor factorial(5):
    ( F4 }$ `$ A9 E7 T& u
    : k# C& a; w5 U
    4 j( y6 x2 M( f" d+ v5 Tfactorial(5) = 5 * factorial(4)& L/ v) k: ^- Q7 W
    factorial(4) = 4 * factorial(3), v9 T6 b( z. u; X  z; L
    factorial(3) = 3 * factorial(2). ]6 q4 C8 {. W
    factorial(2) = 2 * factorial(1)/ o/ P$ ~; @/ t0 p
    factorial(1) = 1 * factorial(0)! j2 ^/ d3 x5 I8 Y; `
    factorial(0) = 1  # Base case0 t2 V5 ]% s# N* g% h
    4 {6 }; d  n, I5 m/ ]& e* O
    Then, the results are combined:
    - O, R, ~4 J2 c$ O# \* |9 B. u$ |# Z1 F8 T  o' N$ D

    6 N  X( O% T, v0 Pfactorial(1) = 1 * 1 = 1
    % m- N, Z" X/ t2 d6 i8 ~$ Mfactorial(2) = 2 * 1 = 2
    ( M7 D& V4 ^; E; y! ~6 a% dfactorial(3) = 3 * 2 = 60 [. Z0 S, d! p# W9 |1 j
    factorial(4) = 4 * 6 = 24
    / T- O+ G. K+ R8 O4 Vfactorial(5) = 5 * 24 = 120# J3 ]9 F& c0 I# C' e$ u% y8 p

    . |1 P7 b4 k/ R; V/ {1 TAdvantages of Recursion  g) G8 y' u! T, f
      D8 k: T3 _2 Q" A$ _2 |
        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).
    , C9 d7 i$ n% s- z
    : M0 n+ o; \* g! o/ [" o; \    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    " s$ d( C; M, t+ C9 Z* j9 n
    - k' G' @2 r/ `" }; mDisadvantages of Recursion$ L; [9 M& A4 Q  I0 N

    ' n+ C+ |# S. c8 C7 b  s( k$ M    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.1 d; `2 Y4 z3 s0 T5 {
    + w; S: o* y* g, {+ g
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).' z5 l. w2 ^' L9 [! c: @7 x  r

    ; h% W8 h! u! G" q: D" ZWhen to Use Recursion
    ) N: ?3 u/ }5 \# w/ w' l+ ]. W3 s/ R- i! T3 a$ S
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    9 e( r* a1 I! F8 |7 w' A& n' x+ h  \/ ~  F  a' M+ j6 z
        Problems with a clear base case and recursive case.7 v& Q7 @% G2 b& L

    4 k# q% }% u8 E1 }4 K3 |Example: Fibonacci Sequence3 o6 b: U3 ^$ B( j% h
    , k/ O+ ^: z6 L6 ^
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    9 @4 k" w) U) {/ x$ U
    + t9 w: K( K  @0 [/ Z) R9 z    Base case: fib(0) = 0, fib(1) = 1: L8 s5 V& V. Y# d: u# Z8 Z, K) W

    2 w% `0 H5 p0 s3 z) B5 m    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    / `- y2 d4 M. x$ z. Z2 {" E& o
    . @- b+ U& j6 upython
    2 b& J6 T2 B; n8 k; t" L
    4 J3 a" E5 K# @2 W( f0 i0 c4 G( u% y" k, E
    def fibonacci(n):
    , F0 i& r: H$ s. O4 {( @; i  X    # Base cases
    # P! r9 Y/ m/ `4 ?' ^! t$ [4 j" W    if n == 0:
    , c3 `5 Z6 V+ X9 u, D7 q% o! n: x7 i        return 0
    3 ^3 Q- s  @" E- c" j, X% h2 y    elif n == 1:
    ; O4 p, Q7 y9 K' K/ f- `' i0 I" h        return 1, u3 x+ `* ]/ n8 A4 Y) y: J
        # Recursive case  N% r( V$ `' |1 J4 Z
        else:
    9 q2 B! j: @* u; l# f        return fibonacci(n - 1) + fibonacci(n - 2), U. ~0 D6 _& X1 I
    : H. P  O5 S3 q5 `9 K3 G
    # Example usage
    8 d# L6 @- N) vprint(fibonacci(6))  # Output: 8
    # A. ^1 h" H& b/ @, \9 T8 y# y9 l& S1 b& b( r4 _
    Tail Recursion/ J3 {2 s4 ~# q' B

    # T( |; Z$ S, Z1 ~* kTail 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)./ x7 ]# j: U  w  Z

    / Y) n; B& z8 i- QIn 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-20 12:01 , Processed in 0.029693 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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