设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ! W7 H+ H2 L& G1 O) ^! O0 {

    8 P& P. j/ m1 S# i& }解释的不错" N8 Z; r. T! a3 q. G
    - ~5 d6 [- P0 v& H  l
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    " O) y1 E+ `+ _$ t  W6 E
    6 z. {- e0 G5 [1 S3 l# ]$ l* K 关键要素+ \' k- J+ o" O' Z& x
    1. **基线条件(Base Case)*** b  d3 h9 O; O& p5 L. g
       - 递归终止的条件,防止无限循环
    + ~% z; E" [+ d; K   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1/ T$ h9 i% P/ x+ S* t" ?+ d. ~
    / y" Y" U  I9 m7 p: a$ e
    2. **递归条件(Recursive Case)**
    0 a" Z# k+ A+ W8 |8 N" \' f; |   - 将原问题分解为更小的子问题: O9 [+ E& Q& O* w# ]
       - 例如:n! = n × (n-1)!
      N& `+ _+ A  X% b( _. p4 z
      [& X# I, b( k6 F: h 经典示例:计算阶乘2 k* q. T  G* I1 c6 A( X$ e5 g
    python
    ( |* k0 H3 r/ Y, i: |9 ~' D5 g+ W1 O: Mdef factorial(n):
    " y6 ]) K( s- @8 u8 G    if n == 0:        # 基线条件
    5 A5 O" E) w3 H3 V1 C        return 13 G! D+ B1 x% W3 p2 ?4 Y& V4 u- \
        else:             # 递归条件
    / v: l$ f; Q3 \* t7 M5 K) P: }        return n * factorial(n-1)
    : L; E0 q& Q; n; Q7 V执行过程(以计算 3! 为例):
    % X5 j$ I6 S" V& a" y3 [% ?- [factorial(3)
    / Q2 ?: }& k8 G# O& t3 * factorial(2)
    ) i+ ~  ^3 M  U8 R3 * (2 * factorial(1))& u& o1 _' b* Z$ F$ u, D
    3 * (2 * (1 * factorial(0)))
    5 ^5 K- i* r/ V; ]4 B$ k% x3 * (2 * (1 * 1)) = 6: a1 ~2 l  S9 F
    ( m; |1 ?# g4 k) B% U7 E
    递归思维要点0 h+ E  f3 n( ~9 p0 n% q
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑  b0 G3 r" r! B5 L! W" p+ y( G
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间), }* @* l( T, i' R" o
    3. **递推过程**:不断向下分解问题(递)- o& A( t$ u6 x
    4. **回溯过程**:组合子问题结果返回(归)
    & [; M$ O7 }  g0 T4 b5 g, h3 U6 s+ Q
    注意事项
    % r. y) t, P1 s( `4 N2 R# C: ]必须要有终止条件
    # m4 x1 i- L* f  w0 v+ L2 [递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    0 M# q6 \7 r  e& D某些问题用递归更直观(如树遍历),但效率可能不如迭代
    . O1 Z( {6 ]' S1 A  Z尾递归优化可以提升效率(但Python不支持)
      F1 R1 B6 x, _7 h+ r0 I; ^/ t7 T" u1 Q9 T5 K
    递归 vs 迭代" E+ S# P$ u7 l, E1 x9 G
    |          | 递归                          | 迭代               |
    ; Z0 V9 i" z- E' @|----------|-----------------------------|------------------|( \3 C3 p& U: C7 N( |/ d8 F. b+ N
    | 实现方式    | 函数自调用                        | 循环结构            |
    5 j  E$ N# B+ H4 T2 u3 ~| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |9 o' }% f) ?1 T+ D$ R
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ! b  J. R, A/ ]! k7 F7 a| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    $ ^6 t' d* L7 V0 d& v' ^
    8 S8 N* b3 }6 _ 经典递归应用场景8 D# z% \2 }6 O
    1. 文件系统遍历(目录树结构)
    ) m) p, x" ~2 x1 G% t" ?( }2. 快速排序/归并排序算法
    1 e% _. w) y- f5 R6 A+ w3. 汉诺塔问题
    # t) n  s! s9 V8 |# c: Q0 S4. 二叉树遍历(前序/中序/后序)
    3 ]& ]1 O) O3 i2 z. p: T! v5. 生成所有可能的组合(回溯算法)) p8 l9 X! x3 k7 K) u
    6 c5 B; C& m, @) M) F
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    前天 06:31
  • 签到天数: 3146 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    5 d2 `/ d( z% n/ u9 O我推理机的核心算法应该是二叉树遍历的变种。
    & N* K  O  h: z# O另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    7 Z" X  B( R+ m4 {$ t  MKey Idea of Recursion
    ! i6 U! {1 n5 j6 |3 J# e. t+ {% C7 C8 r
    A recursive function solves a problem by:
    3 t; @- }, q& ~4 c6 k) t) M  |1 n  G, E% t: E# ^* _
        Breaking the problem into smaller instances of the same problem.( r# a( {3 t& ?( X; p2 Q
    ( N+ k9 m4 ?% P) A8 I; U: p. Q: w
        Solving the smallest instance directly (base case).) U" N& N. a  O3 X. i! Y3 ~) N
    1 S1 n6 N+ Q1 w4 ~+ `4 q. s% d
        Combining the results of smaller instances to solve the larger problem.
    : u5 ]& V1 v  I5 B5 h& M9 S2 n9 C# j, G
    Components of a Recursive Function
    : d7 ^3 D& F( N7 w  H; Q5 O1 _$ r/ B6 j1 t, y0 s3 b( N% Q9 g1 a* e
        Base Case:
    ( G1 b- S) }9 b6 u
    7 [) O3 _* Z! V- ^6 @) b1 I( J5 R* Y        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.4 E: [3 ?2 P9 ?( r$ l) l
    $ V4 Z! C! L) t; y6 S) l
            It acts as the stopping condition to prevent infinite recursion.! Q6 c( ?7 s* F, a' \5 P
    # c" @( O( x! {! E6 r
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    % w/ l, d# C' ?0 \8 g) a! E+ J% ~% V* Q  y2 |
        Recursive Case:, F5 e* p$ r3 V9 f2 _) ^

    $ C- R- l, A4 V; r* u        This is where the function calls itself with a smaller or simpler version of the problem.+ N; b! ^. b! v7 K( Y6 ^% u8 v0 }

    # Y7 |; D# o- r4 t! \& f8 M5 H        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).& I1 D; J. U3 j. @% I
    ; W& d8 v- u. a4 n8 V7 w. }& Z  G- v
    Example: Factorial Calculation. t/ k7 d$ M! J8 Q" t2 i

    3 G2 j- o" V3 e+ U8 Y" SThe 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:- ?2 P$ s" \2 E: o7 V/ h
    ) L3 c  g5 b8 E: i3 R% M( B+ O
        Base case: 0! = 1  G1 F, G4 ?1 G3 F) b" B
    ) d" l5 @: \# |$ k: o# k
        Recursive case: n! = n * (n-1)!
    " G% d4 b/ B- }! l; r2 g& A0 z9 g. w* }- |
    Here’s how it looks in code (Python):
    ( Z1 |; G: X6 O0 @  [python! [7 o( R3 z& l2 i1 t0 o! x! D

    # s- w% a' A  c* h' o" r
    9 O1 a" M2 H- e6 [3 Ndef factorial(n):8 ~" W; i8 G# k! X  b; ?7 t
        # Base case% L  `  d  W4 m: }2 V+ f
        if n == 0:: }$ e, O. Y% W
            return 1
    + |# s; H8 m7 g8 R- l6 t    # Recursive case
    6 ~0 b# n' |9 l  {- b    else:  J: O2 F9 R$ J5 l+ K# l5 l8 ]. I
            return n * factorial(n - 1)
    - r3 o9 T3 P' M/ H6 s# Z7 W2 ?# X: f* ]# C
    # Example usage: [; H8 G  l( A4 E' @
    print(factorial(5))  # Output: 120
    ' y) a& B& M& j9 Z& X! [
    - Z& j  m8 a) m7 @$ f# |7 tHow Recursion Works
    - L" X0 z# ~0 X5 I2 N1 @. x( @: |- @# v3 n* T1 e
        The function keeps calling itself with smaller inputs until it reaches the base case.; S" y+ j/ a/ X. ]; a3 ^
    ) b& o$ @: c- `: o. w
        Once the base case is reached, the function starts returning values back up the call stack.0 {) H" S- n7 W# D4 z! P, _6 H

    $ B$ X/ I0 i" O* Y    These returned values are combined to produce the final result.% h1 O2 P1 l* e2 a0 x

    ) c) n8 k7 X* I* t, i' XFor factorial(5):
    % ~; _0 K% M) m1 t! f0 `( V+ s$ D" b! ?8 q; }/ J
    + @( t+ b" Z+ \( N
    factorial(5) = 5 * factorial(4)/ |% b6 q# }! `, v1 i8 K
    factorial(4) = 4 * factorial(3)
    + ]$ c4 X6 O5 [  L  ?" Efactorial(3) = 3 * factorial(2)
    ' U8 `( d. o" w% n+ d& S) L/ }factorial(2) = 2 * factorial(1)3 M8 ~/ T9 |* N& C; Z
    factorial(1) = 1 * factorial(0)
    0 o' L" w' E7 @1 P: r  W% |2 ]/ Kfactorial(0) = 1  # Base case
    7 u" p* [6 R9 Q
    ) N, j; T, G' c) i5 RThen, the results are combined:
    5 Z2 p8 e: [& z' z5 y
    $ p1 S3 m9 ^' Z
    5 F; }' O8 [" x* N# c& a( b% ]factorial(1) = 1 * 1 = 1
    6 E7 h! U6 p! ?% T/ d/ \, \factorial(2) = 2 * 1 = 28 V6 y* r6 J6 W
    factorial(3) = 3 * 2 = 6- e( P5 R1 H+ N# b# R0 b; C
    factorial(4) = 4 * 6 = 24- l& x4 P1 u) _$ c! |1 H
    factorial(5) = 5 * 24 = 120( Y4 h- A) k2 ]* T
    : \9 X) c5 t+ q
    Advantages of Recursion, u1 q* D4 H. ~
    " z( O: o( D# g0 ]+ N
        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).0 I6 G' o3 M+ \5 l
    3 H, h) Z9 Y5 k7 W* Z6 a; H
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    9 A4 s/ \; h. B  W' }4 J5 ^( F/ j0 b& |( v
    Disadvantages of Recursion; [( Q5 ]7 F. \( r/ z

    : W( u5 V1 P+ H: 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 P8 j: U, P, c& M, F3 n
    / _1 @. ^8 i+ i9 y- Q/ L    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).9 w( F: y& ^" H

    ' o: d2 w0 @: U! @# X+ v4 QWhen to Use Recursion
    5 y4 [. P. s* _: P+ |! X2 ?; u% b! E" J$ N2 N) ^0 [
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).+ j+ h# `9 r8 ^" e9 M$ ]5 W7 ?
    ) S- j" d& |+ N- R% ^: e
        Problems with a clear base case and recursive case.
    ) I0 D4 O2 Y- |# k/ Q* Q+ U. K" `8 A; ~3 f- c3 r
    Example: Fibonacci Sequence
    * ?7 k3 @6 H6 l5 @$ }0 V: |2 R' w- `/ {4 \  m
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:4 [! @. [9 Q$ }- W& O. ?

    1 {8 E0 T# B. h. p( \# i5 L' y    Base case: fib(0) = 0, fib(1) = 1
    : M4 H# ~1 B  u* ^: g7 t) E* n; D5 \6 G
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    # n. ?/ K0 M+ F6 d" G. Z3 I% u. q- Y4 V' V
    python% d  }% H; s5 x8 `
    ( t8 z+ Z% ^4 B
    # X) T: z/ z+ u0 z0 p- {
    def fibonacci(n):( E5 g# J# L( [5 o( ~3 g- \
        # Base cases
    6 f/ \/ J6 E+ V1 I% N! Z7 T5 v    if n == 0:* {9 p9 o& R: r6 b- r
            return 0
    5 A3 u$ F! }0 ?# n8 l# x7 d    elif n == 1:. Y$ Z1 X2 L. n: U+ F
            return 19 E) s6 P, T3 m, ^
        # Recursive case
    8 U0 `0 t1 {& h9 i    else:
    6 e6 q7 n" z8 L. V        return fibonacci(n - 1) + fibonacci(n - 2)7 z& x/ @! @! j) m- H
    9 K+ l8 x8 a  |7 i
    # Example usage0 r/ h  g" z: H9 M* [% h6 c
    print(fibonacci(6))  # Output: 84 @! \, s( F& w/ u0 X
    ; K8 r6 u4 k$ _- W% t6 h  l
    Tail Recursion( M# c0 w5 S8 A0 x
    " E3 D# q. t9 d6 l7 V% w. g
    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).
    8 c; |& x7 h- {* f6 D, g! I9 m' G0 H& i* z
    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-1-16 08:08 , Processed in 0.035364 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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