设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 2 {: @+ f, l+ `4 a( s5 d6 l
    9 r6 ~4 U- z% h
    解释的不错6 J( ?7 x4 s3 o) H( F: K# \

    2 ]. ?- j6 p6 w) }0 h$ G& y递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。$ d3 G- s- V7 p7 U" j& r3 b

    8 W6 H* v* l# I0 Z- G7 m 关键要素
    / p6 D1 r9 d" p8 A- Y- k1. **基线条件(Base Case)**; q- g3 k" r& ^  n; N9 w8 T& n! N' x8 ]
       - 递归终止的条件,防止无限循环
    2 q% B) q6 B! J1 Z3 N   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    7 c! S1 X* J# l
    / s5 m# C1 q7 C2 f6 Q( N% a# i8 \2. **递归条件(Recursive Case)**
    & T. j- n6 c3 x# S   - 将原问题分解为更小的子问题# Y- E0 ^/ H+ h7 Z$ T5 t* N/ O3 r+ @0 Q
       - 例如:n! = n × (n-1)!
    1 Q0 R( u5 o" f3 U- Y9 b+ O9 U. s5 V* t8 a$ t$ r+ Y
    经典示例:计算阶乘
    ! G) n: x: I0 H: i: A9 U/ t) y' ]python" l" V% |/ J2 W6 o' x
    def factorial(n):# I4 j! w8 Y4 l; a: ^
        if n == 0:        # 基线条件
    / n4 i7 E0 y; a, F        return 1! w" u8 o  Q0 S' v
        else:             # 递归条件
    ) Y7 p# o0 C; z8 O& Q# [/ ~* `        return n * factorial(n-1)5 C0 L. J; ]  l8 y
    执行过程(以计算 3! 为例):' F8 w  P3 [* R2 M
    factorial(3)1 Y  P4 @4 s9 I2 Y( T6 h: Y# B' L
    3 * factorial(2), g. s& _' g) I$ }& p, [
    3 * (2 * factorial(1))
    5 X( O4 G" @5 t! U2 U2 W3 * (2 * (1 * factorial(0)))
    # {+ E0 E/ `& m. Q/ F) c! F/ Y7 c3 * (2 * (1 * 1)) = 6
    7 K: c: h& s7 C, O& X- U( J7 z0 l: M' v( f- d; B! r
    递归思维要点
    * ^6 @! S0 @) c( `# O% }, z1. **信任递归**:假设子问题已经解决,专注当前层逻辑9 ^# o+ v1 \" y& ], M- D9 M
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    6 [/ ~* S7 X- J2 M3 b3. **递推过程**:不断向下分解问题(递)
    $ P. ~/ T2 C2 {0 n4. **回溯过程**:组合子问题结果返回(归)
    4 M9 b+ {/ P- W- \9 K" l& T$ i
    4 h7 V% ]* s( W) @# p) J) L5 u' T7 f 注意事项5 E( j9 G* S& B) @1 X
    必须要有终止条件
    . }2 Q8 t+ ~: x/ T" M' {递归深度过大可能导致栈溢出(Python默认递归深度约1000层)' s( D9 D% ~$ {3 S
    某些问题用递归更直观(如树遍历),但效率可能不如迭代/ x* b: D. V: O2 |) c
    尾递归优化可以提升效率(但Python不支持)5 Q7 |6 N; m( V0 {! x
    + Y0 q8 k& O9 }& {, P5 @& A- t7 D
    递归 vs 迭代" K( e( o. G: P+ _. I* h8 B- l
    |          | 递归                          | 迭代               |% V! `4 T" b7 `- Z% e
    |----------|-----------------------------|------------------|
    : @+ D5 l/ Q$ n| 实现方式    | 函数自调用                        | 循环结构            |
    2 C- N, t* H. Z| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    7 W" h( p# r2 Q. }; H9 m| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |& ], d' X, G0 {" ~4 d
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |# K% W- L9 h- v5 y& U" i
    , Q* r: Y# f5 N- L5 }
    经典递归应用场景' d! V+ [  C  S7 Y
    1. 文件系统遍历(目录树结构)3 N- C! _  n2 G7 O) S
    2. 快速排序/归并排序算法
    , k) O1 y$ [3 ]2 z3. 汉诺塔问题: y9 Z' W- S3 w! \
    4. 二叉树遍历(前序/中序/后序)
    . c4 v" R% t7 A4 G8 y9 C5. 生成所有可能的组合(回溯算法). u" D0 ^, z, x! }6 j; J% T6 Y
    0 A! ?5 r% j; g4 M' o6 U# q, `5 n
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    昨天 05:49
  • 签到天数: 3098 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,) r. ]% S4 f: L
    我推理机的核心算法应该是二叉树遍历的变种。
    ) f, u: Y3 \/ i/ o! f/ k0 A另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:. _! J' _; u, M. Y5 U/ i! p7 ?
    Key Idea of Recursion. f" t) [% |7 y
    ) @) O1 ]: f( M7 j% @
    A recursive function solves a problem by:
    5 X  ^7 S# V' h
    , r" b& F& z* @) d    Breaking the problem into smaller instances of the same problem.) y$ n3 p8 |" s6 G
    7 s# _- @$ `* E
        Solving the smallest instance directly (base case).: G6 w. B: g" d2 j7 _
    : g, ^4 o4 L9 @3 Q: g' f- w
        Combining the results of smaller instances to solve the larger problem.4 R+ h& d  v1 A/ s
    , A* M# F4 X/ h- U! c# \! U/ W
    Components of a Recursive Function+ z5 t( J1 I6 o
    5 @/ {) S7 |5 [: V" G; S
        Base Case:
    3 y- b8 J8 I% v; q% y) |
    ' a4 E5 E$ D* X# g        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    % a- V, A. Z! D3 i6 [! s7 @$ U. i, `6 ]5 r
            It acts as the stopping condition to prevent infinite recursion.
    9 R9 o* ^6 B- t) Y3 x' I9 m' {' X0 t6 [% K1 y
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    # `6 z* f2 ?9 Q, D/ _' I: Y; N' n7 x) Y7 u9 t
        Recursive Case:
    5 U. k# [. u; f$ I$ E* o, m4 M
    3 b. t6 t" h, s        This is where the function calls itself with a smaller or simpler version of the problem.
    ' q8 j7 }& e- P% [
    # q! h0 p$ d& V1 R        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    , _* K) W3 a: l7 j" H; ]$ Q  ]9 Q# D
    Example: Factorial Calculation
    7 _9 |  U" N' u  `3 b1 p. W
    & g& L+ T6 d8 V# J6 C: g7 xThe 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:5 ]3 F# h) y) G

    0 |: T3 {. C$ a" J+ n6 }* q    Base case: 0! = 1" Y& c1 c* d3 h$ f, o; L

    ; l3 D4 g( W' E2 P, e    Recursive case: n! = n * (n-1)!
    % C( k  C9 w2 O$ t" V# @
    , _# y: V6 c; z- K" z3 BHere’s how it looks in code (Python):
    % ]; A5 G; ^" x. Qpython2 p* t' Y: \, l- Y
    , E, f) i+ X# j  C1 |9 T( G: h
    & V8 K; ?2 s" @- J/ A0 z
    def factorial(n):
    / N% t) E) S, c; `    # Base case8 H+ {' S7 }' h
        if n == 0:
    1 Y1 `; K2 p# r' a; K        return 1# o6 n) b, T# u8 F
        # Recursive case* {& q* c! i. T2 V3 t' n) R
        else:
    + T: l% l$ L( q5 R        return n * factorial(n - 1)  X# g* N) D; q9 ~/ c! h' _1 Z

      Q/ z& Y+ h/ `6 X4 @# Example usage
    * P; w3 p& T+ W% Tprint(factorial(5))  # Output: 120. i' H, C- v- K. r& s$ u. `
    ! L' I" O1 m- K
    How Recursion Works# O" \8 |: Y; w$ G
    - L( d8 ]0 N, `& }) h
        The function keeps calling itself with smaller inputs until it reaches the base case.3 _6 m  m7 T4 K0 p0 z
    . z# D* v5 j6 O: `: \, f
        Once the base case is reached, the function starts returning values back up the call stack.
    1 }. O8 {: n7 N$ t/ {& }# m: `- J) |" l1 `
        These returned values are combined to produce the final result.* ^/ Z: H+ `% L* M1 n. Z, e# B3 z
    2 b2 x0 Y9 N! Y) A( t
    For factorial(5):8 f+ N4 @+ ^8 i( N9 ^$ e/ u& H

    : x: M* y5 M8 P4 Y) e& l# X
    : _, j- y- D+ R3 sfactorial(5) = 5 * factorial(4)
    2 _  d2 U5 R  ^+ v0 ]factorial(4) = 4 * factorial(3)
    6 N) i$ @% {! }( W/ o4 `factorial(3) = 3 * factorial(2)
    - r% E& q8 a# M9 e4 l' dfactorial(2) = 2 * factorial(1)6 L& u' z8 l* X/ q
    factorial(1) = 1 * factorial(0), J$ {( P) C0 M) k& a. h, y, P% H% e
    factorial(0) = 1  # Base case: h7 p& m  K0 K( @/ X$ U
    4 |) B$ T. [: _2 s
    Then, the results are combined:6 r0 J& q+ H+ n

    ; P$ _3 F, k1 C0 n, B6 l" \& S- A, }; [6 `6 j) U( F
    factorial(1) = 1 * 1 = 1
    ; Y5 S5 ^. Y% j* N1 E0 Tfactorial(2) = 2 * 1 = 2  B. Z% q( B( B& E; v: B, C
    factorial(3) = 3 * 2 = 6( W, a# J# P4 ~; p% P3 o
    factorial(4) = 4 * 6 = 24
    4 v( {" R2 [' P; N/ L3 V6 j4 kfactorial(5) = 5 * 24 = 120
    ( u: o/ ]- ]* e; N3 r4 |- d
    * B2 S/ h( p* M1 S5 F; L5 KAdvantages of Recursion+ `+ w0 N" {9 {5 |
    # k, }! w0 M( ?7 j
        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 s/ D% h) ]! B1 H: p$ l
    # A5 v) L8 A" Z    Readability: Recursive code can be more readable and concise compared to iterative solutions.
      g: n; V8 H- q" I( A6 j9 a: ]% D" g& C/ Q& ^8 h0 f  T- o
    Disadvantages of Recursion
    * W  W8 ]( D* B& z3 w4 g5 S" Z: ?+ q. `
        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.& ~3 S& C' {8 j# ^. j
    7 ?! [4 |. s3 `* c! L
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).4 q/ @  ~, i6 d7 q1 d/ I# Z" j
    % x& |) Q5 ~) T8 X( {* y
    When to Use Recursion
    7 H  ~/ q; H; {& k) J6 V& J2 M& X) E8 N, q; ?& U
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).5 _) \% v9 t. a, }2 H- x& h0 T

    ( A" S0 ?: U* r) k# q& \  r    Problems with a clear base case and recursive case.
    1 E% V5 W3 d+ A7 A! s$ |; p. ]: {# Y2 u
    Example: Fibonacci Sequence) b0 k  Q* [- m- V$ {+ I/ y1 Z- a
    ( L; q0 C; p/ `
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ( L/ B+ D) y2 r, `% ?# `6 H4 V2 X# l$ ]4 m" B% u7 G; c3 g& P; i
        Base case: fib(0) = 0, fib(1) = 1
    & a* n! s" A" {" U4 a( A4 n& X; ^% H3 s4 s" l; Y
        Recursive case: fib(n) = fib(n-1) + fib(n-2)3 M5 e( n9 V! N" J
    1 R; w$ p- P$ O% W
    python9 v, y9 r( ?0 F, s

    " h7 E1 m( F% y" r* t+ g# n% W0 W# `& G6 H
    def fibonacci(n):$ p! v; W% a  m6 d+ |$ H9 `
        # Base cases
    " N* T  b. z# F4 \9 g2 M    if n == 0:
    * d3 q7 x$ t' u, P# C) b, T2 o        return 0
    ! P3 T& {' E" v7 D2 S3 y    elif n == 1:
    9 N1 d- q* b$ g, K+ G9 \% S& N        return 1
    2 G# c) _  V# d2 Z* p: D    # Recursive case
    * L. `$ J7 N  O6 R+ X# e  |$ a    else:) N! U  [, q5 l  i! O" E- S1 F
            return fibonacci(n - 1) + fibonacci(n - 2)" ?! O5 y$ U6 i0 R

    : w! i0 T- X3 L" C$ Q) {# Example usage
    0 w/ V5 Z+ F" ~7 y4 E8 Xprint(fibonacci(6))  # Output: 81 Z% i8 A; J9 O# U1 l, B, l
    " {7 S0 L& ^8 M
    Tail Recursion0 R5 z8 s# ~: @6 _# t9 N0 ~4 b! Q
    7 \4 u- @3 L8 i
    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).5 E7 ^. i9 G% s5 t1 j- C4 k

    * x/ \. j$ E- ^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-11-22 06:28 , Processed in 0.035834 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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