设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    3 P: J+ v  n) O* I& x2 C: \2 q$ Z$ g- p
    解释的不错
      Z) C# e0 p4 p) j
    ) ~' Q5 h, k' Z; I* R递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    0 j# G, m7 l6 N. K4 A& q4 |; l% I: e
    关键要素# C8 g$ `8 E( c; z1 R1 v5 A
    1. **基线条件(Base Case)**
    3 j, B& G' z0 X( _. w7 q/ m   - 递归终止的条件,防止无限循环
    5 t9 w6 L. m3 n& J( T   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1% _; x* A+ g" i- M

    ' Z* |) t6 u' y. |+ O, g- `2. **递归条件(Recursive Case)**1 }* S8 y; _3 ]7 V6 I  K# Y& u( V
       - 将原问题分解为更小的子问题
    ) Z& a8 [! t9 }   - 例如:n! = n × (n-1)!
    # a+ ]. ], t% I3 S0 e
    6 `3 {( ~" U- {0 E 经典示例:计算阶乘% @3 ?: R) }9 x) W5 J2 ^: o# k
    python! {6 H7 p2 f: |
    def factorial(n):9 H! S# q0 z! }* `: g' O3 s
        if n == 0:        # 基线条件
    8 Z5 q+ k- T) D# ~" U# \        return 11 f6 t$ j, ?: m9 K; [. {
        else:             # 递归条件% o( |# s( g& P5 s) j) r
            return n * factorial(n-1)
    % I7 A7 ^: f( f7 X' D执行过程(以计算 3! 为例):4 A: k9 \$ L/ f% Z, |
    factorial(3)
    ! ~/ L5 q& f/ T3 * factorial(2)
    7 {. z$ z9 G$ `! O: C3 * (2 * factorial(1))9 s0 p3 o. P, ^5 R3 ]
    3 * (2 * (1 * factorial(0)))
    ) Q* N* i& v+ m3 * (2 * (1 * 1)) = 6
    # p: B5 Y2 a7 P8 I) T7 j& r; p% C, y  c$ q1 p, E
    递归思维要点6 l+ k: K& X* u1 z$ H" B; x' ?
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑2 s' n3 h3 ~3 [; |  v* J2 ~1 k  J
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    3 T/ }# L/ v) H4 A( o3. **递推过程**:不断向下分解问题(递)$ i" p0 F1 @; O  i" P7 x- h
    4. **回溯过程**:组合子问题结果返回(归)1 E4 W- b! n" ?+ h: Z

    ) W9 h: s' o$ H4 b/ U( U9 t 注意事项. d# o# j. g2 R, i) Q
    必须要有终止条件
    % r3 J( \0 Y4 `6 E! T递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ( q4 V1 T3 A$ \- ~$ K3 u0 S某些问题用递归更直观(如树遍历),但效率可能不如迭代& s6 p, A* T0 i% {5 ?0 G/ m
    尾递归优化可以提升效率(但Python不支持)
    ) n" H( |: V% m7 |
    : W3 c& B0 w. Z' O3 v# ?/ x/ r* e* {2 E 递归 vs 迭代
    $ R: _4 l$ s+ H6 f( @$ r  c|          | 递归                          | 迭代               |
    ; ?; l5 P+ @, T2 b' r7 V: {|----------|-----------------------------|------------------|
    3 ]2 P  t0 }6 b" S' Y| 实现方式    | 函数自调用                        | 循环结构            |) X* h1 g1 k) o) C) I. X* u. x/ f
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ' y' Q0 \% z7 e# ~& x1 ]5 e/ P$ z9 r; A| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    6 [+ h8 I) k! d. w+ V| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |' \: n; @0 h4 N  A
    3 N1 G/ g1 p$ a* P' R
    经典递归应用场景
    ; ^0 L  ?, c+ y3 E+ H& [' T# T8 x; Y1. 文件系统遍历(目录树结构)
    6 `3 b3 B) S! e+ Z- {( X$ l' t2. 快速排序/归并排序算法
    6 }, u9 c$ n% K) P$ [3. 汉诺塔问题4 @' X: g6 |9 n9 l! e& Q
    4. 二叉树遍历(前序/中序/后序)
    6 W  G% e& n- v5. 生成所有可能的组合(回溯算法)
    * o3 I) O* E0 \5 |2 Y8 [9 S+ P# A, s6 @  v5 Q0 H1 P  W
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,3 S1 Q; d! T" j8 H
    我推理机的核心算法应该是二叉树遍历的变种。
    * k  K" K2 f& X! {/ |6 M- m另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:) A' D) }4 \6 a# [% p; _
    Key Idea of Recursion% n; Q% |0 p2 w" S8 j1 o

    $ o' b. f- F3 F* Y9 f) W6 g, [4 \A recursive function solves a problem by:  P/ `* S9 p( }" f8 g7 J
    , M; k% j) S- {' s. p% i3 Z
        Breaking the problem into smaller instances of the same problem.' f+ S: K2 G3 P

    4 A. T8 r( W" X& a! |    Solving the smallest instance directly (base case).4 H1 D8 W* o8 U
    " i7 ^3 |9 S& L& u% M) T
        Combining the results of smaller instances to solve the larger problem.
    " S  c  v: I: }/ J" ?* a% B6 i) v3 d3 H! P$ A* b9 k/ W
    Components of a Recursive Function
    1 B% V4 q: [, _8 F. b) P: y: o* k0 k: P2 @5 |. y
        Base Case:
      S6 C; S  d; c& R2 ^( R& u8 O
    1 \$ J( x: H, `4 h5 t/ k        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    7 E4 Z: h  O' c/ y: Q3 o' l5 |0 i4 j. C
            It acts as the stopping condition to prevent infinite recursion.
    0 B5 @# W: ?! B7 x) z
    ' d# C2 F5 G6 F9 b, I        Example: In calculating the factorial of a number, the base case is factorial(0) = 1./ F' t- e. b0 {6 b

    ; M$ X$ K6 ]4 A  y/ s( ?    Recursive Case:
    ; z: w  G$ o2 i0 u
    ; T! a+ B' P% N; O        This is where the function calls itself with a smaller or simpler version of the problem.
      r% c% J/ G4 j9 e: {
    ) P& l( i' F, ], m: [7 s; D        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    " t0 q1 @5 g% A( G6 x3 U7 t% F1 g/ |' m& y
    Example: Factorial Calculation6 h- B7 E( c( W! |3 [
    * M3 H7 y5 o* X- x5 U3 v
    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:# W" N. L$ J6 @  C- b; |5 j8 `
    6 ?# `- m8 ~: Y! S5 f7 \2 }
        Base case: 0! = 1
    2 k% }/ N- c8 Q" M, `" F! K( x
    * W& w3 ~( O1 Y: `& ?# R: o5 M7 g    Recursive case: n! = n * (n-1)!, @& |6 ?2 J7 c* p/ @6 ^
    ; n$ z% |& n* u& l% l0 X4 }/ _
    Here’s how it looks in code (Python):
    4 Y( B' Z, d8 e$ t4 O  Q2 F: Mpython
    * n. x7 N8 M, N5 ^- u" O" K: q, Y- o5 E' s# C( V+ u; z/ E
    / B, P' y- k/ M8 a: M
    def factorial(n):
    - k1 `) o. G9 u    # Base case
    & N% K8 Q' ^, j+ M    if n == 0:) d4 d- [/ r- \$ v) `+ x! L
            return 1
    4 \! _1 s; d: L! ?: [8 ^( r    # Recursive case
    7 h  K( S9 T" u# P9 H    else:- ], q2 l, h  v: b
            return n * factorial(n - 1)8 P+ W: G( ?  X3 b7 p
    / E; N( f9 l% e; Q' G
    # Example usage6 X' M9 z, X( T* G2 G
    print(factorial(5))  # Output: 1201 E; _5 c: d( H( {
    6 i  u9 ~( w( t$ I
    How Recursion Works- B8 g% r; z$ j4 p
    * q7 J1 P1 M# h, L/ }3 ]2 C
        The function keeps calling itself with smaller inputs until it reaches the base case.
    ) d9 v# w2 d+ {* L% g" w, @! c9 D$ c- E8 M3 y& b2 ~
        Once the base case is reached, the function starts returning values back up the call stack.
    9 \' N& K3 i6 I
    1 M; Z* e! s' D8 g: v    These returned values are combined to produce the final result.0 _# p0 n" ^3 |# Q4 Y: U
    9 H( ?' b4 X, X) f1 }
    For factorial(5):( p3 C1 h. X; e0 z6 _; j
    % f7 q( [5 `- o( l. f2 `' ^
    5 P  Q. ]! }' I9 X
    factorial(5) = 5 * factorial(4)
    0 s) s  D3 Z3 _$ R" s. o# L4 Zfactorial(4) = 4 * factorial(3)
    8 Y( M9 f0 x8 qfactorial(3) = 3 * factorial(2)
    7 E7 \$ ^: z. J+ a( W! E/ O+ v7 K2 ofactorial(2) = 2 * factorial(1)
    ! T( s% A, H1 c, O5 n. Q; xfactorial(1) = 1 * factorial(0)1 _9 `3 P( @% w6 {5 p/ i$ \
    factorial(0) = 1  # Base case
    ; |) `* v* |/ x: a) ^7 k$ N( Y, g0 [, R1 \
    Then, the results are combined:
    % m3 ^. U6 r3 ?- C( A9 X
    6 v4 N6 |2 @4 _$ j- N. m& d  v4 M& W% I) J/ j$ r! d
    factorial(1) = 1 * 1 = 1
    + C0 E, `" a) l8 |) Hfactorial(2) = 2 * 1 = 2& ]3 A0 T! J2 R9 ?7 ]
    factorial(3) = 3 * 2 = 6
    # x* F0 W0 j1 C- V! H( L: `9 lfactorial(4) = 4 * 6 = 241 Q* r" \9 f/ C. D
    factorial(5) = 5 * 24 = 1204 d+ T# O/ Z% y, t  d1 v$ p' W

    3 F2 W' c- G# Z* b1 |Advantages of Recursion
    . q! \1 w) L* {' E  `& y2 }
    , p$ T' C, c9 r, r* U& h0 B    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).
    0 `3 C: B6 ?( [
    2 X( ?, O' d3 o, L6 y    Readability: Recursive code can be more readable and concise compared to iterative solutions./ a7 \  p  W/ d& ~' {" x

    8 x* F1 N" u3 {# B6 cDisadvantages of Recursion0 z: j% ]4 G( l# d% k4 q2 D6 B" o/ T

    $ l( l% w8 c% V    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.0 K+ b" F. }' ?" X% F+ {

    : ?" y3 o( J) @! O/ ~    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).. x& _1 Q7 Z5 {, B3 D3 H
    * b. E  i( ^1 ]7 _  [3 N! C
    When to Use Recursion6 Z: {# B6 ]9 K/ _! e: p7 E

    ' I% S& a# F: X0 a) S4 z& ?5 k    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    : d9 c" u, S, o/ B* l$ Y5 n$ I. p3 \8 d* M0 ?" D& t; w, l
        Problems with a clear base case and recursive case.
    3 j2 |. k  ]! `, ^0 B- ~# ^: }1 e$ j! S# m* s
    Example: Fibonacci Sequence
    / a+ ~6 R" }  g! }
    , ?5 s/ C# B; ZThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:# `( I4 V* c  M4 Z' E6 @
    - L' i3 r8 b/ k
        Base case: fib(0) = 0, fib(1) = 1. @) I7 J3 A2 |+ @

    " B1 E4 x- R. _* i& A- J+ J    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    3 G/ y% v* J1 M6 l8 F$ e/ i$ H
    ( `( s) L3 v' u- a* C4 ]python) R' t8 o; o3 I* t  D# b

    8 @' R+ o' {' F9 P! y: L, O7 H
    / z) `' c% ?! ]2 edef fibonacci(n):
    8 ]) i7 [2 F2 Y) \8 ~    # Base cases$ y; Z0 q9 M5 V1 b# t& {3 n! b' n( h4 L, a
        if n == 0:
    4 G0 |  N0 F, ^8 ~: E( C6 u. U        return 0
    ' p* _$ s5 R; A; ~4 v9 O' f. m    elif n == 1:
    ; d( q) G% h% d4 E1 t+ }: J1 K7 A        return 1
    ) r" O. |) n# e6 X+ O    # Recursive case
    4 K  z. _' k9 Y! n* ~    else:/ h6 e" _1 R9 E: J
            return fibonacci(n - 1) + fibonacci(n - 2)
    5 h& @8 _+ ]1 y% j: ^$ \
    " d6 Y2 G+ a, T* B( `# Example usage- }) n( B9 m% [
    print(fibonacci(6))  # Output: 8
    ' [, N5 K! q" B9 M' A5 B/ Q7 g; d* G, q0 u  ~# c
    Tail Recursion, K$ z2 R. \1 V% L
    ; ]% V! d9 I/ t9 V( ~! j: b
    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).: h& p' Q: b1 S. o

    ' R8 a2 B  |5 V3 M) BIn 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-3-13 23:13 , Processed in 0.064265 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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