设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑   q  z( `% E, t$ f" [

    ! F- P8 F6 o4 f! ~解释的不错
    3 X% s" k. Q, Y0 q) C3 W) i! S$ c6 D; g) p4 Q6 y* _
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。- |0 s4 e% t$ G/ ~1 P2 }
    ; j( g& H0 ?2 M' Z( b! R  H
    关键要素
    & B2 [# H8 w3 O' K4 h$ Y1. **基线条件(Base Case)*** ~$ w) Q- H( p* ^* L5 e$ `' |
       - 递归终止的条件,防止无限循环0 O6 K/ g1 ^) [" ?4 ~$ }
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ' n! K; H8 f: s4 D! e
    5 R9 f  S1 b8 W# U' t9 _% q2. **递归条件(Recursive Case)**
    8 w0 M: F. g- c5 J( t6 D   - 将原问题分解为更小的子问题
    9 D/ D7 q$ J( ~& h- q, K4 i6 }0 v* B; o   - 例如:n! = n × (n-1)!0 O1 c2 r4 Y$ W. n. O; _' z- H
    , r% ?9 m: }5 F5 y) A
    经典示例:计算阶乘
    5 f2 G' G* l$ ?# ^python
    # O+ [, o- Z% P2 a& Z& \def factorial(n):
    ' `7 G! J; r) C# n' }0 U    if n == 0:        # 基线条件
    $ g3 [1 ~7 I# i  C        return 1; `! S8 s# X2 L) A
        else:             # 递归条件; L. s* g9 `% H9 r$ W
            return n * factorial(n-1)
    4 f0 P6 c/ B* J执行过程(以计算 3! 为例):0 `6 i9 X. K1 h+ b
    factorial(3)
    " `% Q- p" \# U& L3 S1 j3 * factorial(2)
    + X' M' w% `0 t5 j3 * (2 * factorial(1))" S. k! p; l; h; |  f$ R9 e
    3 * (2 * (1 * factorial(0))). z' a# v2 l! u' t1 a' r/ D) z' m7 H
    3 * (2 * (1 * 1)) = 64 O& r, C' W  t0 \6 d8 }8 x

    2 O8 ?' V3 k$ T. C& f 递归思维要点' _( n% V9 d: K/ B' ?' I
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    4 V: `, ]9 E% w( e, a2 Q' Q+ P2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ! n7 u5 d! |% Q! c. L% {; M( c3. **递推过程**:不断向下分解问题(递)& e7 C6 Y- s+ X+ \; L8 i* Q
    4. **回溯过程**:组合子问题结果返回(归)7 T5 {  S5 |& A2 X, P" I, @
    6 V0 h' \. C0 P* _8 W! Z6 Q
    注意事项5 E& m" O( \5 d% S* |
    必须要有终止条件
      Y, b' e) T5 A5 r0 s递归深度过大可能导致栈溢出(Python默认递归深度约1000层)1 d+ G& d) i, h4 Q
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    9 @4 o$ |. S+ P3 X9 u9 }尾递归优化可以提升效率(但Python不支持)
      I: L! q% d5 I' S
    : z  S1 D" v  f+ g 递归 vs 迭代
    ; N7 \2 K+ |! S2 {. A|          | 递归                          | 迭代               |
    0 G2 E. J) t8 H' C/ y8 `/ W" X|----------|-----------------------------|------------------|. k6 q; T/ @: U5 @
    | 实现方式    | 函数自调用                        | 循环结构            |
    + ~8 @# z! t/ o% A, x  Z) Q| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    2 L, |& f9 I( K  _' X| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    8 \2 ~1 T9 n5 o) N( E| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ; x+ {- m$ ?$ O) {- D" }6 _; d. X1 _, D% D( ~0 h7 G& u' Z" C+ f" a
    经典递归应用场景
    * }$ \! {3 x$ X4 Q: c$ u! X1. 文件系统遍历(目录树结构)
    6 M1 I+ ?2 ?3 B: V2. 快速排序/归并排序算法7 d2 r( S( d" R5 H
    3. 汉诺塔问题
    ' X- P* W* R5 r, m( s! u4. 二叉树遍历(前序/中序/后序); q, c8 u2 {2 X8 R& e
    5. 生成所有可能的组合(回溯算法)9 @0 A7 Q3 P( L, ^( d- b
    ' Y) d3 N3 e- ?9 o
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    慵懒
    7 小时前
  • 签到天数: 3225 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,% J7 r, W+ V" n" l
    我推理机的核心算法应该是二叉树遍历的变种。2 a5 u3 z1 F3 f, y+ B4 m
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:; u; O6 b8 W# t2 s
    Key Idea of Recursion
    * ]! B; J# }+ W' m$ c$ K
    ! Q/ }( \( `: Z1 s6 O  q, Y9 m+ ZA recursive function solves a problem by:& b9 g7 |6 v, {8 P. _

    3 ^. s+ _7 y4 }9 d8 A$ S    Breaking the problem into smaller instances of the same problem.
    # a1 t0 r6 {/ r4 u# v: c* n6 o6 `
    8 [/ S' E% x. Z7 M4 R$ T: J    Solving the smallest instance directly (base case).: e# J7 E3 \% I5 J2 x

    # u! a2 f; y& O; X' D    Combining the results of smaller instances to solve the larger problem.9 s( s5 c- F5 I6 |
    $ ^, {' X$ @1 c
    Components of a Recursive Function( ?& d/ D; ?  `
    ' g0 N8 g4 _+ U8 r( ^. {! ^7 p
        Base Case:
    * [9 X2 T( Q$ I9 j6 c! r- j' ]0 O' f* I- R, r) {7 H' H1 X
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.% {& e4 Z: T, ?9 y

    8 {( {1 q# f% S  k5 z        It acts as the stopping condition to prevent infinite recursion.( b, O( X3 Q1 N
    ; j/ P/ B# n1 x5 w3 ?
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.( I4 c; C* Z- D5 Y9 I' {

    9 i7 h2 y+ s" E9 ?) V& T) s5 D    Recursive Case:
    % M2 ?- \2 g; x5 G7 F3 c
    3 D3 y. D% Z- M# `$ R, p        This is where the function calls itself with a smaller or simpler version of the problem.1 p9 P6 ~; Q- S1 D

    + t3 e8 |' {1 s/ _        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).: s: A4 l; z( s
      j: }. M" N: d( Z$ m# Q4 i, m! @0 [
    Example: Factorial Calculation
    , q$ h0 k7 P  H# Q" _
    % A$ U9 S, ^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:, f9 v4 ~$ E) x$ a

    , }/ a4 F! H- I8 X3 J    Base case: 0! = 1# u8 B" Q) I& C! {% k

    $ L# Y2 c; D* `% R    Recursive case: n! = n * (n-1)!* k& w$ q6 x9 W" l! [0 T: i+ R
    : J+ g/ _/ k8 G5 |" a* O7 @
    Here’s how it looks in code (Python):
    7 o. V. w4 h( q. Bpython  K6 o. f" ?9 }% A' y

    9 ~- ]  F7 y5 `$ ]; l4 t, x) I  J- L( p2 n! r
    def factorial(n):
    2 I5 o8 Z/ Y9 f    # Base case0 T: J1 C3 R6 [, v
        if n == 0:
    5 }7 }( ~8 Z. Y5 y- n7 c        return 1/ b6 e/ E+ }- x& T6 }1 D5 I
        # Recursive case5 E  ]' h, J4 S( C1 M7 R' A1 Z
        else:/ `' X/ _9 X6 Z: `- M
            return n * factorial(n - 1): s' C+ ^) {# v2 p( ]

    6 n4 g3 d( z! `8 E* j, m# Example usage
    0 r4 A+ ]/ Y5 N' {; vprint(factorial(5))  # Output: 120
    7 g% g1 i3 Z6 ~2 H! g
    0 i# q. s6 h' uHow Recursion Works
      J: t  G! e7 }
      K: S6 ~7 J; _4 m+ i    The function keeps calling itself with smaller inputs until it reaches the base case.. R% t% I. S  j$ Y. R6 C

    5 V* C! b6 C8 s0 d* g    Once the base case is reached, the function starts returning values back up the call stack., V) m! }3 y/ y2 R. m

    + r' ]7 b4 O9 U- E7 o    These returned values are combined to produce the final result.1 t# i. b: [3 c' r5 y
    % K; o+ _, b3 w/ s4 x& O# ^
    For factorial(5):
    1 ?/ g2 ]  g+ D3 @6 [! o1 {3 V
    2 g% D2 H, U* V" A. j
    9 g' G6 Q3 F3 u( ofactorial(5) = 5 * factorial(4)% l% i  t5 U/ F4 {! G& f
    factorial(4) = 4 * factorial(3)
    4 n" P/ y. f9 K4 F5 Nfactorial(3) = 3 * factorial(2)
    ; x' w8 i, X5 t# o0 ~factorial(2) = 2 * factorial(1)$ j0 _! F* P6 [6 a: C( ]
    factorial(1) = 1 * factorial(0)
    1 G, T$ w' `5 N+ g/ U% Bfactorial(0) = 1  # Base case% _0 j7 @% d2 u8 t7 L1 J

    4 s; n& r) y& UThen, the results are combined:* i$ ~5 o* b( I* a
    9 H  H$ Z* m% d4 B$ |4 F: {# O
    8 k+ H2 r. ^* E( b
    factorial(1) = 1 * 1 = 13 [3 @8 M4 D4 Q6 A
    factorial(2) = 2 * 1 = 2) K6 P% i+ ?" J4 x3 K( P7 R
    factorial(3) = 3 * 2 = 6
    $ z0 v' t' q2 k: L! A: Bfactorial(4) = 4 * 6 = 24
    6 a/ g: t- L3 q9 a1 s- ]factorial(5) = 5 * 24 = 1207 `0 L( n+ Y# H9 B
    7 V! N. z. c, A  G; {; g
    Advantages of Recursion
    " D% U. _$ t8 ?& a$ t; C# L
    : V) H- v) o9 y& o. a2 p, ?! G) 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).7 n2 \6 ~' B# q

    , u, W6 Z5 g; O( c& N# P2 ]    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    - w- o7 o& z- Q. Y2 d
    ! n7 e/ @2 f4 bDisadvantages of Recursion
    ( p% X% U8 F; B! ?! |& ~& }3 F$ p$ B6 S
        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.4 }" E& }0 p) d; [; L. ?

    $ B: N# J7 r7 s, I* z* ^& b, N    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).7 u8 B5 W7 X: c, V/ |
    $ d( m7 R0 n' M  `% u
    When to Use Recursion2 \8 `3 z$ R6 w! `* w

    6 ^" ?- n5 X: g    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).2 }+ |: [$ j! C% E6 k7 c1 q

    . c4 p+ N: b. C) n2 m8 b' A; ^( \    Problems with a clear base case and recursive case." Q( M/ ?7 O3 C! o1 W3 \7 N

    3 l- w2 M! k# ^3 h( Q+ YExample: Fibonacci Sequence' j3 `" Z* K3 c  D" A8 U! ]8 x
    + w) _+ Z6 ]7 j; v) G7 v
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:; `% h7 S$ s5 l, h/ v, x

    . E0 O8 I) n6 Q; ?- q    Base case: fib(0) = 0, fib(1) = 1
    - {- x: Y/ \& `& x3 C/ r1 E
    . V: z1 A5 Y2 ]$ K    Recursive case: fib(n) = fib(n-1) + fib(n-2)5 B; I# \' z  n3 E
    3 P  e9 p9 c  ^) g
    python4 b* C; ^# o0 o4 B2 \

    * w  T% m* }" o9 S5 \. w5 L$ o2 z, j2 J( {6 f! l" I# o
    def fibonacci(n):7 P; ~8 M( x: G. d
        # Base cases
    % P7 U5 n0 C6 v- m, Q. o. l! [    if n == 0:
    4 n# d. f9 `& v/ ^1 I        return 0
    5 A( ]( ?. `- [% c( p    elif n == 1:: Y- B' P! V9 J) N8 N
            return 1
    , X4 v: |4 B3 `6 `    # Recursive case
    ! S: H) _- c2 x  R9 S    else:
    + i5 K) C8 a* E# `% E) T        return fibonacci(n - 1) + fibonacci(n - 2)$ Z# S0 f3 y5 T4 Q$ p# [2 t6 c

    ' T; \0 I9 i% Z# Example usage
    0 N; r1 Y" l  kprint(fibonacci(6))  # Output: 8
      C* L& W# R0 D( h9 Y
    8 z( i0 q3 ?, B/ Z+ M2 z( \Tail Recursion5 P( q: k  s0 ]) A

    # g1 C, |1 c  rTail 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).
    . M; C0 v8 Y, l' p: g6 `' n
    ' v2 G4 x% S6 T# j& l$ [0 j/ EIn 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-4-28 17:14 , Processed in 0.070764 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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