设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ) g8 M% C; F4 [9 |2 H

    ) x$ n) u' \" R' ]5 }解释的不错0 S- p) `: I6 j/ Q1 b: U; ~
    # B- d( ]8 F  `7 {7 j9 t9 u
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。. V& c2 U: D2 G% t2 M8 q; ^
    ) G$ \7 n1 d- `# t: z) C$ P* c5 v
    关键要素
    ) v: z' L9 D" U" @: y1. **基线条件(Base Case)**
    : y+ y: k0 a9 ]  |: i2 S   - 递归终止的条件,防止无限循环
    / F/ `$ S9 m, {   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ; `* ?# a3 p0 Q9 x6 B9 U' J2 U3 C2 x' R2 W
    2. **递归条件(Recursive Case)**7 u$ ~& q/ T$ C6 d# W3 d
       - 将原问题分解为更小的子问题
    8 L* d5 ]0 g! W# v  d2 v5 e2 c   - 例如:n! = n × (n-1)!
    ( S1 F# ^' Z% \. I" h
    & `. U/ z" [. j' O9 w. g 经典示例:计算阶乘! w' K/ j7 O3 {& W5 i2 q# r' r
    python1 {& X- }/ O" k% m
    def factorial(n):
    8 r& S2 S7 `) L: }& k1 X    if n == 0:        # 基线条件
    3 `$ |) ~; x( Y        return 13 b0 o: y3 J- J
        else:             # 递归条件- i4 n4 V( b2 |# O
            return n * factorial(n-1)1 u3 Q# y$ M! u7 i5 f# E. ~0 Y$ o0 x
    执行过程(以计算 3! 为例):
    : [3 R( ?$ S6 _factorial(3): f! `  j1 ?. z! K- J) g: ?
    3 * factorial(2)
    8 ?3 ~( g4 q6 S3 * (2 * factorial(1))
    , r" S7 X9 Y/ u% ?4 S3 * (2 * (1 * factorial(0)))! [" w  z) p( L2 \
    3 * (2 * (1 * 1)) = 61 X, U" ?3 p7 R% ?

    9 ]" M& W  @' ?- d$ g 递归思维要点. p" b) A9 _' l' N7 H+ S( k" t7 a6 U
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ' t' A- |+ X/ K6 A6 F* n2. **栈结构**:每次调用都会创建新的栈帧(内存空间)9 w/ O! p7 Q# I% y9 d
    3. **递推过程**:不断向下分解问题(递)% L2 D( w  r6 {+ V
    4. **回溯过程**:组合子问题结果返回(归)
      \' a8 j7 A* z) h% R/ C7 C' S- i9 w7 C- x5 y
    注意事项; w# I) }$ G- d
    必须要有终止条件
    * u  R2 p5 @) n递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    8 U8 l# @5 _, p5 I6 L某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ; p* O5 u- t% P尾递归优化可以提升效率(但Python不支持)6 Z% |$ p9 \6 W, F3 N

    4 k; ]7 u6 d( t 递归 vs 迭代5 W8 W) _! V8 b
    |          | 递归                          | 迭代               |1 @! m& @9 `( y
    |----------|-----------------------------|------------------|2 O3 |9 m# d4 o7 C+ U4 W" a
    | 实现方式    | 函数自调用                        | 循环结构            |
    : d- ~- u1 C  X| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |  o, x: C/ b3 k5 |& I" ]4 g  J  r
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |: w4 B& f3 o% ]: I4 z! V
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |$ r8 l$ R& v7 E+ I& h+ N" V
    $ M  N! o. s' i
    经典递归应用场景" K- a' L: ^9 w# n* a$ a4 l
    1. 文件系统遍历(目录树结构)& n; T' M- ?/ _) u# D6 a
    2. 快速排序/归并排序算法% U; G- ?! I  y: U
    3. 汉诺塔问题( M$ z1 \7 h1 {
    4. 二叉树遍历(前序/中序/后序)/ V# y( }' A- j1 {
    5. 生成所有可能的组合(回溯算法)
    ! W! r; l5 d; e4 G0 A( {: V) {4 _% w3 ~& {* A$ O( U5 e- e) T! I
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    昨天 07:08
  • 签到天数: 3184 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    , T/ o& E. e/ x我推理机的核心算法应该是二叉树遍历的变种。6 W4 z8 f+ s: f- v# Y: Z( }& {0 y
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    3 J/ N0 E- ]1 z; F* U5 n+ l' J" B% sKey Idea of Recursion- m$ D' \& `0 v, b5 P7 |& X, v. ?
    9 W2 c, k4 t: G  o1 p8 }0 H
    A recursive function solves a problem by:
    1 P( |! l0 k4 Y# h3 s, |
    " h5 H) y# f# P$ o    Breaking the problem into smaller instances of the same problem.
    # z7 q. o& k) F/ O7 G; c  e2 I
    , \5 t. M8 [. I' l    Solving the smallest instance directly (base case).  u7 B, ]# q) s8 e! j5 I
    3 Y) z) G- F1 k2 `5 G* l+ M
        Combining the results of smaller instances to solve the larger problem.& c( R1 F9 [1 m! E: S4 n; E, Q
      Y% t7 l9 R& T( M( j
    Components of a Recursive Function- m+ f  M  i! L# @( [8 J# Z- n

    : \6 T5 z  g, m; p2 ]& ?# |    Base Case:
    / j9 u2 m6 z) c, z
    ; e. E4 z: P3 r" l& [) x. ?; A        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    , V0 m  A$ r4 ^* H7 Q0 @4 E' F' S! U
            It acts as the stopping condition to prevent infinite recursion.- [, w/ q* h; A
    9 t" v2 p8 X1 c+ v6 N
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.( F; j, D+ X, |3 H! X: @
    $ P( ~( l  j2 g+ e# q0 ~
        Recursive Case:
    4 Y9 u0 D$ t( r$ m" s; X* t; j
      b6 H3 s& d) I! M        This is where the function calls itself with a smaller or simpler version of the problem.
    7 J& I) O! W+ }
    7 K) ~; H- C# M( F$ b+ o        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).: S( c- u- j* K" J) x# O0 S

    6 W. |/ t- u# D5 NExample: Factorial Calculation
    4 {, C) L# X2 n* T& w1 A( |* z$ Y& a( ~; P# ?! m
    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:! L4 `# B8 R# k
    " R* {( o# ^* A( x0 b* L
        Base case: 0! = 1
    3 N! _$ T& O  f/ k: @$ a$ H9 R3 \& S( U' S# s  L" [, A+ B& N. l
        Recursive case: n! = n * (n-1)!. O5 y5 }; i7 B  k9 F) q$ h$ ]

    " d% V& ?, f- x' sHere’s how it looks in code (Python):
    + e0 }0 X3 B, n) {8 zpython
    & ^4 Q# U+ ~8 D( }' T* ^3 T4 l6 m1 {) L+ R7 v$ u( N9 ~
    ' K5 u% k) U' b* k: Z3 o
    def factorial(n):
    : L! C: Z% J6 x6 L8 z    # Base case
    4 D1 `7 m& v- z7 `    if n == 0:
    ' S  |. M* b: ]  y9 x! C8 Y        return 12 o8 ^1 p0 A% T# X
        # Recursive case
    + r, x6 x8 L1 U' j" f5 ^    else:
    4 a1 b; w# M" b3 {0 V        return n * factorial(n - 1)
    1 B+ @7 E; `% Y3 P" }
    4 L- [7 A. V1 H2 `- D& e# Example usage! }* z' ~$ {" |5 c- @/ `
    print(factorial(5))  # Output: 120. d4 _  b- ?6 |4 I

    * U4 f+ h) p9 L5 Z5 M7 \. j& eHow Recursion Works  n/ j0 K$ ^  b* C

    : ~# \( b9 P* \: D/ ?0 k( q    The function keeps calling itself with smaller inputs until it reaches the base case.
    , X: D3 h1 p3 {* j* r$ v0 R( L6 a6 {4 Q7 ^
        Once the base case is reached, the function starts returning values back up the call stack.# z. O1 g) I( c8 _* g& e' Y
    & P2 t4 B& ?' C% a/ D. X- O& M
        These returned values are combined to produce the final result.; L2 Y9 C, ^7 h' G2 `/ j2 ~

    " ]3 d2 V7 c+ L/ }# A6 kFor factorial(5):
    + ]( I# G8 _* E& W8 u" a0 J" c1 R3 \7 i8 D7 S4 s+ a5 V

    . M9 ]" _; N: H# m% z# L4 G/ ~factorial(5) = 5 * factorial(4)
    0 ], `; ]3 {2 }2 f- Nfactorial(4) = 4 * factorial(3)
      X8 U5 e$ D* ?4 o: S$ A5 mfactorial(3) = 3 * factorial(2)
    # @- I" |5 F1 d5 {* ufactorial(2) = 2 * factorial(1)
    ! [5 v9 D; H8 kfactorial(1) = 1 * factorial(0): G1 u9 z6 b, j, p" u* [% Z0 I
    factorial(0) = 1  # Base case1 |7 w8 G& _  v
    - V  ?  {' E7 Z8 G! R) ?) ?
    Then, the results are combined:
    $ K, ]( [# ^6 G+ T0 r; a: z( X+ s  B* h2 Z; q  P9 T$ R

    / Q6 A3 ?8 _, H9 afactorial(1) = 1 * 1 = 1$ I' B& [7 A% J0 C) d8 s- E
    factorial(2) = 2 * 1 = 2
    0 U. X. f( v* vfactorial(3) = 3 * 2 = 6
    $ }3 M' k0 K+ D2 j' vfactorial(4) = 4 * 6 = 24; {5 K+ u; a2 L. U: d1 w2 K
    factorial(5) = 5 * 24 = 120% E8 p) A3 h& A; A' N5 Y) ~) C0 N+ O
    7 u" Z' S8 P3 ~0 X
    Advantages of Recursion' a6 o, X4 A1 p5 b

    1 j* \2 D- l* 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).4 ]4 {. \$ I& i
    : p% O, X# \- j4 w( f- I* J
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    7 {( t" ^( r: G3 Y" T+ G" C3 ]" z# h  y0 S
    Disadvantages of Recursion
    , J! L3 N( d5 I  Q, |; _: [# B2 d/ v- m
        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.
    2 f2 x$ `5 ~0 q- |! l
    ' u0 ?, H) J  a! D% J+ V    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).; E' s7 a4 p* i9 C4 K: O% a2 A% l5 d

    0 ?" }9 `& B0 i% hWhen to Use Recursion
    $ v3 i" g. h+ ]4 ~  }6 J
      m) K/ o1 B  Y6 @: W0 E    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    0 }* d' j! m3 j' w9 ]5 a
    8 W  ~- c: v$ |, k  t" V4 t    Problems with a clear base case and recursive case.
    2 v7 D( J( ~. l# `. C' x
    # O6 w3 F; I. f0 k/ fExample: Fibonacci Sequence
    % M4 A  K- [8 R+ u1 ~+ e; _; G: C( l  o' ^
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:0 j0 O. B' Y9 y+ J4 w/ ]
    * N# i. D' b2 A
        Base case: fib(0) = 0, fib(1) = 1; n# y! K& t1 `: A
    5 h0 I; G9 }- I: i' F" B1 u( Z
        Recursive case: fib(n) = fib(n-1) + fib(n-2)' r, [. c  o4 u0 R. s$ N
    3 A+ P# D8 _" a! l
    python
    ) N! r" ^- W- Y3 M( y% `% l0 X3 F4 l* E4 F" L9 O

    , \% A6 C- J5 R% cdef fibonacci(n):) A: W/ n7 s/ f* s6 K2 F8 P8 d
        # Base cases1 U6 r* S& x+ A6 b
        if n == 0:- ?6 M! m% ^$ R) \% Q3 ~. D5 X' Y
            return 0
    3 ^5 T. u: M9 {    elif n == 1:
    ( `' {8 a9 r# \& z        return 1
    9 z0 o/ O9 ~# m3 G* q9 @    # Recursive case
    2 {0 \. L( G( J/ y1 Y    else:# R! m( [: i# t/ g( E+ q
            return fibonacci(n - 1) + fibonacci(n - 2)
    ; {9 N/ f1 e( J+ ]0 y2 O4 x+ O+ P8 U" t& b! [% s% a1 W- T
    # Example usage
    , e- e; ?; q1 |& W8 Qprint(fibonacci(6))  # Output: 8
    6 G( c3 L) \( Y0 k# X/ E
    - O* d, f* ]" `7 I& _- H4 @Tail Recursion
    $ B3 ~4 Z0 k' X! Y8 N$ a; T; i# g; ]$ S8 `' G* S
    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).# P. H* R( [% @" z2 W  J) s- F- U, A

    " z6 Y1 h* x2 H" I$ ~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-26 02:09 , Processed in 0.056277 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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