设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    9 b; {; M$ K+ x: }7 u) u$ b7 X9 f) C3 f1 p
    解释的不错
    0 U8 U$ D2 ]- [3 t5 X- C9 j! S( O4 \) A& n
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。- ?4 n8 @7 O" ~. Y) q& \" Y

    ! ?( u/ w( v. {+ J" \, ` 关键要素
    ' L' q6 ~2 g/ e' F1. **基线条件(Base Case)**) I. K# i* L- n2 h9 m1 f0 O3 e: j
       - 递归终止的条件,防止无限循环) W# M* H+ E) U  {
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1! Q4 d2 E& N# n  ~$ x( D- l/ u

    8 r+ j# t, E0 u+ F+ `$ `% F2. **递归条件(Recursive Case)**
    0 \5 W7 u5 _3 ]7 o( |   - 将原问题分解为更小的子问题
    ! C+ W2 E& c' O- F   - 例如:n! = n × (n-1)!+ E7 j+ Q9 [9 ~8 V# D
    + |- p* ~% |$ [
    经典示例:计算阶乘+ {. \, N/ Y/ v: o7 H  {
    python5 Q2 Q! L" i8 A% G# G6 z
    def factorial(n):& ]' v9 Q' h; R# J
        if n == 0:        # 基线条件5 T3 h  F( b1 n$ G
            return 1. G, U& y3 t2 m6 l0 w0 J* y+ V
        else:             # 递归条件
    7 F; z- U% m, R" M3 ^        return n * factorial(n-1)
    8 f$ Z4 [( k6 @6 [执行过程(以计算 3! 为例):8 M* V( n1 V/ N* N8 a8 o5 F
    factorial(3)
    , D: V9 {8 _6 h- ?& V$ z' e3 * factorial(2)/ v# m! U7 D7 b0 Q9 L$ h
    3 * (2 * factorial(1))
    2 T1 A  Q0 O" S7 E$ e3 * (2 * (1 * factorial(0)))
    0 E8 r& q6 e3 w2 m1 s, e3 * (2 * (1 * 1)) = 6
    3 }% \0 [$ k5 i1 u) {2 z" f0 E) C
    递归思维要点: Z" _9 i3 g. O; m* c
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑6 I- y  I# n! P; ?3 t! P$ Q
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
      p  S- k1 K' d' J" }  @3. **递推过程**:不断向下分解问题(递)
    / y8 S4 n% j6 h/ _4. **回溯过程**:组合子问题结果返回(归)* Z; g% n$ U  w0 R1 O" r2 C
    1 e6 [( @, B  V3 t# a) Y3 g
    注意事项. R; D# ^( `- h" f$ _$ }: K
    必须要有终止条件
    8 o) K% K/ p  A1 M& c0 ]递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ; t6 e5 Q5 F% m  f7 L, `/ H( u  }某些问题用递归更直观(如树遍历),但效率可能不如迭代
    7 j7 p, H8 U$ b尾递归优化可以提升效率(但Python不支持)- h1 E" T$ ?0 Y6 }3 [( `
    2 O9 ~1 m8 `; l5 @! P! a4 [; S
    递归 vs 迭代+ r2 X* d+ a" O, z9 v
    |          | 递归                          | 迭代               |
    ' q( `! T7 z* r4 s|----------|-----------------------------|------------------|
    - g0 |1 q5 P+ A" P0 Q: e: p| 实现方式    | 函数自调用                        | 循环结构            |+ s" P' u- U7 }8 J6 h
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ' U7 b/ n. j" \9 H6 ?5 U3 o, k| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |- k2 H6 G# _9 o( s6 C# N
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    4 V- Z+ r/ P7 J0 @2 G/ p/ i$ m( i4 I3 e  f( Z' E+ r
    经典递归应用场景9 N/ @+ z1 b5 Q: _  w
    1. 文件系统遍历(目录树结构)2 [' F, H6 y3 m! G- D" B4 d' q
    2. 快速排序/归并排序算法
    ( [/ ]/ L9 N* K0 y7 y" N3. 汉诺塔问题
    ) Y4 o' o6 r8 Q: t( O$ R8 w  n4. 二叉树遍历(前序/中序/后序)( T- q. C% p' q/ @- J1 ^! W
    5. 生成所有可能的组合(回溯算法); o$ X4 J" J$ W; _  s9 {

    , M3 T$ C/ F* r+ J+ [: _/ p试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    昨天 12:19
  • 签到天数: 3107 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    0 @, \0 J3 ~2 V- g$ I* x我推理机的核心算法应该是二叉树遍历的变种。! D1 _( q" I: j" q: a5 D; x- q
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:: L) Y7 k) f% A5 T: s5 ]2 R
    Key Idea of Recursion; |5 W3 F9 z" Q  F1 ]1 U

    * i( j$ _* r1 |4 J1 B: A( JA recursive function solves a problem by:9 L7 T. e0 M3 _
    3 g  ?& I+ X" m
        Breaking the problem into smaller instances of the same problem.
    + c2 h1 M# e1 L  q
    3 Q* w" V* T/ G. y& _7 j    Solving the smallest instance directly (base case).% j8 S  B3 L% `( T/ E

    ( |1 e$ J" d1 o3 ^    Combining the results of smaller instances to solve the larger problem.
      u* U0 ]6 Y1 b5 M# X  Q6 e9 Y
    0 j, X( Z; h3 z( HComponents of a Recursive Function
    0 j% e0 M: z  f( a
    / V9 O, |4 l; ?7 M3 C5 i    Base Case:! ?; ~- A2 z3 Q. O; y4 f, M
    3 f: i! {* f/ f& ?* O
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.* I! H5 ?# K9 O/ s  g9 p( k3 m
    / {3 t5 v: L7 }% q8 c+ u. L' S
            It acts as the stopping condition to prevent infinite recursion.
    ( n# b- ^) e6 t
    4 B- K( a0 {+ c  E7 [7 @        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ; ~+ |) G8 }# L: X* P% W' h
    : P3 |. `4 E2 t; \+ |# q: J    Recursive Case:
    0 J# Y$ \: h& P$ N* }& C* P, C" y
            This is where the function calls itself with a smaller or simpler version of the problem.: e4 a- Z: g: B% u5 P0 |9 o" k

    ) L% f/ a8 \3 M4 X# p+ f. x6 t        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)." c5 Z% K8 S3 p! L* X

    / w6 V7 o) U$ R# v- rExample: Factorial Calculation. V3 }0 p$ K9 U2 K4 n
    $ K* t; Z" ^, U: V1 M1 M
    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:8 M6 P' {1 f8 N' [' D% }9 j- _

    - b! e8 a% r% X1 b5 z, e    Base case: 0! = 1
    2 k7 x9 T  s- H4 x
    * @6 ?9 M, u9 u' D    Recursive case: n! = n * (n-1)!
    5 [$ U5 d+ g: d. P! P  Z- s# P1 f4 C/ r0 O7 _, a" L# G' C, w8 U
    Here’s how it looks in code (Python):0 Y- H9 u! }/ g2 o) J$ Q  @
    python
    9 i( q( t! S9 N9 R' c8 V7 p( R, U5 U& N# y5 T# O: A4 {

    - ?, N9 h# K/ t2 y3 ]8 }. R2 zdef factorial(n):: w+ `4 T( ]9 [% c& c2 T$ D7 \
        # Base case
    ; X8 d+ [# a, b& ?2 e    if n == 0:
      L8 B4 d# F; V9 Z* m1 Q! u        return 1+ E( ^6 D8 g1 D( r# ?; e
        # Recursive case, X' u+ ?3 o2 l( B2 f$ Z  ^
        else:' ]- i0 G. K* L. t  F- s  W
            return n * factorial(n - 1)9 d- d1 p1 T5 q" S# d2 P6 P
    9 K, M) H) ~0 ~! T7 C
    # Example usage$ ~: d! y3 s: M6 Z8 N# @
    print(factorial(5))  # Output: 120
    " I1 H7 l5 Z* z( Y2 Z
    6 X- G1 |, b8 e' ]/ c  W3 Y$ [# pHow Recursion Works
    % v! d% ~1 ^( J  B+ }1 a! o5 c" s6 E: `
        The function keeps calling itself with smaller inputs until it reaches the base case.
    6 L7 c% ?- Y7 d4 ?/ w9 L: K
    # [% |' M" i9 F( F4 f( \& s    Once the base case is reached, the function starts returning values back up the call stack.
    $ m% z1 J9 ], i. @8 M- U# ?
      U: N; o9 C( ]- I5 ~* j: e5 t. ^    These returned values are combined to produce the final result.
    % a. S) i; c- j5 b7 v: k$ M: I7 x5 x( [0 t( z6 f0 J
    For factorial(5):' ~+ ?1 }2 b& ^. o& i
    ! d, O9 L$ I  }4 H7 W* l, g
    ! E, H( G1 R# C( ^7 j/ U5 q, u9 O
    factorial(5) = 5 * factorial(4)
    . y9 [# l1 Z) j  m5 Cfactorial(4) = 4 * factorial(3)
    1 Q9 a$ k# x5 D1 E) z/ J; Mfactorial(3) = 3 * factorial(2)
    * e$ C3 T2 k% |0 w9 @5 n$ M1 Gfactorial(2) = 2 * factorial(1)
    * Z1 ^6 Y2 m2 S/ C6 Gfactorial(1) = 1 * factorial(0)1 o. _! \' s+ T0 ^# ?  M' [- E
    factorial(0) = 1  # Base case2 ]  g) l6 x. l- i% o. y9 }
    6 {% _7 _9 {. z1 h  T8 \7 T
    Then, the results are combined:( x- ]6 \  s4 t  B) h, S
      W! T' ~+ r0 q
    7 Z2 X7 a6 F! I, _2 u
    factorial(1) = 1 * 1 = 1! R) G7 Z+ g* o, U. _9 W
    factorial(2) = 2 * 1 = 2" r2 W1 ^, l! ]
    factorial(3) = 3 * 2 = 6& K  N' E5 j: m) K  C! L
    factorial(4) = 4 * 6 = 24
    # W" P6 i5 M$ ^9 x2 ufactorial(5) = 5 * 24 = 120( k  P' d+ {- w- P* {3 j& d# Q

    : ]5 y" k% O6 S& X% lAdvantages of Recursion3 i$ @( Z. u2 w+ l( a& O/ h

    1 b3 Q& U" ]/ x( R    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).: r; R' u# j1 b4 w) y

    % s3 C" r0 B5 X& G4 Q    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ) P# e0 L1 ^  Z* e8 x+ X7 ]2 g+ }; C% @
    Disadvantages of Recursion
    ; m4 C7 |0 z2 A  i' @0 T% g* x5 F7 F! ?0 F/ M
        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.& g  w! E2 @% n( t. g1 P+ p

    2 J* K( Q/ y0 H8 s$ A7 X5 h    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).# b/ D- c9 x$ j+ ?& C: i
    ! w$ B' `1 V7 B# A+ i$ Z1 r$ {
    When to Use Recursion
    6 p% W. h5 a4 x9 _- L
    & _# p- B/ V) B5 R5 r; X' E4 g    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).# I8 v$ C7 W! M2 b; J# z
    3 j; I0 N( I( L
        Problems with a clear base case and recursive case.1 `& K/ c. B0 v$ w
    , w+ W% ]* z$ |) {- F0 {( \, f
    Example: Fibonacci Sequence* G+ X" ^* H- p" u& Z

    0 E; @& I/ A& Y1 ]$ [5 ]" uThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    $ [  `" z. x: s+ d" Q% b
    * \, p3 N. h, N& H; p2 s3 _    Base case: fib(0) = 0, fib(1) = 1$ c9 J3 G4 D  d  Q

    " I' i: H6 q& s; U5 F2 |    Recursive case: fib(n) = fib(n-1) + fib(n-2)( F  Z/ @- v0 h3 [
    4 @0 S- q9 ^( Z: U
    python
    ( o, X" d/ W, g- h% ^$ D; @6 F5 u; I$ S- B( E5 w$ T) w$ v/ Z
    ) W8 w  u, E5 t$ v0 O
    def fibonacci(n):
    * X6 e6 c4 L! S3 i4 L, L7 F    # Base cases
    # u+ `) J- e7 p0 N6 k    if n == 0:1 N) e" g5 M; C& ^, `0 n
            return 0
    / s5 k' E; l; ?; w+ S2 p. J' B" y: q* m    elif n == 1:
    5 h# y" H' r# t$ [        return 1$ s7 n7 h, O8 N" Y3 K
        # Recursive case
    6 K3 h, S# g- h7 ^/ d0 R3 k    else:4 C) b2 J0 n4 ]) W* V+ d  b
            return fibonacci(n - 1) + fibonacci(n - 2)
    3 Q  S: q- g8 C3 ^, N; Z9 S& D6 }" a
    # Example usage" }# \$ [( f6 ?" b
    print(fibonacci(6))  # Output: 8  G* ^0 Z0 y9 |, \
    ( v2 F" d6 E( |5 l
    Tail Recursion0 y# G6 [$ o' d0 t9 x3 |9 l* ?4 e* Y
    + D9 N# z. e4 N) Z
    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).* E; u& A; J( X. w/ X
    / o) S% ^* ]: ]+ b
    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, 2025-12-4 10:16 , Processed in 0.031873 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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