设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ' k# G& i$ I9 M4 i5 A

    ( X7 ^1 K. O# `3 Q解释的不错( t* j; J2 h4 {

    ! z, i3 t  Q3 [  e- o. Y6 S3 u) l递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。/ |  a/ t0 J- {( _9 d+ A

      Q8 T1 w( |' S" ?# Y+ d% E* I 关键要素) i" p/ T) g1 E) J" m( `
    1. **基线条件(Base Case)**9 j. Z+ t  t$ v- n# m. f: c
       - 递归终止的条件,防止无限循环: F  q* ^$ G, \. e0 i7 K
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 17 j4 K6 M3 u- _$ t! B7 D

    # F- I$ t1 N3 F- C2. **递归条件(Recursive Case)**/ d9 t4 l8 o$ S% u
       - 将原问题分解为更小的子问题
    8 b+ p  `( \1 e1 |5 E/ m) A: X2 Q   - 例如:n! = n × (n-1)!2 i/ f6 y' f; J) X' A
    4 V& a( s! Y! {: ~4 p$ T6 {
    经典示例:计算阶乘9 m( J$ c* s. D2 Y
    python
    $ w! J* b. B! i! c  \. |def factorial(n):% b. Y! M0 v* Q, r. j% p+ B
        if n == 0:        # 基线条件
    1 J6 {6 U) r1 M/ z        return 17 f7 [. a- w- V3 |  h) N! o5 P. B
        else:             # 递归条件3 h7 G/ |! g- F& _! w! ]% e' U
            return n * factorial(n-1)
    * |7 Q/ a* p; J执行过程(以计算 3! 为例):
    * G2 u& n2 x# C) P; w5 r, i) Zfactorial(3)
    4 G3 v/ B3 Z9 w9 A! @3 * factorial(2)
    5 \8 |( L/ k/ ]& E0 h, y6 e3 * (2 * factorial(1))
    / d/ y+ C$ S- V: Q3 * (2 * (1 * factorial(0)))# p$ C* k# B3 }2 m, D8 P
    3 * (2 * (1 * 1)) = 65 `7 [5 p* V; o% f

    - {6 W2 A9 ?1 M1 Z9 B9 v 递归思维要点
    , }* l- J7 W  _, m% A% k6 y& }1. **信任递归**:假设子问题已经解决,专注当前层逻辑5 W' ?3 ]* F2 Q; e& C
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)+ d  H/ W; j' X6 i- \( d3 t- e
    3. **递推过程**:不断向下分解问题(递)
    ( E; R- j; v3 O: I- L, r3 j4. **回溯过程**:组合子问题结果返回(归)$ {  S# D1 ?# L- `5 h5 c3 g, |

    & p/ l# G4 X, w7 P$ Z 注意事项
    $ L8 f# e. {8 D5 K必须要有终止条件
    4 s9 \+ e3 t" b0 A" ?$ p$ f递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ( e5 \1 |8 g( ?; E7 S: O某些问题用递归更直观(如树遍历),但效率可能不如迭代$ ^2 V$ g. [0 n0 r# Y' U' j0 v/ C
    尾递归优化可以提升效率(但Python不支持)" g) `" N. Q& U8 b2 n6 J
    ( k8 Z- a6 j( W) e$ B
    递归 vs 迭代* c: O- M1 z5 m3 @9 {3 e& J
    |          | 递归                          | 迭代               |. r/ \$ C3 x1 ^
    |----------|-----------------------------|------------------|
    6 s; ^5 s$ b, ]" Z| 实现方式    | 函数自调用                        | 循环结构            |. y5 J& E3 m$ _9 i
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |2 I" d: ^7 o/ f& @; Z+ t# Z
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    6 G0 V2 P& p6 i2 e/ C| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |. P+ I* f* \) T$ ]
    ( V7 Z; j' E: ^, e% G" T
    经典递归应用场景+ i1 T& O$ o8 J6 K+ e4 a
    1. 文件系统遍历(目录树结构)# w+ b) N3 t" n
    2. 快速排序/归并排序算法
    ) n' f. M- s7 j. Z2 R2 ?3. 汉诺塔问题. M( }4 D$ K1 A3 W2 b
    4. 二叉树遍历(前序/中序/后序)2 J! E& y5 N9 i7 w5 R' ]+ E" \8 q
    5. 生成所有可能的组合(回溯算法)6 @! }0 {& q4 P. T# T

    9 [7 R8 L( s7 k" G- D3 J8 s试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    13 小时前
  • 签到天数: 3139 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    * y% @; x! E4 s) {我推理机的核心算法应该是二叉树遍历的变种。1 y) F1 [3 O1 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:
    3 _5 \5 x2 ?5 W+ N) `Key Idea of Recursion. O2 G7 l) s# S% V5 g

    - z( ]' v# m0 K$ w# T, {2 fA recursive function solves a problem by:
    4 Q7 X% `$ ?4 h$ u$ Y  w& V: M( e5 F, |5 J6 X
        Breaking the problem into smaller instances of the same problem.# v8 t% m( @. E4 s* ]1 E$ z
    & ?' ]& J' s- t  j* t/ I
        Solving the smallest instance directly (base case).2 c5 x- m! J; z" R9 h+ w! s" a  s% ?# x

    " u# P9 k8 j+ H7 {; @. u    Combining the results of smaller instances to solve the larger problem.
    ; m0 Q+ |& G5 N( P' `5 f# F& m5 T8 l' t; a! Q
    Components of a Recursive Function6 s0 h4 @% T* Z: O

    * Z7 u  G) e5 \+ L    Base Case:) U0 K9 a1 Y- p9 X6 q. |

    3 M8 Q$ G$ e& l6 z        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    9 f) [/ {$ `/ k; x; ]
    * y+ I- w# A$ p        It acts as the stopping condition to prevent infinite recursion.7 [9 ^9 h2 \8 G0 ^- x7 w" I

    ; F  I% v( n! g9 }6 A! ~        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    - x# k1 ^# F! L5 H+ R& @: V) m7 b' h* k$ e. y5 X
        Recursive Case:
    - u5 }0 k  j% F8 Y/ y" C  g4 }9 {3 U- b7 D$ P+ x8 m) V
            This is where the function calls itself with a smaller or simpler version of the problem., X) ~! u  Z) G$ W! V

    4 R/ v" O* A. X+ |        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)." x$ {, u9 C! u# L2 P( \

    3 ^+ L. d& f$ L( h% U0 U" jExample: Factorial Calculation" H$ g% ^/ U# r  _# V
    # x# e+ c, B/ M$ x9 T
    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$ _; U- c- G
    ' Q3 }# F. C: J& {* J1 f6 Y
        Base case: 0! = 15 w, O. Z+ z0 d- J- D3 `

    1 r3 p! }. ^# n% J2 B& _+ e& i    Recursive case: n! = n * (n-1)!
    0 S. I6 d8 m3 L, C; ^. J& @' V& Q: J
    Here’s how it looks in code (Python):8 L- Q. _! s  f  y* l" u7 c9 p
    python# D/ m- x" V* a+ S5 Q( p8 Q

      G4 Z) d. i4 s& K4 Y* D- h9 U8 z, _7 K8 w: |, D& ~% g6 M
    def factorial(n):/ i# C- t8 z& F% i9 M) ^8 s! z
        # Base case
    1 |: R7 M* G2 X' p& K* G    if n == 0:
    8 x( b4 K3 v) i3 a7 w1 \% W+ T        return 1
    % L/ ^* w" m7 z    # Recursive case- z( k0 ^7 W  t4 R5 w6 \; h
        else:
    & O$ T2 Q7 S1 Q        return n * factorial(n - 1)% \0 u2 a! U; J( K& B" K

    1 z! `4 q3 {% h: v# Example usage1 Y) m4 v' c# S& o/ p3 P% r
    print(factorial(5))  # Output: 120+ z& w5 D* N, I

    ' r" n" r$ `" r  Z4 w, Y. p4 IHow Recursion Works
    4 _# ^( |5 `: m  x" n* `! g8 G$ c% n" x
        The function keeps calling itself with smaller inputs until it reaches the base case.- [3 u9 [3 p2 V% ~9 k4 h" Q! a; A

    6 ]; C$ [' F1 C    Once the base case is reached, the function starts returning values back up the call stack.
    . O, ^7 C* w, z) z7 V% s' R
    9 E. @9 C2 k' I2 a% Y0 `    These returned values are combined to produce the final result., P' f: Q. v8 D! z# Y7 [# v

      {9 M; g$ ^% W* ^# i( ^( k3 G4 ZFor factorial(5):
      M1 W% t: u9 p/ y
    ) Y1 M& W! o9 C0 E  R: w
    & P# f: ~4 K( J6 {7 c; f5 ^# Lfactorial(5) = 5 * factorial(4)- m) L- y7 k  m5 t8 z+ n4 a
    factorial(4) = 4 * factorial(3)
    ( w3 u: R1 t1 V5 ^# a* sfactorial(3) = 3 * factorial(2)
    % f; q. ]! a* [9 i' m/ ~; Vfactorial(2) = 2 * factorial(1)
    7 u4 D, w/ e- S3 j- H1 f# l& q& _factorial(1) = 1 * factorial(0)2 w2 o  O0 H+ c# D4 E' p3 O! F. B! E
    factorial(0) = 1  # Base case; m( h5 J; y, s$ ^3 d6 a: j
    ! W; r5 X" P3 d) c" z; E' u
    Then, the results are combined:$ p5 [% P# M1 Q' g( x* b
    0 o" U" t7 T% n; W
    : q/ ], u8 A, J
    factorial(1) = 1 * 1 = 1
    4 Z; W, P( |& `# cfactorial(2) = 2 * 1 = 2) H3 k4 {, x3 ?6 `% Z3 L
    factorial(3) = 3 * 2 = 6
    ; L: Z7 c+ ^) w2 {: D4 |factorial(4) = 4 * 6 = 24( H9 C0 d, c+ ]
    factorial(5) = 5 * 24 = 1201 D6 o- m4 k1 W- [" P' U3 R. |

    / \6 b; x$ s! s5 T9 x0 D1 M2 {Advantages of Recursion
    : v0 q: r* Z' c
    ; q1 S- h* p6 ^: B! Q& V% P    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).
    ) C) l8 a  ^/ a/ e: E4 z$ S$ J" E7 c) ^# e( v/ z
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    8 _. ^( X1 S( C. j4 L1 Y
    % W# Q" s$ ?- W1 s2 n% b; }Disadvantages of Recursion
    " u0 c& S0 h# [
    / _0 A1 X! K$ S1 z: r1 g    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.( Y: t" _  V+ P, Q: T5 V( z- J

    . D, e! j- d. ~    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).+ j9 p1 U$ ~3 G
    - E1 x/ [* ^- b8 L0 x" f
    When to Use Recursion
    ! a2 ]3 O7 A8 l+ j) f
    % p% h1 x2 u5 l4 Z- Z    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    % l$ p1 M2 L3 O4 D5 Z1 I
    * _  j+ }* c5 I2 ^  _4 H2 w    Problems with a clear base case and recursive case.
    * O! u7 i# v+ [" D+ b0 a7 {6 u) E$ T# `/ e% C# m: v
    Example: Fibonacci Sequence
    5 i3 n1 ]- F+ [* w0 t
    " B4 j! K3 b& S; K- k" Z$ cThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:1 s" x& ?; P! A+ P6 K6 L+ @# c+ v4 O
    5 S9 {' d6 Z% d' ?) t8 ?
        Base case: fib(0) = 0, fib(1) = 11 D5 ~$ k' F5 l0 o6 j$ `
    ! u% o2 A5 {, p
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    + h& d) ~% O* [( h/ d) n, i" v
    ) f2 a/ ~- p! [/ I) Q4 ?python, q7 Q8 Z) S0 i8 M0 I2 W
    * h# V& S. \! `1 M6 E# J
    : G0 W( O/ G3 [$ L% r3 a& x; H+ g
    def fibonacci(n):
    2 P( x: v2 F, U5 F7 j& N* B7 c    # Base cases# L' n! y2 i, Q3 e; [9 ^7 T
        if n == 0:0 k4 x- H/ {* {8 @, i# u
            return 03 ~# P* t4 V( B
        elif n == 1:
    $ l0 K8 C; j# K5 E) ]: ?        return 1; L% _+ y7 V5 w3 A+ \# ?
        # Recursive case/ D: i2 h3 m; t% X) ^$ c
        else:; E- {: }% y2 d  `: p/ c# M# H2 f
            return fibonacci(n - 1) + fibonacci(n - 2)
    4 H0 P/ n4 ~' X+ u: y2 J( [/ b- w  n: b
    # Example usage
      u9 l" y0 ?+ Vprint(fibonacci(6))  # Output: 8
    3 ?- b( H, U# Q3 }& R2 a. I7 `8 P% d7 c8 z( z
    Tail Recursion
    " F+ r/ S( }1 X4 F
      ^: N8 V, S: H9 JTail 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).
    ' I/ J! w! S" _7 Z' n( i
    , F1 [6 @" v, F# D( n: F% l3 FIn 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-1-7 19:29 , Processed in 0.030624 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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