设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    2 c9 G- z4 Z4 e3 x1 c1 G
    % Y1 t/ S! N! ]0 P' I解释的不错
    8 T- u3 K3 \7 u) O  }; Y4 I6 B* r: N, S/ J( V$ @6 E/ X5 S
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    # K; Z& c% @( G) O. I
    ! X+ G) W+ j0 Z% \ 关键要素2 x% M6 y4 |: u3 f6 }, E
    1. **基线条件(Base Case)**
    + m) M6 `! x$ d1 U% A7 J& z   - 递归终止的条件,防止无限循环
    : Z% L2 ~* \6 u/ Z   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1: _; o! E7 A/ @3 L2 Z% M
    $ I8 f% x6 ?/ W4 V# P
    2. **递归条件(Recursive Case)**4 l" _$ s8 o; k" @1 ]0 T) |- T
       - 将原问题分解为更小的子问题
    3 u6 r( |* |) a: E, N   - 例如:n! = n × (n-1)!
    $ B6 S  u% y0 \! j2 d8 u& m
    & }2 P( p+ e/ U' G! e) y; u 经典示例:计算阶乘% S  x/ K$ X" {' ^) @4 n3 s( ]! s
    python" o; G5 c0 p9 L1 H
    def factorial(n):
    ) O+ z+ c9 [  @$ c    if n == 0:        # 基线条件
    1 W' V. q3 ~  m, v& v* g$ U        return 1( f/ F8 _& F& X; Q3 z3 ~" N7 P' R
        else:             # 递归条件
    9 K5 V9 g* r- G0 x        return n * factorial(n-1)
    * f/ N) f8 R2 ^$ K% d( A8 b执行过程(以计算 3! 为例):: y( N& A- O+ `! E" g
    factorial(3)! S$ q+ H; b) J8 n- |0 s
    3 * factorial(2)+ f' _, ~: E% }. j( M$ g
    3 * (2 * factorial(1))* K' t: p2 U. a; P9 ^# L; ]$ X
    3 * (2 * (1 * factorial(0)))' y5 q8 k3 G- \: R
    3 * (2 * (1 * 1)) = 65 r: _, J5 _9 I; V
    " T' ]+ W# F  f! b1 b2 i
    递归思维要点$ @  q& c/ z' K- N6 Y
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ) m- O& Y! w9 P2 l" Y2. **栈结构**:每次调用都会创建新的栈帧(内存空间)+ E  |9 L/ ?4 F2 H4 F) ^0 w# Z
    3. **递推过程**:不断向下分解问题(递)
    . w5 X- U% z7 h& c/ ~2 h# v4. **回溯过程**:组合子问题结果返回(归)
    1 B7 l# w9 q$ _3 `2 t5 I+ L4 r  {7 \
    注意事项, v! j' l' @+ N5 c
    必须要有终止条件& Y0 ]5 Q$ t3 [! S) D1 _( Y
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层). t( K6 R8 U( O( s
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ( I2 s3 p( V0 G尾递归优化可以提升效率(但Python不支持)) d- W6 O7 H5 g- g7 t8 s0 w+ m& ^
      J' C" m3 r* ~
    递归 vs 迭代
    8 c0 g; ?7 T0 `|          | 递归                          | 迭代               |
    ) v. |- t5 ?; }: h|----------|-----------------------------|------------------|
    & q7 a" j" P! `- f6 k. j| 实现方式    | 函数自调用                        | 循环结构            |
    % v- q8 ]# m$ a' i1 p| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |: Z, {- b* I* O. |6 R1 f
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |! j- g6 D, w0 f2 Q5 g$ [0 t7 v5 v/ Y
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |$ z" q4 ]/ r. g! |
    , N! f( q- ?6 Z) y
    经典递归应用场景
    & x( `2 h$ o  o! }; o5 |0 t) f8 v1. 文件系统遍历(目录树结构); ]+ Q0 u. T- K7 V
    2. 快速排序/归并排序算法
    & x8 f1 Q+ j( |! I( Z/ X9 {3. 汉诺塔问题0 l4 r: t: c% L$ M' X/ S
    4. 二叉树遍历(前序/中序/后序)9 v; e- y. |4 _# T% Z3 h1 i
    5. 生成所有可能的组合(回溯算法)
    , B2 H% X6 D. B: {: J
    8 U3 N$ O8 |' l- C) c试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    15 小时前
  • 签到天数: 3222 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    / W& p' y; e* P1 @1 R6 b我推理机的核心算法应该是二叉树遍历的变种。
    0 n3 W% l" T) K5 R另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    ' o  J' x0 B6 ]4 ?" R5 O+ zKey Idea of Recursion% C: [7 E# H& m4 c2 v

    $ I6 U  u3 N2 H% bA recursive function solves a problem by:1 L# t' x% s; T6 Z4 P4 P

    3 y- n5 ~- A& q/ G: z% R! L    Breaking the problem into smaller instances of the same problem., F- e+ W/ l& ]  P
    & X8 Z$ `: K9 M/ _, t
        Solving the smallest instance directly (base case).7 r/ G& z$ _0 o. z

    7 \" H& T% I9 G2 Q% `6 x    Combining the results of smaller instances to solve the larger problem.5 D* m" }$ l- b* [. f

    4 c! J) B0 s2 KComponents of a Recursive Function
    0 ?2 l% u+ y( C8 O1 x; f- N0 c+ Z" Z& X& Q! C
        Base Case:
    7 z9 {, K! K9 O# Y2 Y2 @8 e; s
    & z* u6 ]7 G+ d1 y1 g8 d        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    3 G' o# T4 ]' v: v9 O
    * r4 a' R! G+ E4 P3 _* N1 ~        It acts as the stopping condition to prevent infinite recursion.
    / B- \' x( b1 Y3 y! V" b
    % _/ a9 Q2 K, w1 T; O        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.# ?. B4 \5 `3 K" h% `

    # L: w: x: ~+ e+ g0 ]    Recursive Case:$ p9 J" R6 n* S: O, r+ u3 a) g

    " ~; u6 n' e: _# S" T        This is where the function calls itself with a smaller or simpler version of the problem.
    : L8 E% T: `! K" T. ?) t
    8 `* |0 M$ N( M- e        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).6 |9 o- Y; m) [# b% n2 x

    5 C: [- x9 m' e; U+ gExample: Factorial Calculation
    # \) f- p0 x: P: N3 R1 P: O, d) l1 ~* o. ?7 u2 g% O
    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:# l1 T  ^+ E+ P, X: h
    - |+ i; H( I$ N( ?" b) Z, C
        Base case: 0! = 1: U& N( s& \1 M2 c% u$ d$ t
    8 O$ i( [9 V, W, J& Z' d
        Recursive case: n! = n * (n-1)!/ u& b+ X3 I3 d" W

    1 Y$ P  E7 ]" [9 G) VHere’s how it looks in code (Python):
    6 ~: k. Y/ A2 w6 N5 b* Q7 L8 hpython9 R+ F( J0 G& H0 f

    5 b0 e7 _3 F& B  d, n. A/ F4 D" @
    def factorial(n):
    # x  P4 x8 N+ V& V: O# J    # Base case
    5 w4 B: h' p: h3 m    if n == 0:
    % F9 p* g# U6 W8 k. |( d3 Q+ m1 V        return 1
    + `# r1 [3 H' \, x! u    # Recursive case
    " g; P2 V- b/ `# A    else:
    * p. E  {* r* K; }. e' I, k  t        return n * factorial(n - 1)
    $ y) v6 B! y3 _1 i% W
    5 y- f- j2 @/ A$ g# S# Example usage' k7 N$ v! G6 w5 }/ W9 [9 B7 H
    print(factorial(5))  # Output: 1204 l8 D3 j3 [3 X! j: Q5 ?! u

    9 U6 i* ?' o% z2 _. F8 XHow Recursion Works* f0 }$ b, f( }2 Z; j5 ]7 k

    / p- s9 l" z+ d) y2 V. b, `    The function keeps calling itself with smaller inputs until it reaches the base case.
    5 d) C' [7 E( a. {' m0 [/ v  {; }/ |4 o* D5 u5 D
        Once the base case is reached, the function starts returning values back up the call stack.% M/ p) r1 o6 F6 S
    - z" I' f/ ~- w/ V! S2 t1 o
        These returned values are combined to produce the final result.
    * c. x9 o9 S) P( ^# e# ^8 ~7 s1 o6 A- J& ?8 s/ W
    For factorial(5):8 I2 A( c) m# ]$ E9 L/ J

    7 g! J6 I) m% a. N# c9 \8 K* V8 ^) N4 Y. F, }1 ^$ D1 T& X
    factorial(5) = 5 * factorial(4)/ O- N( N& H3 Y9 P: N% T7 R, j
    factorial(4) = 4 * factorial(3)
    . Q- ]8 _" [, a! W8 P, O5 Nfactorial(3) = 3 * factorial(2)
    " j$ s7 e8 t2 Y' ]7 kfactorial(2) = 2 * factorial(1)
    - _: N8 d0 O. t6 A4 }. N) H3 ]factorial(1) = 1 * factorial(0)
    + k9 [0 X* m- Q6 xfactorial(0) = 1  # Base case6 ^  z0 `  E& P( q

    $ {# G# _+ H8 M; A9 L+ a7 uThen, the results are combined:3 a$ ^: d( R$ E; w
    $ Z# Z; C' {4 x' y# f
    " s  R9 w2 I1 j/ M* o$ x! h+ g
    factorial(1) = 1 * 1 = 1
    % e# |7 t+ i. X- R1 V+ x) f" Rfactorial(2) = 2 * 1 = 28 _7 ?5 H6 e) {2 v1 G
    factorial(3) = 3 * 2 = 60 v( p9 F; O* g5 B! e4 v. |( Y$ |
    factorial(4) = 4 * 6 = 240 h( B. {/ t# U0 }* F) [# h
    factorial(5) = 5 * 24 = 120: o1 l9 E. Q9 P" W4 M
    ) Y% Z+ r/ m  L
    Advantages of Recursion' q' p. ?0 F4 n

    7 ^1 g4 ]" n! Q2 U    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).
    $ D$ H2 B1 V0 a7 w" B4 t; J9 |7 G5 f
    3 f. y, k7 G/ _/ a! u$ Z    Readability: Recursive code can be more readable and concise compared to iterative solutions.# K; G8 V1 t5 z9 p6 b* g

    9 O6 {# H- r4 J& `- n/ P  RDisadvantages of Recursion0 `# ~2 t6 G9 X" T) J$ v4 |, [
    . R# }. e/ B7 y7 `: F
        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.# L# X2 Y, S& W  q

    * A/ y/ l( i6 @* C    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    * ]) p( I( i3 c* l8 d) d' N$ p. o6 _3 o
    When to Use Recursion* `. Z) P- t, }3 y) m! O: L

    * `+ E6 i' i& o+ ^7 L    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).  {2 ?4 L3 E% \! I6 X' C

    1 T5 {: W+ j7 W! `) w2 W* ~# q    Problems with a clear base case and recursive case.
    , Q0 `4 z- o0 o6 T4 d
    . q4 N1 e$ w' P$ ^) I" k! ^3 nExample: Fibonacci Sequence
    ! F$ V9 k( m& z) H3 g' C9 c+ q2 G# ^1 c
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:- y; E9 n; w0 O3 f  I

    ) B" o  v% d1 Y8 D+ c# V/ z    Base case: fib(0) = 0, fib(1) = 11 b$ z* d& B) r' b% d
    ( @/ e8 f) E' u" P2 ^
        Recursive case: fib(n) = fib(n-1) + fib(n-2)6 u4 k. G7 O7 ^

      i% k+ D5 G$ t5 U. kpython
    * b  d! r" g: ^' x$ x
    ' K; W+ U9 h0 P1 C: V8 u- {: A( d; f5 @, v7 i; g
    def fibonacci(n):0 Z0 l$ ?# p% ?1 E
        # Base cases
    7 B7 T- v  R4 ]7 G0 n: M9 I. ^: P    if n == 0:
    ; @& o* e! V9 I. b8 V8 A        return 0" l$ a% z( C/ y, g: d
        elif n == 1:: y; f( C5 J3 v9 z& {
            return 12 v3 v* ?6 D. C! k
        # Recursive case
    , F5 k, l/ t& N% `    else:
    0 j6 h6 H, n, n. U7 S1 d7 z        return fibonacci(n - 1) + fibonacci(n - 2)
    ) R8 d- f& S. ]+ F
    + e6 Z5 a+ U+ _1 \2 k) w# Example usage
    6 [$ Z: j# o/ e. W3 w4 G8 |  D8 X$ aprint(fibonacci(6))  # Output: 8' s; F" b& p+ C  H: g. Q* R
    * k- {, ~; \) e2 B. ]0 A
    Tail Recursion6 u$ }! M' o( ~5 z7 m2 A' L
    1 l& O$ e9 d4 S8 u
    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).
    ! e5 m% @% R" O; d. W  ]) A- N- b( P- }2 `* M  h
    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-4-23 23:00 , Processed in 0.056181 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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