设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ' c  l5 T) T; q. S& l" a% k* p5 B& u4 N
    解释的不错
    0 |( M& P# V. @: l8 f* V4 E9 ]' F! c6 Q- X) L  Q
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。3 U0 w# A% H+ t5 c
    0 n1 k' c: H  _& ]4 z6 p0 `+ Z3 \; F
    关键要素
    - g: q( i# I* S2 ]7 B0 h; f1. **基线条件(Base Case)**7 l- i* \& h' `+ `+ A8 r9 {" w* `
       - 递归终止的条件,防止无限循环
    6 D4 s1 q$ p7 I) B; c$ }3 B2 ?   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    7 d- }/ m9 h5 R, m1 \/ a# I# S6 g
    2. **递归条件(Recursive Case)**3 h4 t" e' c: A8 ~
       - 将原问题分解为更小的子问题; {2 ?! N. }9 F8 p2 D
       - 例如:n! = n × (n-1)!6 {6 ~5 O! k$ C+ ~9 J1 a
    2 y! f0 i: _; P0 L- s
    经典示例:计算阶乘: D: }. A! Z( V4 ?" P- j  Y) i
    python, b( H7 Z1 D2 N. Z, E
    def factorial(n):) N; G, n" }5 p7 Q' O
        if n == 0:        # 基线条件
    - ^$ H1 f. s/ p1 }        return 1
    & v% R# w) @9 q* Q& z    else:             # 递归条件
    1 t6 |6 ?# C, q! w" S& K" n: A        return n * factorial(n-1)
    ; J  T' G$ H1 p4 k! n0 N执行过程(以计算 3! 为例):
    0 C' G- J) U2 e1 Wfactorial(3)* l0 r) V) m) k- P. l
    3 * factorial(2)
    9 [2 l4 N  a7 m, l7 Q3 * (2 * factorial(1))) X& P4 p3 M1 C
    3 * (2 * (1 * factorial(0)))  e) X# ], a4 j5 \# j2 E. ?
    3 * (2 * (1 * 1)) = 6/ J$ }% p+ ]) M  I5 J; `
    0 ^. {2 Q: t4 U" R" N
    递归思维要点
    ' p% Z' F: @8 }; h& R9 K1. **信任递归**:假设子问题已经解决,专注当前层逻辑0 U; n% _  s8 ]4 O& Y
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    $ p. A5 s$ k  l8 m2 J" c3. **递推过程**:不断向下分解问题(递)  Q" Q6 T& I% m! y+ ?! L# c
    4. **回溯过程**:组合子问题结果返回(归)7 o  X$ w" J& j# V

    ) p5 q& x: f& Y6 C8 M 注意事项
    ; }8 q3 O9 O- R* z4 D# W1 g必须要有终止条件( t, q9 e. |. d6 ^: t
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)* `5 U* Z# H) E# W5 k: T: ~4 S
    某些问题用递归更直观(如树遍历),但效率可能不如迭代, z7 R" g9 _( @  [- F* w. l
    尾递归优化可以提升效率(但Python不支持)
    9 o$ T( f) x  \& O4 l: S3 S) I. P% H" a
    递归 vs 迭代
    ! [5 s; c" `- k1 {3 Q/ Y|          | 递归                          | 迭代               |
    + H+ Q; \. E7 T$ N0 @' n2 Q|----------|-----------------------------|------------------|
    2 `" N! f$ I2 f6 z" T, S# V1 i| 实现方式    | 函数自调用                        | 循环结构            |
    1 i' k2 J3 i+ X| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    $ q/ Q7 C3 O* h: \8 u' I2 @* g| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ( |9 i# y' V$ _: G1 A+ F; `| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ) F, [* [& {3 u" x5 }0 b: E$ a/ `( |
    经典递归应用场景1 L6 l0 D* S0 J2 d
    1. 文件系统遍历(目录树结构)3 h" H* w9 M4 u4 W, G
    2. 快速排序/归并排序算法/ b! B# p6 L8 S% x
    3. 汉诺塔问题
    # Z: {7 s3 R$ l, B/ [/ n2 b' W  O1 j4. 二叉树遍历(前序/中序/后序)
    / ?5 [! C0 L# V/ W# f1 |4 \% p: C5. 生成所有可能的组合(回溯算法)
    6 n7 j+ D, Z: r
    $ X/ ]  H. K: w3 R$ [1 ^7 j( E试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    昨天 07:13
  • 签到天数: 3231 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    1 T/ G) x) `% C2 G2 A6 d; j我推理机的核心算法应该是二叉树遍历的变种。
    0 L$ m3 t: |% T0 A5 w另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:, H% A9 E8 o5 V, m4 d+ r
    Key Idea of Recursion
    * f1 g7 I/ l% ]- R; `! Q1 m. J! s6 a, A/ }  v* W/ i: G3 \
    A recursive function solves a problem by:2 k9 [; w6 r4 V% T+ v& j' ?
    : @5 h' L! K* d9 I
        Breaking the problem into smaller instances of the same problem.+ n) W9 B* K* K- u8 O& M

    5 S( k: Y  Y% y: s2 n    Solving the smallest instance directly (base case).8 `& U( T  c( r% C) s0 e' m

      Z0 Q/ b1 V- e    Combining the results of smaller instances to solve the larger problem.! k% i" b" w+ L, z
    ( j% z0 i5 b. F$ o
    Components of a Recursive Function3 }  m8 C0 \5 x6 k7 M# \
    ; m; Q: S9 {6 h% {, T8 a
        Base Case:
    . i' R: e( j1 h5 Y9 |% l, V& m( J2 [+ P
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    5 _1 s8 w' E. s1 Q
    7 V. M  L) G, @" I6 Y- S" x        It acts as the stopping condition to prevent infinite recursion.
    / O+ g/ L3 K2 r, x" F2 G  R  j
    / A; ?* F, Z) x/ ^* I        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    6 f) h# E/ Q$ A& y# \+ e3 Y  j+ R. D) ?2 P3 {% S$ Y& N
        Recursive Case:
    " q$ O8 _4 f! _' ~  }
    : L) ?6 z, Z' ?; Q! u" G7 L        This is where the function calls itself with a smaller or simpler version of the problem.3 q) z7 o7 G  \
    " ?* _7 ]5 m1 ]0 J+ }: Z4 _
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ' t( c7 D6 ?9 N
    1 N( d: l+ `. `+ @- F7 T2 nExample: Factorial Calculation
    0 T! }6 c: L9 b! v5 \# Z( l9 Q: M0 |2 D
    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:4 L% z" G, D9 `+ n' _" r
    3 R0 {7 e6 S2 \2 G! v3 a* B, Q
        Base case: 0! = 1
    8 s: B0 P1 I! U! d! U; T( `9 v$ `9 u0 `+ l0 ?" i2 U
        Recursive case: n! = n * (n-1)!
    # r+ @# _# ~5 l3 _8 N
    % C0 j+ t1 t8 UHere’s how it looks in code (Python):9 c- I) v: t! P. m
    python* x+ i( [9 B/ o( n

    ' ^1 u4 y9 D, s0 M# {, l9 R* x3 P. u! x* C# P7 H  M% z
    def factorial(n):
    3 t+ C  y. m. z5 p" U" R8 z; ]    # Base case
    + a9 E' D/ X' Q6 D. ~* T    if n == 0:0 v( h" S, m- V* |+ Z4 [& ^7 {& q
            return 1
    # h1 {# R% T& ~* {. r, O5 y; o    # Recursive case) y. {$ k4 X" _0 _7 O; J
        else:
    6 R( V( I$ [' I        return n * factorial(n - 1)
    " j! s0 R( a. C2 R% p/ A7 V5 l' e8 I% A. [9 V1 m
    # Example usage
    " X6 n5 H' A% W4 q9 Xprint(factorial(5))  # Output: 120
    : Y# V- y2 Y2 o8 g% X! d4 F5 z( c* _5 |
    How Recursion Works9 f9 n. h+ V' t! b- s0 z

    6 [5 U# Q- d. [3 a' _2 j" i: u7 i    The function keeps calling itself with smaller inputs until it reaches the base case.7 U& V' o# B) t% f5 V
    ( |# [+ E. [8 G, R) t  k% c
        Once the base case is reached, the function starts returning values back up the call stack.3 Y. P3 Y% ?& r4 v7 [7 `# O0 w
    3 {; r* `- g: r. E) S
        These returned values are combined to produce the final result.
    0 s& z' |! c+ v+ ?! l- x4 V2 h5 ]' |1 Z
    For factorial(5):
    , u1 ?1 d  `! W
    * R4 ]$ v5 i: z5 p, `" I# U# |
    - g3 m( {$ G8 L( Vfactorial(5) = 5 * factorial(4)
    - b. J3 q9 H2 ]factorial(4) = 4 * factorial(3)  a! T' ]5 y8 Z7 a
    factorial(3) = 3 * factorial(2)7 I; X3 e1 c% F7 p6 P
    factorial(2) = 2 * factorial(1)0 V* H/ ~: D% Z7 F5 D" u0 _
    factorial(1) = 1 * factorial(0)( T  c' ~2 l, |
    factorial(0) = 1  # Base case
    ' c" k! l1 i; z2 U' H7 g, n3 S. {- o/ O" z- x. M3 {" K
    Then, the results are combined:. X$ b0 _: q) J
    3 n9 s6 y3 P3 X
    ) V8 }6 t$ W0 G( E
    factorial(1) = 1 * 1 = 1
    $ @6 s+ \; @) ?5 S! d6 Pfactorial(2) = 2 * 1 = 2
    5 `( ~4 q$ }: W! Y% b, afactorial(3) = 3 * 2 = 6
    1 m7 s& }/ c$ a. k" A! H/ F( bfactorial(4) = 4 * 6 = 24( L9 X  {; W1 f( c$ o0 \
    factorial(5) = 5 * 24 = 120
    ) y, S* h6 E  S( F$ e- W5 _" Y% X. \9 W/ G' f6 j2 l/ s
    Advantages of Recursion
    * p4 `4 c( l  B( G2 K  O
    * ?% c6 n8 ?( a  ~  \    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).) j% F; ~$ o( S# Z1 b& V. E8 L% }

    / g) @- T0 U( s7 ]  M* \5 N    Readability: Recursive code can be more readable and concise compared to iterative solutions.; P7 @1 p$ \( F$ E' i" }

    * Z: n* K. D9 Z/ j2 x' ODisadvantages of Recursion
    / j$ m  Y2 i6 X# ?: Z2 m. |) x& U8 M+ q4 w
        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.2 `8 p2 N. A& J8 ~1 E
    + c& v- ~) C' `6 h- l
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    . \& N$ ^( Z1 J! |/ A, J
    : J7 r# i5 B7 A4 OWhen to Use Recursion5 d2 d0 ?; R8 L  C+ m
    9 L- O/ G2 v3 Z; s
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    4 o2 w: p& A4 O4 G% ]
    - l2 e' G" ?0 o: H3 Q, u    Problems with a clear base case and recursive case.
    7 }3 F9 l1 X; U; {& U) ?
    5 l3 Z4 Z8 `5 B* R: t& W% J& FExample: Fibonacci Sequence7 ^" f" D; P; J0 l# l, Y4 [
    . ]# S1 P2 g- B3 S7 u' `. f1 P6 Z
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    + V! w' Q* {+ |9 `* Q3 U+ C
    ! L/ V, _/ q' \! g+ q7 F9 U5 d    Base case: fib(0) = 0, fib(1) = 1- g* P4 m2 w9 C9 T% A5 M
    7 @2 T- p) G5 X1 X
        Recursive case: fib(n) = fib(n-1) + fib(n-2)% }9 w9 M5 G0 w3 h

    . D% e1 |. w1 J8 Dpython$ \: N3 [, g+ T( [
    8 a2 v4 D8 A8 d. @2 L5 D
    ( x7 d7 i# Y: T( D! _) J" P
    def fibonacci(n):
    0 @' B1 B1 X. Q0 r% A" e    # Base cases) r) q/ B5 k3 P, ^" t) K0 T
        if n == 0:0 G/ Y% z; \& X& o
            return 0
    5 J' a4 U! ]! t" {( d    elif n == 1:3 Q$ E/ n; S& H. W6 z6 b
            return 10 F* Y9 m0 y8 p4 ~" g1 [3 e, @% {
        # Recursive case- W: M+ U. P; h( v; l) C& L+ T: H
        else:
    5 q1 C3 I, A, }. b4 _1 G1 i) S        return fibonacci(n - 1) + fibonacci(n - 2)( p9 Z( |* E  F& i

    : A! x' Z% O/ J: S1 {0 |# Example usage
    - q# I1 j! V* z/ e0 c  zprint(fibonacci(6))  # Output: 8) ]4 d6 [; @5 d0 b/ V& _

    ! D# `3 n: }" M3 ZTail Recursion0 s: U8 H! A0 |, Y
    # `5 n- i! @  W- Y
    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).
    ( }7 g2 p6 Z8 X* M6 E( Z0 Z# ~" O7 b9 m/ X! D( @7 ?2 o
    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-5 05:35 , Processed in 0.060350 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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