设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    3 I* N6 m9 ~2 ~6 t, m4 J9 x5 }! r, J1 B* {" }* `5 z1 G9 N, u  D
    解释的不错; d6 a+ R0 e8 S3 R# W- q

    6 I- I) Z6 \" f递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。9 c7 u+ T- P7 Z# g, o3 t0 T

    ' ]6 s9 Z( L! j% f 关键要素6 d6 V9 c; B! }$ X
    1. **基线条件(Base Case)**
    $ S: [/ L2 ]6 M9 G% m6 O% Q7 a   - 递归终止的条件,防止无限循环
    * s( E  T- J5 z# A   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    7 P, O7 x  P& M( D* B! t" r
    8 A& y5 S! Y4 l* u+ T8 O% R2. **递归条件(Recursive Case)**" n" q$ S4 l5 l/ W* A. D4 U
       - 将原问题分解为更小的子问题
    ( _' k7 M+ A: R" R' S  r% K   - 例如:n! = n × (n-1)!
    0 Q0 C+ w3 e! G1 W- F- f  a* i! n3 V. f) @1 j, b  R! m
    经典示例:计算阶乘7 J; l% Y6 {- o, p  r% C
    python: A6 \$ ^) w, x8 z" a. q, F
    def factorial(n):" K+ a% L. k2 w8 G8 {6 e7 Q  c
        if n == 0:        # 基线条件. f2 l$ i: N5 g- F: N) t
            return 1
    9 F' u" M9 \, T2 z- ?8 U! J3 S# B    else:             # 递归条件; Q/ }' u/ ]' [/ n( v8 p
            return n * factorial(n-1)
    ; d" h$ h( Y4 h  d9 j/ |执行过程(以计算 3! 为例):
    ! f& u1 U% F- ~( h+ q% n2 b/ nfactorial(3)
    & b+ v+ f1 d( f3 * factorial(2)
    ( l  c5 F! B- T/ p! T% D# H4 F3 * (2 * factorial(1))6 D2 r* ]& z) G+ R
    3 * (2 * (1 * factorial(0)))
    . z* X* _4 B, i3 ~3 A3 * (2 * (1 * 1)) = 64 I4 B8 [. X$ S& e5 q2 o: @0 e
    : p. y/ s& X! ?+ e* `$ u$ ]) d
    递归思维要点
    ! V/ K+ z: u4 V( g/ p1. **信任递归**:假设子问题已经解决,专注当前层逻辑0 A& B: S: u  q% q1 a: x0 M2 a
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)6 `- x+ T# L2 q+ b
    3. **递推过程**:不断向下分解问题(递)& g3 ?& u8 [& E4 j4 f5 e9 Y
    4. **回溯过程**:组合子问题结果返回(归)
    1 k4 N6 Q1 x- \/ w9 C
    : j5 Z' A  B) U: ] 注意事项) T1 N$ T& d# H# p1 m" g6 d9 O
    必须要有终止条件
    , M( j. ]: W8 f6 |% n. u2 R递归深度过大可能导致栈溢出(Python默认递归深度约1000层)9 W& Z% `- P- M& ^. Z
    某些问题用递归更直观(如树遍历),但效率可能不如迭代4 B- i) t9 a. s* P- R& ~0 }- V
    尾递归优化可以提升效率(但Python不支持)! {1 O  y; c* @7 I

    6 F6 A$ L; N  |0 B 递归 vs 迭代
    6 V: ~" p/ J7 q5 M6 ~|          | 递归                          | 迭代               |
    ; i% m1 g$ u( _% ^|----------|-----------------------------|------------------|
    , S7 ~& P7 w: T. R| 实现方式    | 函数自调用                        | 循环结构            |
    " z  ^4 H8 _5 ?% W3 D2 P: A& N' z| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    $ K+ n' R4 B2 S| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    / G; M# b3 j: F# w$ T| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    7 _2 Y& q$ w5 H2 f" e) a7 C- W; m; h
    经典递归应用场景- A4 C8 V) u$ s$ f- z. y2 c
    1. 文件系统遍历(目录树结构)  z- ~8 g8 Y. h- J5 r2 }
    2. 快速排序/归并排序算法: d5 g- V7 g$ _4 |5 N
    3. 汉诺塔问题
    " Q, u8 u9 |/ N$ {7 }4. 二叉树遍历(前序/中序/后序)) S3 T6 Z3 P% g% t4 Q/ d
    5. 生成所有可能的组合(回溯算法)& l' d5 e  M) j! V

    / F9 b) K- K* A试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    16 小时前
  • 签到天数: 3164 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ) T! a6 C) i$ `2 T% Y我推理机的核心算法应该是二叉树遍历的变种。- O8 [: O4 G2 J7 I# P
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:3 ]+ U5 k: [9 T* X# z" w+ r, Z; |: B
    Key Idea of Recursion7 y: U+ x) ?* d5 T9 J

    / [$ B8 ~3 q. w8 ^; s  IA recursive function solves a problem by:3 O; R+ C  k9 I' s' Q1 l
    & @; U9 V: i& W: O
        Breaking the problem into smaller instances of the same problem.; i9 h6 W; F! u/ [
      S: e1 r# H+ p9 o* }9 j
        Solving the smallest instance directly (base case).3 [) [6 F7 F) q* r3 o3 U. H
    6 p# }1 c2 |: b9 B5 z
        Combining the results of smaller instances to solve the larger problem.% H) b. }7 `/ B
    / V7 A& D8 [- E1 x; h
    Components of a Recursive Function* R/ x) Y1 ?' r, {, r5 R7 B

    3 ~3 v  ~% p3 z2 n3 l: _    Base Case:
    5 {( k1 K% _( W2 a
    ! H4 I, X, C( w# p        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.1 U2 c8 {: u- o# e

    3 s6 y3 y6 a( s3 b; B        It acts as the stopping condition to prevent infinite recursion.
    ; F7 Z; q6 U6 C- ?* H" _& C9 U& ^1 w4 o$ E. h8 _
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.# a' I  Q; P: J/ x0 a8 [! [* |- [, [
    3 B0 \/ Y; z/ w/ h) [
        Recursive Case:
    - K5 D' v& ~, h1 p9 L( ^7 o* u" u, j" X  ~9 T: }# B/ I
            This is where the function calls itself with a smaller or simpler version of the problem.
    5 R' r! N! n4 ]* Q2 e/ J0 A" i
    4 k+ p5 Y6 w- c% H' F        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)., A: f  J* f+ o" r/ Q, ~
    * m% s2 d+ P" C
    Example: Factorial Calculation
    8 b3 b( }; ^) G8 r- x7 S9 z# \  Q6 L  L' W) K
    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:; i6 E3 B4 ]  r9 H4 }

    5 M8 Z& ?$ C' S( D+ @% i) j    Base case: 0! = 1/ S2 Y+ D. W/ [6 ~( P

      z9 {- Z8 k0 L5 I4 C    Recursive case: n! = n * (n-1)!& Q6 [/ e/ u, w. o
    % y& f) U5 r- \" T+ u# J
    Here’s how it looks in code (Python):
    4 J% T. H) x! f- d" h% Epython& F, d- [( ?4 {$ f+ }6 D
    * u. [2 k& ?# K& o& m" U
    $ b9 _2 s5 P; ^6 w% P0 r/ G' D5 {
    def factorial(n):1 Y# c( |9 d1 f
        # Base case
    , G/ s) o4 O0 U0 V- ~$ v$ H- f. m8 `    if n == 0:
    $ c* }5 ^# `) N. j: {5 Y; m        return 1
    4 P0 ?- Z# q+ B! Q7 Q8 ?    # Recursive case7 i* _0 L, q, o9 b4 P+ D4 }
        else:! D7 ^1 G* Z. K8 V. v' q
            return n * factorial(n - 1)
    9 o: [( v( f: }$ a( a2 |, z
    " N: M6 ~( j. J/ i, V$ k: o9 A# Example usage! W& a$ ^) Y$ X$ H9 T1 V) z
    print(factorial(5))  # Output: 120. y  j/ A9 G0 X% _7 z) I6 ^- I
    4 }' S8 b& M+ s4 o7 h- b
    How Recursion Works
    , n2 S/ Z" z; _$ {5 f
    4 k& r  X# _) X$ z( j; X; B    The function keeps calling itself with smaller inputs until it reaches the base case.
    0 N: I0 E% D; h( I% \' ]! S- P' M. V& N4 F5 x& ^* m( y$ G
        Once the base case is reached, the function starts returning values back up the call stack.
    - d3 ?! z! v* v/ B* [: O8 \3 m5 ~) O
    8 t, D: E- ~. U( G( ~0 u( u    These returned values are combined to produce the final result.* ]! u0 b" a9 o4 [  \

    & z5 P4 V$ f" b& D6 n7 EFor factorial(5):! ]7 B! C& [3 i
    % f0 H5 ]5 w) ?* M* D  S

    7 j2 S# y: }& \& D  X5 S' efactorial(5) = 5 * factorial(4)' {+ L% G; g  k) o3 N/ u- f
    factorial(4) = 4 * factorial(3)
    ' p$ u2 X* _" w/ C, Ffactorial(3) = 3 * factorial(2)
    4 o, P5 y+ l2 h! p4 Kfactorial(2) = 2 * factorial(1)
      w, A' V2 B! V; F& }3 H8 g+ Sfactorial(1) = 1 * factorial(0)
      X2 p; u# c1 V0 C6 E' Qfactorial(0) = 1  # Base case
    # i, }5 y7 @7 m, B" B
    6 t1 J0 X& D3 N) e9 bThen, the results are combined:% u- q9 j- k1 B# h

    , q- x+ F% _& w; l$ ^7 M* @( U( p- |- O  S4 n  b& h# ]: u9 U
    factorial(1) = 1 * 1 = 1  W; }6 Y# E+ G
    factorial(2) = 2 * 1 = 2. O6 I) T1 p( ~/ M' N
    factorial(3) = 3 * 2 = 6
    : N( W3 X7 M* lfactorial(4) = 4 * 6 = 24
    % h$ S9 O" i2 \8 V7 sfactorial(5) = 5 * 24 = 120) c: U9 P* q8 ~, M  A9 U

    + C5 u+ C6 h" M& p. q# d, v$ bAdvantages of Recursion: I( ^4 v/ d' C, y

    ! v8 @, B3 }8 K4 U- p# \9 [    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).
    8 z. R5 m# V& R
    ' [/ ?* A: P  t/ P    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    2 h. e, g6 g7 Q1 ^( F, Q( [6 Q9 V: B7 K; d8 H" [7 E- Q$ @- {+ m" ~& c
    Disadvantages of Recursion
    ( O- q5 a- F3 m; q- E6 Y3 K9 w! z6 X  O0 F2 k7 c4 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.
    8 V. S: }; w0 {  o
    ( O. r, c, k/ n    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).! y& G- W4 y* Z( \3 h5 R

    6 \# G! c% \8 ~" O: Q" ^When to Use Recursion4 R) C8 d5 w$ X. A: U* \

    - ?) g+ M' }, ^" u4 F3 n    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).: w  ^% _  Z' \, b: t5 L& a
    / t' P9 p; B$ Q' n( {$ m
        Problems with a clear base case and recursive case.' }* O, U/ u( `  g, ^: i

    # ~5 _5 I. N3 k- R! C3 JExample: Fibonacci Sequence
    - M6 N0 {( c; w3 @6 j3 ?' Q6 M* X1 @& s: l: O8 j8 c
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:+ i0 {% X- m( Y

    # A7 m7 d2 |2 D% D" Y    Base case: fib(0) = 0, fib(1) = 15 J8 }& Z3 i, |# l. t  i

    & p+ P* A( c9 ?    Recursive case: fib(n) = fib(n-1) + fib(n-2)6 G& }4 \: L" J. x# q- u
    4 A( G$ m$ \& n$ p2 z
    python  `6 ~- W4 t: k

    ; l) b* y+ _! W) b& h9 X  O; M9 V6 z. |+ p1 I
    def fibonacci(n):
    4 i$ ~1 @/ ^; v    # Base cases7 m7 M' \/ b8 a* o
        if n == 0:
    $ d) b9 `! l) A- U8 \7 M  B        return 0
    8 E7 O9 |2 b$ k3 m5 z% ?$ N    elif n == 1:: @) W8 N8 ?# b# `" {% `
            return 1
    , A; x# I* I3 e# x% G1 L6 g% d    # Recursive case
    0 y- ?$ o( X5 b" a    else:' N* J& Z" J7 \: w1 |! g  W
            return fibonacci(n - 1) + fibonacci(n - 2)
    # _5 |% q  o& ]- R
    1 i: h* N& t/ R& Y/ n: G3 r7 f% \# Example usage  n" b! g( j2 [9 x5 ^0 z+ O4 U$ w
    print(fibonacci(6))  # Output: 8
    - s: a. h& W0 z- a& R
    " _+ W0 r2 ?4 z: R4 L( r# A5 H# iTail Recursion
    3 u) f1 `& p. e& z+ v9 Z) D6 r5 q- Z" V/ F. h6 |+ n& l
    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 P- d* s+ T- x7 t9 O% j
    ) W6 r: J" u% z+ H0 _2 CIn 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-2-5 22:49 , Processed in 0.054259 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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