设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 " K2 w& k7 Q8 D( o

    $ C; N1 k1 T- ^. j: c解释的不错+ [* R( o6 P  U+ @) S
    5 w: `* S# V, r" |6 g: l* N
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    . b0 @5 x. f2 J
    , T$ t+ [( G* l* e+ Y) X0 a8 r 关键要素
    $ Q' I* R1 [# G" q1. **基线条件(Base Case)**
    ; b6 y2 C2 z4 Z* M/ d8 \' p   - 递归终止的条件,防止无限循环. `, I% x/ \3 G# c( e$ E
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    : c3 s% `  h) z- o& ?3 h' ~- r( ^5 B3 t
    2. **递归条件(Recursive Case)**' K7 G, g3 e1 y/ d9 Z7 Q3 g: D
       - 将原问题分解为更小的子问题0 c$ k9 @1 h- ^5 D1 C
       - 例如:n! = n × (n-1)!
    ( A' [. x6 `' t1 u5 r
    ) t( B  F8 K+ s3 L$ Z 经典示例:计算阶乘
    / r' N. q+ n& j) b, ~7 t( zpython; ?0 h1 J, t9 N% t
    def factorial(n):9 b* v, v0 q! s
        if n == 0:        # 基线条件
    ( G, r) v0 @/ i* b% ~2 M        return 17 {7 |& A" `& H
        else:             # 递归条件
    ) L. J$ Q- x6 M2 O2 Z# a        return n * factorial(n-1); i6 ?! W# P/ S8 [7 `# J: K
    执行过程(以计算 3! 为例):
    ) }8 j) M' N! M! b+ M3 J! O9 efactorial(3)
    ) y. B% w5 ^/ p3 * factorial(2)$ P% u1 _( w1 l/ l' k
    3 * (2 * factorial(1))
    ' H% }/ [$ M) w3 * (2 * (1 * factorial(0)))
    * W6 \# g9 T1 m# V9 R4 c2 E3 * (2 * (1 * 1)) = 67 T, Q& h  V2 S+ d( y

    8 @9 A0 q7 N4 h/ H 递归思维要点3 c# n+ v2 |0 y2 j4 z+ \: e
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ! f& L4 K$ W9 S2 D! {* z2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    9 |( L) y! a! {) w# G6 ]  h/ t1 \3. **递推过程**:不断向下分解问题(递)
    1 j+ P, A  _2 G" {3 z! `4. **回溯过程**:组合子问题结果返回(归)! ~1 u1 `( C9 y% y. s
    / w! w" G2 Q4 G0 D: v
    注意事项+ P, j1 A8 K5 f9 A4 C! N
    必须要有终止条件3 }! u8 p8 W3 V; i
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)6 o; L$ R) ^# V; D/ w* C
    某些问题用递归更直观(如树遍历),但效率可能不如迭代- q  n; k3 G% O
    尾递归优化可以提升效率(但Python不支持)
    ; D6 a4 F5 d; G( M9 o7 \& G
    8 ]* T! a- Z- k8 W 递归 vs 迭代
    + X+ g- x3 K, O; T4 a6 H* P, J|          | 递归                          | 迭代               |
    6 A: {! ^: v' I|----------|-----------------------------|------------------|
    ) L1 }7 [' A" o' |7 E| 实现方式    | 函数自调用                        | 循环结构            |
    9 L$ x0 j8 B- |( y" z| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |+ f6 O( `6 L! x, e  y
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    7 y# C/ e5 i& n0 L& \7 M' q| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |/ R0 C7 y. ~. b% C: L% _; z
    . x. ]8 f4 p1 U# U' b
    经典递归应用场景" ~" _+ e! y- ?3 S' Y
    1. 文件系统遍历(目录树结构)1 y1 F* M- e9 n3 b4 V0 @
    2. 快速排序/归并排序算法+ |* c8 W' o" q6 Q7 u" q
    3. 汉诺塔问题: s5 [7 T& {( q. a! k. q! H, Z
    4. 二叉树遍历(前序/中序/后序)
    & l  T" T- L. V& W# [! ?5. 生成所有可能的组合(回溯算法)9 y* y# S3 n2 W5 A4 `9 F/ b/ S

    # z" W* @( b0 |试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    昨天 14:35
  • 签到天数: 3132 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    / y; C0 a' [' z' Y' r3 ^3 ?我推理机的核心算法应该是二叉树遍历的变种。% p/ O. z6 p5 \# t$ V
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    2 T: j) d# C5 JKey Idea of Recursion
    ) M, _) l- ^0 W( R) j  L' c$ m# p% O
    A recursive function solves a problem by:
    . p2 Q8 P! [( R; f* h% Y7 v" ]" L% F5 N: ]: I0 J. _
        Breaking the problem into smaller instances of the same problem., _3 J- J3 b: M; n# F

    2 M. o# R) f4 P3 a7 X  N% V    Solving the smallest instance directly (base case).; V9 E2 H0 Y& T  z, I* U
    1 Z+ B1 Y* H/ E; O
        Combining the results of smaller instances to solve the larger problem.
    2 K/ `/ H+ d/ T# @: I( d8 v* x3 H) f' d) Q. s5 ]- w3 H
    Components of a Recursive Function
    0 K1 c5 G3 O! K1 z/ j9 [
    2 J( |" X& ?! I4 F+ f0 d    Base Case:4 q' v9 E/ Z: V. g+ s
    3 j+ A6 v- m8 N/ [/ Q
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ! |# v7 @3 @$ U' u7 g" @) y4 m7 Y! g2 K' @0 j7 ]
            It acts as the stopping condition to prevent infinite recursion.
    7 J2 f9 P- o* K% t& r8 l5 t5 @& t5 Y7 {8 X7 V
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    $ }3 y! f8 W( _" r. Z
    8 d1 X* R  G  |% S/ k    Recursive Case:
    , F" J+ u% I: d2 L" G6 y$ o" M, V0 b/ U4 u, S/ R% p
            This is where the function calls itself with a smaller or simpler version of the problem.
      S# r- l" f* h$ b% S5 a+ N/ k$ ]1 R& ~* R) y  u1 f; q
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    $ l; H. Q+ t+ d  B4 w6 N7 I" [6 T* _' ~, e% r, C
    Example: Factorial Calculation
    $ `4 f! h& m% c* r# u1 S
    1 ^2 i0 G( c3 c7 ?$ z: Y5 B! TThe 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:
    8 D6 G9 j$ T0 |2 k! K: q. {" @/ }' g7 Q1 h1 g- i. g! p
        Base case: 0! = 1! h- L/ Q/ K% e& S( G- C

    1 \0 ?) ^: [! }( ~    Recursive case: n! = n * (n-1)!+ e, h7 J8 _+ _9 m
    + K; U: k  U4 q9 e) [3 n9 l0 x  j7 `
    Here’s how it looks in code (Python):4 O/ O( \' x: Y: B
    python
    3 e* P4 s$ A" A' d2 @$ h7 U) |1 s
    ; M$ ]& q0 Q/ r% X- C
    ! K: o) \. M2 r, r9 xdef factorial(n):1 p" Y* d+ E/ F5 w
        # Base case
    4 ]. e! _4 B4 M' k# r  o$ g    if n == 0:6 g9 s! B# j9 P' ]4 L
            return 1
    : z7 O' c5 q- v6 N4 l    # Recursive case8 K6 J4 L- a' ^9 A
        else:& h; y. [$ j6 c
            return n * factorial(n - 1)
    # v. v; [9 H$ v2 M$ W
    ) ^' X* o; \, j& o; ?# Example usage* [+ i" c- N, C
    print(factorial(5))  # Output: 120
    & _2 }4 I/ t" e6 r- a& C
    ( J2 m( L. v8 C" r9 `' w3 }How Recursion Works$ L' k; ?0 H- N  |  n/ O

    ! }7 y3 C3 _0 f. v0 \$ {( I4 G    The function keeps calling itself with smaller inputs until it reaches the base case.1 x4 Y2 A/ a, N8 X
    # ~# H" E- j9 g7 q
        Once the base case is reached, the function starts returning values back up the call stack.
    ; W2 L8 t2 g  a; d: j% J/ M
    ' C3 M9 d- d1 z! B% n+ u    These returned values are combined to produce the final result.6 ]! g7 Q8 r# M+ |
    3 `  \- S' I  Y1 p1 a) E
    For factorial(5):
    ! c9 ~% H8 u- ^- M: f2 N. i" l! f8 X! G! h

    5 v$ @3 x* W% X% d) R, U( ^4 efactorial(5) = 5 * factorial(4)$ i6 O: e" q* Q# e
    factorial(4) = 4 * factorial(3)
    : b! C& e6 y1 {% ifactorial(3) = 3 * factorial(2)' J- \4 \" e- p# U* U! G
    factorial(2) = 2 * factorial(1)5 V4 f# \5 w; }) l, G; R
    factorial(1) = 1 * factorial(0)+ A2 c$ y% t/ u# a2 ~! H
    factorial(0) = 1  # Base case
    5 m9 C# o! u) H7 d3 f. ]1 }) s$ o; [: w7 y5 S5 \% u4 l: p
    Then, the results are combined:
    4 h5 \, I$ R9 P( z) I6 h
    / |" ~; o( Y5 P2 H, F% l- r
    8 P& o& ~2 ?- ffactorial(1) = 1 * 1 = 1
    0 ^* v2 Y3 }$ j. d6 u$ Lfactorial(2) = 2 * 1 = 2( O' z; j- Y2 ]' k
    factorial(3) = 3 * 2 = 62 f' S% f" g: [) G' N  o
    factorial(4) = 4 * 6 = 24, p9 i4 p1 m+ C4 _
    factorial(5) = 5 * 24 = 120
    7 ~7 v: J, e! @: j9 Q/ _! A
    ; J  Y5 u& g% L& o/ ^. iAdvantages of Recursion
    7 P4 F) M8 N6 \+ f$ w. {7 j! E8 ~
    $ A$ t: ]  [; s- W) i    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).
    ; m# \7 g" q7 \0 D8 p+ O
    ( V. M8 D1 j3 i    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ; B9 i/ S1 c  Q! L0 F0 w) r9 m: o0 q1 K, ~; x
    Disadvantages of Recursion- k, m4 M/ y' o* C& z

    5 h! n- ]+ t( ~& p' g    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.
    7 ~3 Q! u- R! [& t- q* `) |! S" M+ j6 @" S% P2 V- K
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ' n5 y4 v$ E) G1 i, {( }0 K& K8 I, V  I! Y4 q
    When to Use Recursion0 _' g1 [7 S4 v, M) W

    5 B' Y: S6 \. ?1 t( ^* U& K0 P" E3 A    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).8 }/ ~/ Z. r( j: f' ]6 F, A

      j7 w; N' p* u. ^9 T    Problems with a clear base case and recursive case.
    9 Y& y& {  C) D
    & E0 E. T! K5 g5 rExample: Fibonacci Sequence# y+ E' D& {7 S2 s- f" e
    " K2 q: M$ ~0 q* k3 k
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:: J, ?6 O9 |# |, F# k- s
    # W* X. o6 }" e+ @/ m0 S) ?
        Base case: fib(0) = 0, fib(1) = 1: }9 X0 K7 U6 \
    0 x8 a" D, X2 s( y" r3 e
        Recursive case: fib(n) = fib(n-1) + fib(n-2)+ z+ \# i# Z+ p- d4 p! k
    8 f( `: o* J/ e# f/ k8 n* z/ z
    python
    9 S4 L) J8 Y) ?8 }" x
    " J  h2 n+ U0 y- L, ~6 @$ {! ~0 ?: [3 y, L0 {, Q4 j
    def fibonacci(n):  ^2 m2 W3 s  {8 c
        # Base cases5 n, d% r1 r- v5 q7 t2 v
        if n == 0:
    , @; S& f; `4 j4 a! O9 Q; b        return 0
    " n1 c' q- [& e8 F9 V$ S; C$ c    elif n == 1:
    $ r! s1 I# n; a& _# m  A7 D        return 1
    4 T6 ]& l! n- X    # Recursive case0 N4 I6 C# F8 \5 {/ J. \& w2 B& I
        else:$ U$ D- h6 G" ^# V
            return fibonacci(n - 1) + fibonacci(n - 2)3 r2 G' `+ r1 {+ z' a) w! z+ K7 j
    4 [+ m+ S8 K$ h  A
    # Example usage
    8 @# M; v6 W! V) fprint(fibonacci(6))  # Output: 8' d! o/ C2 \1 L, Q! H0 D0 h9 s
    , s. D9 w4 J3 `! [
    Tail Recursion' Q4 M% R% d1 u

    ' B+ S6 f$ z( S% X8 ]' i! O+ G( GTail 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).
    ; n& Y3 N; @6 e8 S# A' e8 o% g8 @1 \( i1 H% y3 F; u1 P
    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-1-1 06:20 , Processed in 0.031922 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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