设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 , t1 O) K# k2 Z8 y3 A: y8 z* C

    & N' n. X* d! X4 Z& B解释的不错
    0 d; N  V, h7 D2 e/ T# G9 o; o% ~3 u4 A0 X# h) w
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    $ f! v7 [! S2 O: S
    ' Z3 O! O/ v8 _# }: r, y3 O1 F 关键要素& S; @, G5 G4 _3 R0 M6 ^
    1. **基线条件(Base Case)**
    * P3 r/ G& E/ p   - 递归终止的条件,防止无限循环
    8 {2 w1 _  |" U   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    " n6 l5 [$ v% j' Y: d0 [
    + Z& y; P2 U! P- B( n2. **递归条件(Recursive Case)**
    / r/ c0 g7 y; g0 O) U! F   - 将原问题分解为更小的子问题+ u4 Y! m1 L5 [/ a* a5 \
       - 例如:n! = n × (n-1)!3 w- A1 z" X2 o5 M6 a  r

    : I/ W9 U! F) u7 F. C 经典示例:计算阶乘3 ^* N* e2 w, H0 G
    python; h% P/ V, {7 K! U7 Q
    def factorial(n):
    & w& Z8 s  `+ r7 H8 R: l    if n == 0:        # 基线条件7 k$ G4 {8 I4 s2 E- K
            return 1% h6 y: f5 [# t% ]  g, G! @* w
        else:             # 递归条件
    " y) V: s, b; N; m/ D1 J) Y% P        return n * factorial(n-1)( J: M- @9 g3 N# D2 w- M
    执行过程(以计算 3! 为例):( ?+ Q; w" j6 s6 ?1 r
    factorial(3), \5 F, S3 R+ H' w- o  k/ _
    3 * factorial(2)5 ?5 K' n! m2 V& U! i: ]
    3 * (2 * factorial(1))6 K! A2 ^' ~$ M. w# ]( j. I
    3 * (2 * (1 * factorial(0)))
    * q9 n6 M  k% x. ~3 * (2 * (1 * 1)) = 67 H% P! T6 u9 G1 Y# S
    ( s: n# w4 c  r- I* T& V0 P
    递归思维要点
    $ M8 \* R+ [8 p! r& }1. **信任递归**:假设子问题已经解决,专注当前层逻辑2 i9 ~0 b: ^7 t6 U: \3 x
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    - s3 |8 d2 r0 c& {3. **递推过程**:不断向下分解问题(递)- T9 a! p' {$ y& j
    4. **回溯过程**:组合子问题结果返回(归)  t: C7 \; R7 c) g

    2 t7 r' ^7 X0 C* e 注意事项
    ' d* F  S; `9 p/ T7 M8 G0 l必须要有终止条件
    4 o9 }- j6 ?: k6 c递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    : G7 f/ K1 ~# q$ @+ g4 h" q某些问题用递归更直观(如树遍历),但效率可能不如迭代' g* C/ v% [: d5 I4 \% z
    尾递归优化可以提升效率(但Python不支持)
    - y6 j0 u; P" [
    " d, B$ l. C# M* d# H, I2 ~( v 递归 vs 迭代
    0 u4 S5 s' M1 F8 x0 A4 c% L|          | 递归                          | 迭代               |
    " w9 v" R" r& d" a8 C0 @' \: D8 u|----------|-----------------------------|------------------|
    , N+ {/ V7 ]) `9 T| 实现方式    | 函数自调用                        | 循环结构            |( Y$ [8 o3 m1 ?# J! o$ W) K( n
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |) H. R8 _. R8 C" ^
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |1 {7 U( {/ s. `
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |5 O  s) G4 Q9 S$ o9 b5 ]
    2 C* g/ H0 b8 a- z$ U2 G
    经典递归应用场景
    2 y2 J6 m5 d* O9 R1. 文件系统遍历(目录树结构)1 t9 Y! q  R2 a3 i% [9 F0 Z3 E1 A
    2. 快速排序/归并排序算法
    " \6 X& p+ _$ V7 k, y3. 汉诺塔问题' ]7 @+ W# v. P/ L, e3 m1 L
    4. 二叉树遍历(前序/中序/后序)( x: A' ?6 o' m3 L" n1 R; X* k! ?
    5. 生成所有可能的组合(回溯算法)
    % O. t1 x; g- `% F  b; k- L
    ' j6 z1 X, ]' i9 A3 y7 `) b6 \试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    15 小时前
  • 签到天数: 3337 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,2 q3 T& [3 t% ~
    我推理机的核心算法应该是二叉树遍历的变种。
    2 _9 V2 y) K& t5 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:# k! ?  d  |% |! X4 K$ ?6 I
    Key Idea of Recursion; g. @$ v( X- K+ w- ~
      I4 i& H) N& u7 I3 E& p
    A recursive function solves a problem by:
    " s2 o" B. X1 q. f, x# S  `
    ! C* @# l0 X$ c# O6 P    Breaking the problem into smaller instances of the same problem.
    + p. |& Z. [  Z: {; `6 K- S% G; j8 y# a: w4 O' D
        Solving the smallest instance directly (base case).7 ^1 W4 P3 K: e2 R. g, |: Z
    & H+ u: C" G7 s
        Combining the results of smaller instances to solve the larger problem.6 a3 o: A7 F! F* `- K
    / F/ W5 w( x8 ]
    Components of a Recursive Function3 o( J- |5 W1 R& D9 N# Y. p
    8 k2 H# m* f! r8 j' X
        Base Case:3 M3 e% k) d7 g1 @* |6 t
    1 \4 X, Z' F/ J  T1 ^5 _
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.; }5 g- q) F/ a* y0 d

    ; t2 v' s+ g+ N. n* ~# Y        It acts as the stopping condition to prevent infinite recursion.
    , c9 o) [) n7 u4 g3 E% r1 e: ], }# t
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.  y8 K: g5 T: i+ }* N

    4 N" |6 U/ h- ?* v    Recursive Case:2 b" z$ w. ^; A3 b) v4 z! F
    5 `! [" w: W7 A; _7 {
            This is where the function calls itself with a smaller or simpler version of the problem.
    8 ]1 V- K4 Z  V
    - [' Z- L9 O  D" ], r3 K        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    & A* D1 @* }/ ^4 E9 P$ d7 K' }8 {5 g0 z: S1 v  w
    Example: Factorial Calculation( [0 N5 q4 ~8 b& k+ Y% R8 p% ~; Q

    " W$ ^0 _6 i3 Z. {* P9 l) EThe 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:0 ?2 |3 x+ G% `

    7 k9 U* K. E( d0 Z  K. _    Base case: 0! = 1, D6 w, K& \- c# v& _* E! \
    $ B1 O  P$ o9 W4 S& Q2 W1 D
        Recursive case: n! = n * (n-1)!% l6 S; S* |) ~9 q: V: ?" H
    3 J, g1 y# P! ~" |$ _
    Here’s how it looks in code (Python):9 ~4 b# P' O  @, |# n
    python$ g, X9 e2 e; X0 r3 t9 \; \$ J9 \8 t

    . U% X" G, ?) R: |4 S; C. W
    , b( X) a) E! s4 p0 Q3 M0 cdef factorial(n):
    / n4 E$ a. D. \1 j    # Base case
    % f2 v8 h! w3 q# Q# C; W1 a    if n == 0:  U- A7 g0 Y' x2 L# }
            return 1
    ! M8 O/ W# k: v( L    # Recursive case. s# X) z( R% d& |
        else:
    6 [# _& P6 t- T; r5 I6 ]2 _        return n * factorial(n - 1)
    : P+ J' {( `+ v7 w* H# g* ~0 S9 @" P- f: L+ {- b' i
    # Example usage6 n1 h4 T. \. R6 a) D# }+ s* k
    print(factorial(5))  # Output: 120( ~! t( B& O9 u0 m

    # |/ R* q/ ]+ N  J# M% D% [How Recursion Works' N# @8 H) R9 C- c

    - S! r/ k+ a4 o2 \  m2 p9 b3 L, |: [    The function keeps calling itself with smaller inputs until it reaches the base case.
    " S8 A: U5 i/ }
    ; K0 M% u1 f9 h% G" U/ h    Once the base case is reached, the function starts returning values back up the call stack.
    $ w6 Q" `( l5 ?7 n+ R0 O1 C0 t9 b8 S" ?6 m0 e! A
        These returned values are combined to produce the final result.
    1 s: X& A: q' S* Z5 M( w4 Y/ Q" @, Q9 L4 B! u
    For factorial(5):3 w3 H; o4 k* p! E6 w

    5 x) g9 T2 ?6 L% B/ ^4 F2 Y5 i% B6 f  T) t& c  S
    factorial(5) = 5 * factorial(4)
    # Q9 U# h1 U  l& o3 C, i5 lfactorial(4) = 4 * factorial(3)
    2 }; q" N% j4 ~# s: I& zfactorial(3) = 3 * factorial(2)0 ?' Z& s; `4 n! C+ J
    factorial(2) = 2 * factorial(1)) M) `+ l% _; r' Z; o
    factorial(1) = 1 * factorial(0)
    ) \: `. a; k+ Z1 c. Y+ c) lfactorial(0) = 1  # Base case
    2 o9 h9 g+ E* I& I7 \0 X2 |5 M4 E$ g7 L# `' Q/ l" P% |/ P# u
    Then, the results are combined:
    ' M6 _' |- d: O0 H7 o3 _9 a: x* v. z9 F
    1 |5 M3 M' R; ^/ w; @1 y
    factorial(1) = 1 * 1 = 1% f/ f. ^, X7 D& K) H
    factorial(2) = 2 * 1 = 2
    & G' U; h: z% L& r* }factorial(3) = 3 * 2 = 6
      c+ t6 y, [; p7 [3 S) E- U! bfactorial(4) = 4 * 6 = 24
    " z- S6 b  R3 t0 s4 u' Vfactorial(5) = 5 * 24 = 120, D" N- H- Y) }# q+ S

    ( [) ~% C  u  W) P5 C/ w+ _Advantages of Recursion
    % P( i" `* a& y! [$ [% [/ T+ P/ \
        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).: u# N% A! B5 B  x# l4 W

    1 J: y- }% ?' c* k* o  ~; v    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ; b. F1 `# `) ]) o
    ) |! z+ ?9 y, f% `# v) J% F  VDisadvantages of Recursion& H' |6 d: E7 q' {" R( x. R) i" _

    " X' E$ A, [! G1 H" @1 a    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.
    / Y4 D$ {3 O. b7 m7 V( x) i  z- r$ h+ y+ c* W% {, l
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    3 b# ^% V# F. B* t$ A0 E1 B' B8 f1 \( G
    When to Use Recursion
      M/ W' ]. @4 F  v* I
    1 f% H5 r7 R3 U5 B3 O    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).4 ^- d; O  b7 v
    $ j7 K6 q' m3 b1 S# F5 d
        Problems with a clear base case and recursive case.0 ?: j' G7 r) o  I8 ^. N

    9 v9 |5 S1 y" P2 D: cExample: Fibonacci Sequence3 y1 v% l, x" L2 Q* f
    2 n# V* ^, f. N3 z7 B, `# h7 P
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ' i1 X, g2 P' |, ^' }' @& e
    " J4 Y- _( j2 K. g    Base case: fib(0) = 0, fib(1) = 14 X$ a- d% X3 m7 m$ o
    , A7 g  T$ m) T+ f4 R
        Recursive case: fib(n) = fib(n-1) + fib(n-2)  w+ |0 L) j# V" E" T" F9 h
    : U6 h0 h+ L, U3 {# |
    python
    ( M9 K" [6 ^2 ^* t0 E4 S
    ; g. [- k, F# M6 n& a- L
    . [+ m% q8 {' H2 `& y! J% bdef fibonacci(n):# j6 ^& C+ i5 l0 Y/ _7 F
        # Base cases+ F7 ^, I" u) @# ^" x; \8 m
        if n == 0:2 e  s* q( ]0 w
            return 00 P: F7 N! K0 @  `# Z
        elif n == 1:; T. b. x" y0 J3 A, a# R  K5 d
            return 1
    & u# A6 ]' g' p6 c$ ]. Z    # Recursive case
    ! s. A) s1 l1 A  G% I: y' Q0 \    else:& Z# E( a$ {- ?, Y$ a
            return fibonacci(n - 1) + fibonacci(n - 2): M3 |! x# n, n' o8 z1 w

    1 k. A& n" @+ e4 @* W9 S# Example usage
    0 n+ X) Z- d* X* b6 j4 Rprint(fibonacci(6))  # Output: 8' N6 k- Q/ l& u( u0 s
    , O' H, k. X) N+ m# ^
    Tail Recursion3 G9 W2 E# n4 [$ v& N

    ! z- Z4 F: D' y. L. CTail 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).
    " s) I( `& V" T0 t# I- V" k+ }; G, s9 b# I9 C9 Y2 u) L- K
    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-9-1 21:05 , Processed in 0.057207 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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