设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 1 O9 e6 f' Q/ e6 V. Z2 r& q* {
    2 s/ A: j" s" z( Z
    解释的不错3 z/ Q, `, @  @0 C9 ?- ^

    6 V! D% a. V6 r! z2 B+ u1 [) E6 _( a8 n递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    & \+ e2 z, P# X7 n# f# S7 P& V  {) w0 [. g
    关键要素8 U+ ^! i- U/ G, M
    1. **基线条件(Base Case)**
    6 x% ^8 L# M/ t4 v/ x& \$ m! \; n' x   - 递归终止的条件,防止无限循环
    " |, f" u2 K1 r: }   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    , x- W' L5 ]0 ]7 |/ Z7 N6 b1 q0 M, ]
    2. **递归条件(Recursive Case)**
      `0 p. p" ^  A& U+ F" {   - 将原问题分解为更小的子问题" H; [7 h$ X0 D: K9 R# q5 H
       - 例如:n! = n × (n-1)!# D+ B- _3 Y" W8 Y$ j

    * h9 d: J+ P- v- R- {) f: p 经典示例:计算阶乘. n" e7 {8 a) p
    python) ~! x6 L+ N# d5 U
    def factorial(n):# P- b+ A/ P/ r1 I2 {! F
        if n == 0:        # 基线条件3 \. e6 v) O& n6 D+ D
            return 1* h/ \  A" h7 z# Y8 H- o  Z* a: l$ h2 g
        else:             # 递归条件
    ! w4 {. Y" B/ _& @        return n * factorial(n-1)
    ! f8 y8 ~# N+ l* j! v+ R执行过程(以计算 3! 为例):- I0 f0 z" C3 @0 l5 g4 G9 q
    factorial(3)
    & x8 L. i" _) N2 O3 C. u* Z* M3 * factorial(2)
    " m) U2 \. Y: g% c" C1 U0 e0 P; v$ j8 W3 * (2 * factorial(1))  W; B6 M' ?* I+ \0 k  D  B8 M
    3 * (2 * (1 * factorial(0)))0 S2 t) e$ p  N. b" {
    3 * (2 * (1 * 1)) = 66 n( L6 _- f% K. B: t' A

    / F( z( n) L$ |# f' W# a 递归思维要点9 y* D$ B% Q# q' Z5 J% a
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    & m5 h% O7 }) d" ^& B; R2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    6 r8 A$ k3 r5 b9 K0 N. a7 y: v3. **递推过程**:不断向下分解问题(递)
    ' a6 E& `, }/ L, [3 J) q2 Z4. **回溯过程**:组合子问题结果返回(归)
    : m+ \7 e! B! L' s9 _
    - {% V1 [  b9 r5 F6 Y: Z' n 注意事项
    5 J$ N. f! G3 p) _( L6 S必须要有终止条件
    1 f/ m8 `" a) a, F7 a& ]; g) G* L1 a递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    & d3 g4 t+ ]5 ]- L& s# q, S某些问题用递归更直观(如树遍历),但效率可能不如迭代/ `2 ]4 I9 C& `. \
    尾递归优化可以提升效率(但Python不支持)
    ) `9 ^& h0 s9 B/ R" Q7 c, [; x5 \2 m; o- d$ o
    递归 vs 迭代* f9 c# Q5 I  }! C
    |          | 递归                          | 迭代               |5 k$ ~7 D2 ~- i
    |----------|-----------------------------|------------------|8 B/ U# g0 @/ m' U
    | 实现方式    | 函数自调用                        | 循环结构            |3 }3 i4 q; g9 a
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    : W, U/ R! b+ A7 l+ c' i6 K| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    2 G; P, `) B2 o- K* J1 s| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |. T/ y# q* s% {# e$ K$ \! b/ q
    0 T) _3 w0 H6 o
    经典递归应用场景+ S, |, D1 }2 B# T! U3 m
    1. 文件系统遍历(目录树结构)# P' q3 ~# s/ H. E
    2. 快速排序/归并排序算法
    % D) A# S5 Q5 s* }/ s# Z3. 汉诺塔问题/ G9 d  q' U, `2 m5 S5 p: X
    4. 二叉树遍历(前序/中序/后序)
    - m& g9 G6 G0 [3 X! p, \) {5. 生成所有可能的组合(回溯算法)$ C. {! N  \  s* N4 u7 K$ w4 H
    0 Q; e# y) |$ x! J! x% A' T: I
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    昨天 22:40
  • 签到天数: 3176 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,# C" o& |* T  E/ s; q
    我推理机的核心算法应该是二叉树遍历的变种。& t1 y( I+ o/ D2 ^5 |
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:( {0 b5 v( D6 e& G4 I
    Key Idea of Recursion- ^: Z, y5 G4 I: H

    $ I+ q3 o9 ]' \* w* |5 C- }1 ]A recursive function solves a problem by:/ d5 E7 y" e( l! c- H. b
    2 z! i3 C5 ]6 y$ ^; t1 b
        Breaking the problem into smaller instances of the same problem./ Z' e; F9 U1 G( N, d8 N

    4 F( c& {( S7 }% A* J8 q. x    Solving the smallest instance directly (base case).+ N1 {0 J0 A/ z4 I* I" o' B

    3 L  j' X# J( d    Combining the results of smaller instances to solve the larger problem.
    , f1 r/ G7 x5 h" o( y% Q; J3 g4 e' P9 o; x( E
    Components of a Recursive Function  p: N- @% R( O: P0 P! S. a- t
    # m2 R- ~/ X1 H" k$ h  E4 L3 ]
        Base Case:
    3 l7 P/ c1 }+ [# h6 ]: ^- ]3 D" ?' j  `  X
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.1 b$ n& N/ |& d: y
    7 x2 H& z( s- u! T
            It acts as the stopping condition to prevent infinite recursion.6 A: H5 G* W9 n, k$ Y" {% u1 p. n
    / s: e& O& e: R" ~* _
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    5 h9 ?" X' p% \6 t2 r8 K  d5 a" K/ M7 ]# h) h6 ^& a
        Recursive Case:
    ; m8 o9 {( y9 s3 C- N
    ! g/ \  m3 V1 w( r        This is where the function calls itself with a smaller or simpler version of the problem., d0 H1 G2 F! }  H$ l2 k: U

    ) L0 R; l, z8 u- U3 a8 v        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).4 K( m2 q) y* H* K
    . @* j9 j; v! M* [& ^6 W
    Example: Factorial Calculation
    4 w9 w9 w& s/ N* h- d' w' o* ~4 ~' x1 a$ {* r
    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:3 V9 S+ S; A% ~! ?: @
    ( Y$ n( i3 ~) n* M1 W
        Base case: 0! = 19 c8 z. P. @2 e9 [2 b

    ! \, u$ m& C% y5 c9 @    Recursive case: n! = n * (n-1)!
    ) Z- Q( G  H  }2 ^7 A2 `
    / Z2 {& y9 f" e- m/ ZHere’s how it looks in code (Python):5 {( M9 x$ P1 s: W1 w+ h
    python7 F0 V. T/ g2 F9 }  R
    % X" n* E- }" o' M: r. g
    ! i! l  n9 S+ t$ b
    def factorial(n):
    & h1 e0 G0 a0 M! l    # Base case/ V/ J# E/ I) e; Q6 d
        if n == 0:
    " j9 }9 p6 H: s- o5 J        return 1" V( N" p2 Q+ T* G1 D/ X3 a
        # Recursive case
    $ C3 P( d7 e1 N9 {    else:* }0 G  F  [9 l- |* y) Z
            return n * factorial(n - 1)) A" f2 [* r7 s* |7 j  ]! H
    ; y2 N. u( T( e: j1 c
    # Example usage
    " N' ^! D0 z7 Z! }1 Xprint(factorial(5))  # Output: 120. q% U8 O) _% l9 p- j% \: q! ?
    ! I$ @5 G' n% ^& q; ^2 z
    How Recursion Works
    - Y5 P( ^* S2 P1 y2 k1 w$ ^* C  f
    ' X4 s, n1 N! G  e    The function keeps calling itself with smaller inputs until it reaches the base case.
    ( e7 ^' i* M5 E# y/ |  d
    1 w$ S3 s6 p: b+ i+ k  c    Once the base case is reached, the function starts returning values back up the call stack.# o4 W/ o: A* s3 q, [

    2 S3 w5 J0 t9 N4 n$ l8 ^    These returned values are combined to produce the final result.
    ; {2 n0 T. O/ v9 S* `7 Y* j4 `! O. I, t1 X2 {5 ^, L
    For factorial(5):9 w% S& S0 L% Q
      \4 P  T# U: l% V

    ; ~7 J$ y/ p" W+ p6 e/ efactorial(5) = 5 * factorial(4)$ ^8 ]9 y/ n6 a
    factorial(4) = 4 * factorial(3)8 r- p1 N+ V1 z2 ~3 ^' e+ j9 Q9 X
    factorial(3) = 3 * factorial(2)
    # i( D0 ~9 i& c- D' m2 d1 d( Dfactorial(2) = 2 * factorial(1)/ W- |8 z. x) a% y
    factorial(1) = 1 * factorial(0)
      X6 C& d( z3 U& m* C" `factorial(0) = 1  # Base case
    ; x# d$ Y( a  o% Y6 o
    0 w9 F0 k, l+ v3 O3 pThen, the results are combined:
    7 q  I% @" o( d  w; |/ P; a/ @) Y! m1 P* T# {6 E0 f- }

    9 b0 G/ ^* x. x. ^  C4 Ofactorial(1) = 1 * 1 = 1
    1 D! v3 `! B% o( afactorial(2) = 2 * 1 = 2
    ( Y. C3 m0 M  r: v3 ~4 X) Jfactorial(3) = 3 * 2 = 6! x# m* V4 e- G, C2 r% I/ c4 w' r
    factorial(4) = 4 * 6 = 24
    , q! l% ]3 P; _) B3 C! }% w2 yfactorial(5) = 5 * 24 = 120/ P/ T/ v' |$ K  e
    ' h; V+ i; _; Z+ @% Q5 f  ]& m
    Advantages of Recursion+ x/ L" S" K) N' f

    8 m1 w& D3 O% j- @( X    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).: t. N) z4 b/ d2 Q% M" i
    ' j, u/ y7 c# ^& K
        Readability: Recursive code can be more readable and concise compared to iterative solutions.. r: B8 [; c5 R1 ]) c, `1 w& ^7 n3 H, K
    7 V5 N4 z* [6 p
    Disadvantages of Recursion
    ; m, a. `  l. G. S! ^2 r  j! {
    . A5 N: j; c2 k: M% @6 E% i& B! 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.
    : M' r: Z. u% A: V1 d; A5 f+ @. Y; o- g
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    * D( {/ o1 n0 O' u( j% Y% O* s* k7 o4 L: U, X/ H" o3 v
    When to Use Recursion
    / N# l% J! R7 ^1 s! q( D. a; a" S
    1 m' `0 z  |& W+ z( k& G    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    " J1 ^1 r+ p& O8 p9 z1 {5 i; s* W' D1 e9 \3 l8 a& A: Y( c! T3 b
        Problems with a clear base case and recursive case.
    ) p+ ]4 K: T4 F6 P, D8 A, ^3 M
    6 g! R7 k+ ]! i' VExample: Fibonacci Sequence0 ]  G! m; `4 g6 J6 r6 n, v+ Y9 F
    & s$ |7 }* R' h% S3 ?
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:/ H0 h0 i- @1 Q8 U( u
    5 {  b/ N9 T; `4 L2 d
        Base case: fib(0) = 0, fib(1) = 1
    : M& R3 H2 G6 w% k% V+ `. R3 N" r: `6 E" g* J6 C7 t
        Recursive case: fib(n) = fib(n-1) + fib(n-2); \& Z1 y2 [9 t3 v

      u( m' w8 I- S/ Epython
    ( N. q( T, d+ h. ~- B2 Y1 {) i4 s: n, x* h
    & X  G+ M3 i6 U7 y- t
    def fibonacci(n):
    / e, s5 f' y7 ^. P    # Base cases
    ! D9 q: m7 ]$ `; T8 f9 n    if n == 0:
    + |- h, i4 \4 q' f9 r2 t6 h        return 0( P' P% \, R9 ?; u2 w
        elif n == 1:
    & n$ ?: C% E5 r: m: P: N        return 1
    % L' P1 v* ?. \  N    # Recursive case$ y; p" U2 ~* s3 A
        else:
    % A% q1 U" y! Z' P& I$ X        return fibonacci(n - 1) + fibonacci(n - 2). G+ v3 b# Z2 @! ^1 c- V

    ) z0 Q9 @9 m  A9 T+ d% }4 a# Example usage. }3 m. A+ T7 A, P3 @
    print(fibonacci(6))  # Output: 8
    4 Y. [# g, w6 f; ^$ S5 `8 e. K5 p- D5 g
    Tail Recursion  P' C- A& Z3 O+ ^! E* p1 p
    6 f, q1 d3 w4 f3 a! W) a
    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).
    . y2 h4 A. l) \! v* y; [( f8 s) V$ Q- E4 O- B- y+ |
    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-2-18 08:06 , Processed in 0.068284 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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