设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ( V- j; C( s) b$ G1 Z/ X6 y5 l

    ; ]  n: ~! k7 x# f8 X解释的不错, p! g9 q9 ?6 k6 `7 N& |$ o6 [2 H
    2 w+ w% n" e+ {# O& E5 B
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    : M6 ?6 l3 `9 B' e: h# X& f2 s
    " `1 }. L+ F& `5 T 关键要素( ?) w6 ]' T  _6 L% l& V
    1. **基线条件(Base Case)**
    + d5 G1 l5 f' Q! {# E" a( s! k  _   - 递归终止的条件,防止无限循环# n+ @. E& R" b9 c
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    8 G' v! g& K; J: o( v9 t
    5 J/ |1 @/ ^8 Y  a+ X/ M/ H- k2. **递归条件(Recursive Case)**7 x$ f/ o/ L) e9 [7 ~, f
       - 将原问题分解为更小的子问题
    / s7 f; j2 p1 C2 g$ }   - 例如:n! = n × (n-1)!0 O4 C) ], a* a% r" e( x3 G  L6 a
    % y# m+ u2 {5 O- r/ M
    经典示例:计算阶乘* Q2 Q7 z' H. h
    python' i0 Q7 V* j; K* @
    def factorial(n):
    2 t; Z. K+ ]2 ~! @* P( g" \    if n == 0:        # 基线条件" D# q9 {& t( }$ d2 H
            return 1
    7 `! R0 g1 z* [. b/ K2 }    else:             # 递归条件
    " a0 f/ O& [7 F7 t' g# T        return n * factorial(n-1)
    5 k0 R8 y7 ~4 q% g9 A执行过程(以计算 3! 为例):' q: k5 n0 |" u
    factorial(3)
    / Q% V, A: K, r3 J0 X3 * factorial(2). z, r0 E$ M6 k) |/ E: H
    3 * (2 * factorial(1))
    0 g+ F1 ]7 i+ Z4 F3 * (2 * (1 * factorial(0)))
    1 z4 T2 K' h4 F+ B" t' [3 * (2 * (1 * 1)) = 6
    ! h+ V0 W2 K/ l9 |7 ?# M, g9 s. h5 g, Q0 R
    递归思维要点6 E1 X3 W" i- z4 `! X% a# K- u& Y
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑9 z5 t! T* s' c3 K. \
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    5 v  N* w3 O+ w3 e7 t3. **递推过程**:不断向下分解问题(递)
    " c4 J& Q- s6 O% V2 U1 ^4. **回溯过程**:组合子问题结果返回(归)5 X0 ]( ~6 T- P- G" s" Q

    ' n3 Q/ w7 |; N 注意事项
    7 `2 c- X5 A  U$ F3 V' R  l8 P$ J8 j必须要有终止条件
    " J1 w8 M- p" l& u" P4 _递归深度过大可能导致栈溢出(Python默认递归深度约1000层)' f7 ^' U3 |! L, _) R( a
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    , B# i! @  k- @; R+ M8 H0 m尾递归优化可以提升效率(但Python不支持)
    0 g  g9 }. g( ?; d
    ' u4 x4 ?) `+ F# X0 q5 @& `& T 递归 vs 迭代7 S% K' e$ g$ [1 U& @/ I' K
    |          | 递归                          | 迭代               |! J" a8 o' b/ |  [# {
    |----------|-----------------------------|------------------|* \7 q- f6 G6 [. ?4 {" T3 v6 A
    | 实现方式    | 函数自调用                        | 循环结构            |
    " b  D# _! u; a| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    1 t0 w* w! R' U$ e1 g$ }! F| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ( m1 l, j: U- w* v; e/ D| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    1 Q, T6 o( V& I
    ' e! V  Z- K+ f/ _; I 经典递归应用场景8 n# k" R, L2 O. w
    1. 文件系统遍历(目录树结构)  t7 E& `0 {- ]$ f' _+ N* b$ d  `9 W
    2. 快速排序/归并排序算法
    0 U0 X) [0 P  g- H" Y" A3. 汉诺塔问题
    # i) Q* f/ b4 }4. 二叉树遍历(前序/中序/后序)
    3 o3 ]' A1 R  U' z+ L5. 生成所有可能的组合(回溯算法). k7 e8 d5 N- T( q8 A7 Q

    $ h8 m9 c, l! G2 N3 M试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    昨天 07:12
  • 签到天数: 3185 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,# E, F4 N. j, q7 }: ^
    我推理机的核心算法应该是二叉树遍历的变种。
    7 c7 a7 z8 M" u! h另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:9 L4 e" G" f, R' g; f
    Key Idea of Recursion  m# l  P. ^+ f0 l% N+ b
    ) s+ H3 h( n; `
    A recursive function solves a problem by:( @$ m, x6 T& S" e

    4 u: a, \/ k5 w2 C& @. J    Breaking the problem into smaller instances of the same problem., D- z0 ~* g7 S

    , o+ g, i3 W$ o4 _    Solving the smallest instance directly (base case).  m& l/ x! ?. f/ u4 \7 b

    5 D2 {$ K* |8 Q% ?) Q* z* G    Combining the results of smaller instances to solve the larger problem.5 ^& S4 Y1 m( y

      ~* C" _- m: P/ yComponents of a Recursive Function
    ; E- _; m2 N3 W' W6 b7 a4 R4 x/ X" h% G+ C# j
        Base Case:
    " _3 u% y* Y# Z+ ?  b5 _# }+ O: Q' \
    2 [0 {9 y9 E; h: ?        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.4 Z1 ~0 a! F! C3 o: ^% I

    " {2 e# M  z4 O$ \, H7 ]9 {% r9 j        It acts as the stopping condition to prevent infinite recursion.
    0 Z( X% n+ @/ y6 }- z7 S- {1 T, @$ o$ Q+ h/ a
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    / r# m8 l& S$ E2 R. X
    * k9 k! y3 U2 H+ c2 S    Recursive Case:8 S0 N, ]- u* V8 Y* ~4 y- d

    , l! t9 V1 K9 f9 Z- u        This is where the function calls itself with a smaller or simpler version of the problem.: e- Y; w+ B+ z" V" I

    * ~! X7 g0 k# d0 @. i        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).! O: u5 P  P" p' v
    . I! f; p; g, o! c+ c% G
    Example: Factorial Calculation3 u8 E8 A" ^1 N1 J7 X: V2 C# \

    3 k' u) i/ M# f/ ~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:* S- y' D; m; K) U9 W7 E6 o
    * Z' g, M9 A; `' F
        Base case: 0! = 1
    3 T: @- v- w# e8 o2 C& x" j7 v. }4 ~4 z
        Recursive case: n! = n * (n-1)!
    & h2 j2 u6 i5 ~+ S: J, H1 U: m7 J8 l: E4 i( b: i- Q4 i3 B# H
    Here’s how it looks in code (Python):
    1 d8 a( [7 w- n% u: j+ |* Npython: I5 A$ P4 k# z! {7 h
    : v* k9 `* t' S" o' T
    - z, p$ y4 F4 n0 c1 t; A) B; d( |9 G
    def factorial(n):
    " R& g  q2 I, h    # Base case
      G/ Z; i6 }& n8 T  g    if n == 0:4 a6 x5 u1 g3 \; n  C: U
            return 1
    8 W* Q2 z: o& l* s    # Recursive case, f' z4 |7 \$ ^7 R: ^
        else:
    0 o3 p. W( g4 ^& p* a        return n * factorial(n - 1)
    / Q2 K! Y2 b1 w" L1 g; a3 w: _! V
    - V, M8 u, j8 ^5 `- e( f7 ~# Example usage
    + E- j5 n! ^1 e$ h3 \print(factorial(5))  # Output: 120
    + i- [0 x+ {+ M2 ?8 [. I4 {0 W9 k/ w% x2 J! p3 _' R( P
    How Recursion Works
    2 H1 b7 m: ^' \4 _. Y3 E
    ) {5 e" J$ g$ D    The function keeps calling itself with smaller inputs until it reaches the base case.% A7 g0 W. y0 z0 o7 i0 l# L, F

    ! O: q8 G# M" l# z; y( y8 t6 j    Once the base case is reached, the function starts returning values back up the call stack.) w+ F/ a5 s& m
    6 }0 d  e- u9 T* b2 j
        These returned values are combined to produce the final result.* b* @1 d7 V; w3 M
    ) ]0 p9 j0 f3 J) `
    For factorial(5):
    # B4 I+ S  x. d$ U
    4 @3 W' u9 }) A0 b  V4 o0 e: V8 C8 q/ ~1 ~
    factorial(5) = 5 * factorial(4): y9 m& Z- i' m! O/ P3 K& X
    factorial(4) = 4 * factorial(3)
    7 d  i# B  _/ B) Efactorial(3) = 3 * factorial(2)
    3 \5 G8 R- F( r4 Pfactorial(2) = 2 * factorial(1)
    + @  ~, n- j( P. D7 jfactorial(1) = 1 * factorial(0)3 F/ }  c5 J3 G& l! y  [
    factorial(0) = 1  # Base case
    * N) k2 c' U0 k' ^/ v# @7 s; G- J# g
    Then, the results are combined:. f% t; T  T$ F' a

    : i' F: K. V* |  H* U6 A1 c4 i8 D4 g/ C) \+ J4 [
    factorial(1) = 1 * 1 = 1
    + G" X4 A3 C9 d  J3 Ofactorial(2) = 2 * 1 = 2  i  O" B! ]9 H5 J6 A6 F
    factorial(3) = 3 * 2 = 6
    1 C: W# s) [! G6 ffactorial(4) = 4 * 6 = 24
    # v5 M9 ]6 U5 b5 K% mfactorial(5) = 5 * 24 = 1204 r+ @  M) L0 Z# i5 n0 W
    " v5 G) f' V. v1 d+ C% N5 i) |
    Advantages of Recursion
    / Z& I  N6 g% e9 R* M& X  P! [6 ?7 M+ B5 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).8 W7 Y' F9 D9 g

    ! i  d, {- ]3 |8 Z) ^+ X0 R0 [    Readability: Recursive code can be more readable and concise compared to iterative solutions.9 T# y& v5 Z4 ^, m$ Y
    4 b* t" I$ h! x4 ]) R; j
    Disadvantages of Recursion
    3 f! i3 w' c8 h- R+ E9 v' B
    8 Y2 H8 z( V# d    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.
    , }* n9 N& r, |' d+ T3 o. J# E7 p3 [. Q/ w& [! N/ `# b4 Z1 \
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    . ^9 a2 U2 W3 e! d( _& s7 m) D+ i5 {- ]/ e" e  A1 H- q
    When to Use Recursion9 u( G/ S  `- N1 q
    ; l3 Z; y5 l7 |% B6 O
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).( M4 k: S* [3 H  n

    & H5 C& M+ R& M+ [    Problems with a clear base case and recursive case.9 [' g9 y5 ?; f

    8 q3 d* B& h# L" u) @$ `- }! c8 hExample: Fibonacci Sequence. X3 v: D3 V3 I
    * E6 A0 m( v' C7 K+ {
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    # O7 m" t. i& z! K7 p( w7 k$ x
    5 X3 m& V- E9 L) d" `" `    Base case: fib(0) = 0, fib(1) = 1
    * U- M- V0 C0 N* S% N/ x
    6 ?" f: h( @) Q5 t8 q& y    Recursive case: fib(n) = fib(n-1) + fib(n-2)5 b( F6 ?, T& t$ Y
    ) o# D7 Z% A6 E, b7 S7 r! z$ ?3 m5 J
    python
    , b4 S9 V/ T) U& {, C
    8 p4 J6 m6 h9 E: u, x. }8 Z# e3 I4 Q& x. b# Q$ _6 D+ z1 k
    def fibonacci(n):
    5 |/ g$ e1 r$ }9 S9 E7 d5 C' x    # Base cases6 \5 T' S6 W5 w& E
        if n == 0:
    3 o7 f" t! K3 b1 F+ C3 M        return 0
    ; x& i$ M: |3 ~1 k) m2 {    elif n == 1:
    ( U7 \1 N* l  J8 e% u, s        return 1, L: ]9 w5 _5 f1 U8 N  {7 L
        # Recursive case2 c* m. E  \. r/ \
        else:$ |+ m% i' p9 ]' S
            return fibonacci(n - 1) + fibonacci(n - 2)( O  P( |% Q- o  v

    : T' n8 E+ m" @5 w2 p- M: H6 b/ `, |2 M; T# Example usage, H% X3 j& s+ k7 O: ?
    print(fibonacci(6))  # Output: 8' |: E( J8 z! \

    ( n, ]. j' K) D9 n, K, {' U! ^9 PTail Recursion
    . Z' P* T% x3 s/ A; |, a# H" H
    7 M; Y( D2 |. u5 j- k. \- I; c, ZTail 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).3 w* y  ^3 |7 T- K
    + c5 j! {( v7 h5 X) 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-2-27 00:56 , Processed in 0.056704 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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