设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ( ?. V8 P( ]$ }6 N8 Z' O& M/ t

    2 X- H0 E* F% {' y, k0 K解释的不错
    / P8 I+ B: X; N. f% P' _( c- ^0 k3 q# Q" }1 V0 }; @
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。# y+ K9 m, \5 t& ?; P/ C; @
    & V( N+ C5 X' l4 \# z/ G0 S
    关键要素
    7 j) f6 N/ Z! z5 V1. **基线条件(Base Case)**
    2 G9 E  ~" }/ g% {! j   - 递归终止的条件,防止无限循环8 h3 M% Z2 `" f* L5 V
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1& T9 C4 \/ S8 j+ G9 d! Q) v
    : H5 J* R! \' ~/ c# s
    2. **递归条件(Recursive Case)**& u6 X0 v) \# {5 ?8 `0 x/ s5 P
       - 将原问题分解为更小的子问题# Y/ ^  Y) O! o. P% \) U4 x1 @
       - 例如:n! = n × (n-1)!
    - |; v3 A$ E% i( `
    9 P2 Z/ b/ I" `3 s/ T& f 经典示例:计算阶乘
    8 }2 f- ~9 p4 N7 K* s4 Q0 x7 ppython: s6 r* A$ h0 R' \6 K
    def factorial(n):
    % ], `- c+ z, a. `    if n == 0:        # 基线条件
    7 N( _& L5 K  P& @  U* Z, u        return 11 X' S0 ?* [: ~; {4 w3 \% Y  c
        else:             # 递归条件
    1 E1 G2 g$ T2 B0 O) T- f' N        return n * factorial(n-1)
    2 ?; C3 r2 h! @" [/ I4 ]7 V执行过程(以计算 3! 为例):4 G: J' v3 f+ A# l
    factorial(3)4 r" C) p0 C! @4 d) H
    3 * factorial(2)2 p# r+ W# x2 v; ?* s$ V3 |
    3 * (2 * factorial(1))
    & d3 ~# E- Y* H& ]9 {3 * (2 * (1 * factorial(0)))
    5 p' D6 g% e( H/ D3 * (2 * (1 * 1)) = 6
    & j% o: v+ Z/ T6 L) y0 Z0 [
    * ~* r( b% M2 J7 P( v: N" k 递归思维要点
    7 ^4 H; h$ W) I: O5 u( A! m1. **信任递归**:假设子问题已经解决,专注当前层逻辑) h$ a8 a- E& ~! l$ W( J# i# q
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)2 m+ M" y7 A$ }7 ?$ W
    3. **递推过程**:不断向下分解问题(递)$ s. M. M2 b  ^/ }4 u: N$ O
    4. **回溯过程**:组合子问题结果返回(归)1 x. M6 m( I9 x6 z) P7 T, e8 `

    & h% q2 W% L' f4 P7 C 注意事项, ?& @/ t2 m6 _. K! |5 n! c
    必须要有终止条件; u# L) T& S2 b9 f5 N! U
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)- f% I( ]' `1 f) R
    某些问题用递归更直观(如树遍历),但效率可能不如迭代" |3 T, _( @$ M9 V
    尾递归优化可以提升效率(但Python不支持)3 |+ @9 e  R7 r
    , t- h' ]1 Q. M5 B
    递归 vs 迭代
    1 \& @) j4 t8 C( t! Y|          | 递归                          | 迭代               |
      i6 P! T2 Y3 O' E# G|----------|-----------------------------|------------------|* g6 N5 ]6 E% m& x& A/ d
    | 实现方式    | 函数自调用                        | 循环结构            |
    % p% O. ^! B/ [# b! r$ |5 ]| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    7 t0 R, D$ k' H| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |) x- c9 S2 |7 T
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |4 o* S$ L) |/ |1 L

    6 C# P$ Q) e: m9 H. ? 经典递归应用场景
    ) E* M5 W5 a& o; T+ ^1 O, \1. 文件系统遍历(目录树结构)
    & d8 \6 J6 \. z0 U8 D6 c" [, g2. 快速排序/归并排序算法4 V/ u8 m, ~! D; @) V
    3. 汉诺塔问题
    : H3 C" ?' b) e4. 二叉树遍历(前序/中序/后序). q" w" O8 c' e
    5. 生成所有可能的组合(回溯算法)  i' b- p$ Y# h, {5 @
    : ?! U2 Z2 ?. @2 |) [: ?+ s
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    4 小时前
  • 签到天数: 3117 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ! t: S6 r# T7 j2 i3 ^. U9 Z- O我推理机的核心算法应该是二叉树遍历的变种。6 a6 b2 g/ u3 x* p( 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:, R% @7 E4 |& R, g$ `
    Key Idea of Recursion
    $ b0 e  W2 p- ]# ]: r' S# `" P% l" W9 ?8 k
    A recursive function solves a problem by:
    + o4 n* N. v, _8 ^
    $ l6 D8 S' @5 M  q+ u    Breaking the problem into smaller instances of the same problem.
    7 W9 V3 _3 E# M* B2 K5 `* Y5 L" j2 Y# }* C, U1 T; R& N" i
        Solving the smallest instance directly (base case).
    2 j4 x1 J4 |  t8 I, P2 g+ \
    : i& K! p7 a/ S' t    Combining the results of smaller instances to solve the larger problem.
    ) d4 w4 y4 _, l
    & N% }  e9 x- d! {; CComponents of a Recursive Function3 w  B1 \3 S% W" I# y" f

    + J- t0 x0 V4 ~" \  ^! E0 c    Base Case:5 ?9 r- `% y% c( }2 Q" a

    $ a" ~0 j8 @, [& |7 u. `) E" h        This is the simplest, smallest instance of the problem that can be solved directly without further recursion./ V* @1 K! j0 F- a
    2 T3 H1 K2 p; |9 P
            It acts as the stopping condition to prevent infinite recursion.  V" p" y- W0 s6 H5 y

    0 H$ s' @( u5 }: ^  U5 T# r' a        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.9 T- t! S) w1 H+ B: q: \
    ; t6 `: G, ]3 e
        Recursive Case:1 H% I$ X& {; M* y
    8 w- C* c/ b/ g" w4 X6 Y! d
            This is where the function calls itself with a smaller or simpler version of the problem.
      N- x1 A2 g, p/ B
    ! [. H+ F0 `' i$ Z  \        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    2 [  }+ s3 H9 `1 x$ ~
    " \" D6 m5 b2 aExample: Factorial Calculation
    ( h3 ^- Q! t( Y
    2 U! h- _/ C6 L7 B: OThe 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:
    7 B& ?" a( h; O2 ?6 |7 h" z# E  L0 }" S3 _) o6 T
        Base case: 0! = 19 c) O/ R# Q5 m2 e

    9 v2 m: l- E$ r% F0 V. X    Recursive case: n! = n * (n-1)!4 B/ H4 r! ]1 i3 i: @
    1 |' M5 ]: C3 i. C- N+ D  U
    Here’s how it looks in code (Python):
    6 U! H8 C; L6 S! r/ V2 Spython( n7 _( \; J. q! ^& }

    6 a4 d5 D% |! j/ I! M) Z* n
    " e" R- D+ r6 r$ N' h2 ddef factorial(n):: V2 a3 x* @, w8 r
        # Base case+ u% I* w) G9 S7 D( O, ~( G" f( ~7 O
        if n == 0:
    + a6 R: R$ n" [! _, W; o        return 1( _. }1 |) ?, D4 j
        # Recursive case* Z0 s: j3 {) F* o. W9 s: \( {
        else:8 }& W- r. @7 [- s5 j
            return n * factorial(n - 1)7 n% y& q8 p. b  v

    $ M% \2 e% ~! n' F. s3 p# Example usage
    " }' R( i9 d0 ~* e" d( }9 Vprint(factorial(5))  # Output: 120% o0 Q" F. ~" \; g0 |5 W0 J/ D
    % L5 }# \: d6 m
    How Recursion Works9 _9 l$ z% r- Q0 F

    7 ]' ?6 ]1 ?' l; M- y    The function keeps calling itself with smaller inputs until it reaches the base case./ Z' C$ r% z# p7 s0 ~# H$ ~9 Y4 Z
    ! C) X$ ]" {. a# H! X
        Once the base case is reached, the function starts returning values back up the call stack.
    7 ?4 V6 O% A2 y9 x$ M( a
    : ^  f, y0 p$ |5 y    These returned values are combined to produce the final result.
    % d. {0 k( W! q+ |, I. G! H) R7 B
    ) v! Y* P2 J5 q8 YFor factorial(5):- ^4 I% A" }1 ?; Z' p+ S7 w

    4 x. ]9 A0 l- `' G" b/ s- @9 o6 D* X1 r, u
    factorial(5) = 5 * factorial(4)
    ' i2 I* c- [, E+ R* W- ~factorial(4) = 4 * factorial(3)6 l/ ~' j" J/ t+ x
    factorial(3) = 3 * factorial(2)) [! d; V0 C/ F
    factorial(2) = 2 * factorial(1)
    $ a8 U" u; Z& }factorial(1) = 1 * factorial(0)
    ) [6 |; M$ b& O, B$ _factorial(0) = 1  # Base case4 M1 @6 f# w2 n) E) h; V

    , y) \3 V+ T3 YThen, the results are combined:( `* }" h5 P( r

    6 L$ `* A8 Y9 [. z/ x& u) M# U: _8 J9 e5 }! K8 ^- Z( c# f
    factorial(1) = 1 * 1 = 1
    0 E: V/ W. E, h& v  E* ~/ kfactorial(2) = 2 * 1 = 2
    # z: x. C2 T4 n8 t( y5 Y9 {' I1 ~& H$ |factorial(3) = 3 * 2 = 6
    + R* X  ^! \: J# J# k& xfactorial(4) = 4 * 6 = 24( `  x' Z( J, c1 x
    factorial(5) = 5 * 24 = 120
    : b; |$ R+ C( J* j7 c. I! N+ W5 h
    % D7 C3 [0 P7 f: I4 O7 i" RAdvantages of Recursion( h* a2 R% L+ u5 l3 g0 b

    % y- I7 Y& I  f6 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).
    ! `% k! a  x6 Z7 }! D* G. H* @6 a( y
        Readability: Recursive code can be more readable and concise compared to iterative solutions.# Y% d* s) |+ l6 y
    1 X1 Y2 x/ r7 y& N; ?; H7 \+ D
    Disadvantages of Recursion
    ) |  J! W- C; A! N! o. l& k0 u! g5 a
    9 V- ^  Z/ i: F$ h- O. i. i    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.
    * @# P% L; a2 C1 G8 r
    : }2 r. C, Q. M6 C% r' w0 b    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ( S& O9 j- n# x9 p! M. O" T* a" m
    When to Use Recursion4 k1 a. [  Z, a

    ) I( x- }3 S/ [; I7 W. M# h    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).- w: W1 i6 p0 v

    - F2 M4 i* }1 z  @7 Z  ]# t    Problems with a clear base case and recursive case.: f* Z+ V: d8 R8 S* t$ [
    * b5 l# [7 K" A. |8 E
    Example: Fibonacci Sequence  f3 ^. c3 u# F) J7 D

    8 C6 I; i0 Q: j0 ?The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:6 [4 Z" N4 r' B# Z, Q
    . v6 Q$ h2 l, x/ t$ h( U+ a9 c
        Base case: fib(0) = 0, fib(1) = 1, y* N" s/ ?7 a4 Y2 C( u
    ' c, [7 I. x8 ]2 L, h
        Recursive case: fib(n) = fib(n-1) + fib(n-2)% b4 P4 _# `1 [
    + ~: d2 p3 v3 e! g
    python- T1 F" [7 n, j0 m7 A

    - H. L+ b8 s5 E6 ]8 P5 L7 J3 V* D* T, Q! o! v9 ~  c
    def fibonacci(n):
    8 U$ m1 \! f3 n; D. v( X    # Base cases" e) F8 o0 r( n, {, A) X( c
        if n == 0:
    7 D! b" [' V7 x' ]) m9 E$ V2 Q        return 06 o! `. l2 u: A/ l  A
        elif n == 1:8 {3 O" E$ l1 t4 g  _
            return 1
    0 X% Z9 W& \) B- V5 W& }: S5 o2 @+ V, E    # Recursive case
    * Z* h: [/ y! o5 q- ]6 ^    else:
    ' a1 M& K9 f: A# T* z        return fibonacci(n - 1) + fibonacci(n - 2)% r; J) |% j, _; _8 j

    . V3 F$ m% m2 n- y$ ]' X% h% @, C. K# Example usage$ e% ~8 z0 \3 z
    print(fibonacci(6))  # Output: 8
    ( L& @: S! E" {8 d1 j' ^9 r
    3 D& q' J" l' a- Z5 ^2 hTail Recursion
    & X2 T4 u7 [0 d2 a* [! x. q; @% `: T8 Q. s
    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).: f0 q7 I7 ^0 e1 ~
    2 @# W; i# [" N- d- N9 u
    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-14 11:55 , Processed in 0.034468 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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