设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 & A: }) @$ M) @! F7 W

    # U7 ?  b6 n0 v, J6 S解释的不错
    # f* g: w0 ~1 E4 z- W" y2 ]; l  [( L' U+ c! m" x1 m
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。* L  U6 W; a6 J$ S
    - e4 T7 Y! E: r# g$ j
    关键要素' B7 V9 n: D  f# B( }+ s3 s# Z, F
    1. **基线条件(Base Case)*** V% V# C" q! ^: Y0 d+ s/ }0 s
       - 递归终止的条件,防止无限循环
    6 E5 ^3 m* y1 Q7 [9 c   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    / L* c0 W9 c: ^8 W$ b0 ^' L5 r
    8 k; U: u5 y, |% m$ l2 F2. **递归条件(Recursive Case)**
    , T* E" V: w2 Z/ q" j. f0 ?   - 将原问题分解为更小的子问题8 z( r! z3 M9 `* K3 R1 X+ f, t2 `
       - 例如:n! = n × (n-1)!
    ) D7 {9 l: `  _( |, y5 A, c$ W& Q8 q2 b5 H+ G! h8 _/ Z& m3 Q
    经典示例:计算阶乘8 F, ^  {2 H  v' G! x( }8 g
    python
    , U; \$ X) ?' U5 j: @- J# Hdef factorial(n):8 B' c9 P2 i- p
        if n == 0:        # 基线条件
    4 V1 B6 T+ G. N7 j: J& @. x8 Z        return 1: a5 G  ]' l3 r# k2 i6 R, F
        else:             # 递归条件
    * h0 g0 ?9 M; m        return n * factorial(n-1)
    * H  n. i" \4 n' z执行过程(以计算 3! 为例):
    6 E$ I+ W% |5 n8 f9 O* ufactorial(3)2 N8 h5 \7 j) }2 e7 l
    3 * factorial(2)
    - ?0 W* G3 y1 [( p3 * (2 * factorial(1))
    5 G3 m+ g/ [. {& D0 R6 w3 * (2 * (1 * factorial(0)))# [! S' U1 U1 q5 `1 O0 @; }
    3 * (2 * (1 * 1)) = 65 v4 Q5 [+ ~4 N% [2 u

    6 |* {3 N8 t% v) P( {5 u 递归思维要点
    6 b9 q- I+ o9 U+ S5 I' L! D1. **信任递归**:假设子问题已经解决,专注当前层逻辑% v  {3 R0 j3 |
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    , m1 X* r. }* |# \* d3. **递推过程**:不断向下分解问题(递)
    ' ]4 P7 x* B9 \; E9 a- K* W. B6 f4. **回溯过程**:组合子问题结果返回(归)) l( @  e& r' o: H
    ! E$ C4 n2 z7 S" n, e- z% b4 P
    注意事项* s$ C: F7 b# X, y% q4 v
    必须要有终止条件
    $ S$ w. _  D9 X5 H, P8 }递归深度过大可能导致栈溢出(Python默认递归深度约1000层)8 R; b% }6 y/ \* ~
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    9 z4 V' ]& K/ A& ^6 H! V尾递归优化可以提升效率(但Python不支持)
    . o) ~- L( }) m# K2 X  `! K! P
    ' N3 J' V! n4 ? 递归 vs 迭代
    $ `0 d2 Y! h; e+ Y1 e+ R|          | 递归                          | 迭代               |
    ' [1 J6 k1 x+ ~, {/ K7 M|----------|-----------------------------|------------------|0 }1 P6 q& S" |4 S$ R3 I
    | 实现方式    | 函数自调用                        | 循环结构            |# S' k+ x% J& Z3 ~) y6 T& J
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    + V! M4 H# c  `) U8 V0 Z6 |4 j| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |9 X8 r# T& n: c5 P8 |9 b. U
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ( {6 J2 }3 S3 `
    9 q, u# u" `4 o/ S  \6 { 经典递归应用场景" A4 B5 g5 ]7 j4 w
    1. 文件系统遍历(目录树结构)2 h! ^. C, @4 e& s1 b8 c) x
    2. 快速排序/归并排序算法
      s! ^) \8 m- v$ S' s3. 汉诺塔问题
    1 ^, e$ \" B- }1 g4. 二叉树遍历(前序/中序/后序)9 I( m9 z9 @5 ~. @1 ?- i% m, {
    5. 生成所有可能的组合(回溯算法)( E6 ?$ l$ g5 u9 y9 \9 K0 }

    1 P3 R, c  V. V+ L3 y% x0 w试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    10 小时前
  • 签到天数: 2971 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,. s, P# o, J: X2 j7 C
    我推理机的核心算法应该是二叉树遍历的变种。+ A, i7 ?3 ?9 Q4 G4 f
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    ; e1 q  ~8 ?3 X1 m& M7 RKey Idea of Recursion' w  J. v6 U3 E: w' |
    6 J' f' Y- h2 N6 V
    A recursive function solves a problem by:
    0 n3 o/ S9 R, |! r8 G! |9 w6 U% B' R9 `5 P
        Breaking the problem into smaller instances of the same problem.
    1 I: K8 u3 _4 i' Y1 c8 U4 C
    8 K- y% r& V5 G& u0 M+ @  y2 w    Solving the smallest instance directly (base case).& `0 ^+ ^8 U( G0 O

    , l' @' K& b) I: Z% {    Combining the results of smaller instances to solve the larger problem.
    # i' w2 y0 J* \# b; U
    % S1 ?7 g5 [/ b( e, AComponents of a Recursive Function
    + C  t! v7 h% W  G
    ' W+ b* N  O" }) F7 `& U    Base Case:
    . m) c) L- l( x4 T+ [( ~4 z
    1 O$ Z8 Q$ z9 R% H4 q0 \# K        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.7 G+ h: j; _) K/ q& e1 L2 [

    6 e. n0 P) U; g8 H8 x, i: l4 e$ y% p        It acts as the stopping condition to prevent infinite recursion.9 J* M8 Y& T8 n3 A3 O, C; ?

    , Q: z* J+ h% t        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.  S' ]0 ?; Q4 }: Q, A7 M

    4 ^( _  d8 i( h: s* G6 o    Recursive Case:
    - T  c* P/ Y1 ?( M5 C; Y) j' e0 a% R; e9 o9 ~
            This is where the function calls itself with a smaller or simpler version of the problem.
    ( k7 o6 F9 f: d/ f* ]& A
    8 V/ w3 F, ?# i/ J        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).. R2 _* S0 L$ l6 B9 _/ g

    6 F2 z4 H, ^$ ?  m) S- g6 g( N" a* q* \Example: Factorial Calculation
    / D' M5 Q9 Z! {% w- {8 V" z; m) M
    ( p/ V, s' S/ `2 BThe 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:
    ; s4 e1 k0 B3 n& o6 W
    " @5 C7 e" r1 V8 r9 W, W1 z; Y    Base case: 0! = 1
    3 V% B9 {) s4 |; {1 M2 [3 z& ~) v% ^/ L0 J5 @0 l; J- X9 N
        Recursive case: n! = n * (n-1)!: U5 S6 B: ^0 `8 a

    8 O( _; P# C2 Y  ?Here’s how it looks in code (Python):! {6 D- U, A" P: c
    python
    5 E: N; d! S( f2 w7 O2 X' G/ B* h, @1 `+ z) a: o

    4 V  |6 C  ?, I; [def factorial(n):
    # B8 i0 ]4 L9 C$ A/ x' L' `) f- R    # Base case8 F  p5 g1 i# N# Q4 t% H3 n
        if n == 0:. ~2 G# |* X% q3 g9 _# o/ W
            return 1
    * E  l& l* K5 y9 Y2 Q* C% }3 o. b    # Recursive case
    . q0 m9 S% k* q! t, X& f0 N; O+ H8 V    else:  s$ c/ @6 o0 N9 K( U
            return n * factorial(n - 1)8 I& m& c3 u; K3 W: `
    5 ?7 U" b3 ]# Z+ S4 f) U
    # Example usage
    0 B; ^( Z! }" a* W1 }print(factorial(5))  # Output: 120
    & R- x. o; Y/ Q+ B  T, E* z
    + t# z! R3 N$ D5 LHow Recursion Works
    0 K4 e4 N3 T9 y( I& ~" M) `7 _' |. ^% y; G
        The function keeps calling itself with smaller inputs until it reaches the base case.) q& X/ W7 c/ q8 _; c3 g
    ; m6 R1 L  E% e+ J
        Once the base case is reached, the function starts returning values back up the call stack." N+ D; B1 _9 K! @7 V/ z% k' J5 z

    ; h# j9 ^9 ^. w" `% Y5 }    These returned values are combined to produce the final result.5 i$ ~! ?7 S. s6 B* o4 j

    * E0 x8 t/ P7 ~: ?4 w) LFor factorial(5):
    7 R% P$ U1 G1 A" x8 Q, G: g9 B% u/ T8 C

    # w5 t% T7 U4 a" afactorial(5) = 5 * factorial(4)8 w& E- F, F% Y( T
    factorial(4) = 4 * factorial(3)" F. z2 i3 T1 k  W5 D  k
    factorial(3) = 3 * factorial(2)+ G( e4 m; }) |% x
    factorial(2) = 2 * factorial(1)$ k5 O$ X4 @$ k8 J
    factorial(1) = 1 * factorial(0)
    8 c( Z0 }) t1 ^, i7 B' `$ |factorial(0) = 1  # Base case
    / s! M$ x$ ^( i& O+ m9 H2 |1 a/ p9 @! D8 u; P, D3 g  X
    Then, the results are combined:9 i( q5 I, m& i/ ^, Y. D
    1 k: K& M2 ^9 x2 v* q

      n. Z. i* a% qfactorial(1) = 1 * 1 = 11 ?9 R8 L" ]. `1 N+ }/ N
    factorial(2) = 2 * 1 = 2$ X4 M( }' j' c9 B
    factorial(3) = 3 * 2 = 6
    ' v: ]; t4 Q4 l- pfactorial(4) = 4 * 6 = 24+ I5 F. B  A0 E/ p
    factorial(5) = 5 * 24 = 120& H* n/ Y" B' \0 D$ h$ \+ d$ z
    / m$ W; G" j$ j- t7 X  V
    Advantages of Recursion; M: f+ q* Z5 T' ~6 `  X1 e7 w
    * \- e0 t& M" i
        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).' M3 {, u. h# ], F7 F4 A& Y
    - f+ J7 x% H9 M9 A+ I
        Readability: Recursive code can be more readable and concise compared to iterative solutions.+ S8 e6 `* n/ d. F! }5 K* p0 }

    : ~; e/ [: v# B8 {# x, KDisadvantages of Recursion  ^  _# A+ m: D! t! b" N

    ) h* N1 J3 O6 z4 \- _$ |( C    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.& f9 v/ s' [- Z5 Z
    ' ]+ l& S$ D: W: @: l
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    - R+ H$ K8 \8 G: M+ W  p1 T" F* k8 u( s6 x: Z1 P$ j8 e
    When to Use Recursion3 _: ?9 d' }) K1 W
    9 c# U# X$ \3 \& l: ^
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    4 Z6 R! g/ h: j* C) D
    + R/ g/ t) i8 p* C1 K    Problems with a clear base case and recursive case.
      p: J1 s, h( Q; T( p# V+ Q
    , h+ \: U$ V* a0 x; _9 RExample: Fibonacci Sequence
    + w; V# B3 I7 r, J, T7 w
    6 ]" J* M+ |8 X* D# b# }  }$ QThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:: C9 z. c  Z2 G3 K! n
      S2 Y, Y0 C" \- r8 I
        Base case: fib(0) = 0, fib(1) = 1# S' V6 T0 W+ r' s* S& p3 u

    & l% T( \5 I, X; B    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    7 S4 c* [. C# P1 C
    % j! z9 b1 _! s, Zpython
    ' E) O2 w2 s5 r( O$ b" _( R3 `9 w" a" A, u- ^
    & ~4 o1 r- j7 r
    def fibonacci(n):8 F* o2 K6 f) Y: E4 d' H
        # Base cases: ]3 w1 N; M, l* ^4 S
        if n == 0:
    6 [) q0 E! q7 A* m        return 01 ?) l2 K. n' E. \
        elif n == 1:. W. @8 i" [+ D: K
            return 1
    7 A) q$ T% L6 m( S' _    # Recursive case
    * J3 @) P, k& Y! m" `7 y    else:
    2 G! z7 V! w4 X9 A; h! o$ k  b        return fibonacci(n - 1) + fibonacci(n - 2)- W: @1 H& M1 Z0 P$ Q* o7 `% A, L5 ]. x' @
    , F5 S  R$ x  {1 B0 q
    # Example usage7 F5 o4 n6 w) b( @3 a6 l
    print(fibonacci(6))  # Output: 8
    * x  ^# i' b$ U; H* f& ?- F2 q, ^0 g( N/ w$ n! I" n2 o+ v/ I
    Tail Recursion: Z1 t8 i. y7 E: c: g4 c3 m3 d3 H
    * K; r  b; j9 X( O+ \
    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).. l' ]8 E: h$ T

    - h4 O9 b0 x) `- Z! d5 T8 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, 2025-7-6 16:17 , Processed in 0.044993 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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